1 | #include <math.h> |
---|
2 | #include "nrutil.h" |
---|
3 | #define ROTATE(a,i,j,k,l) g=a[i][j];h=a[k][l];a[i][j]=g-s*(h+g*tau);\ |
---|
4 | a[k][l]=h+s*(g-h*tau); |
---|
5 | |
---|
6 | /* |
---|
7 | Computes all eigenvalues and eigenvectors of a real symmetric matrix a[1..n][1..n]. On |
---|
8 | output, elements of a above the diagonal are destroyed. d[1..n] returns the eigenvalues of a. |
---|
9 | v[1..n][1..n] is a matrix whose columns contain, on output, the normalized eigenvectors of |
---|
10 | a. nrot returns the number of Jacobi rotations that were required. |
---|
11 | */ |
---|
12 | void jacobi(float **a, int n, float d[], float **v, int *nrot) |
---|
13 | { |
---|
14 | int j,iq,ip,i; |
---|
15 | float tresh,theta,tau,t,sm,s,h,g,c,*b,*z; |
---|
16 | |
---|
17 | // TODO |
---|
18 | // b=vector(1,n); |
---|
19 | // z=vector(1,n); |
---|
20 | |
---|
21 | |
---|
22 | for (ip=0;ip<n;ip++) { // Initialize to the identity matrix. |
---|
23 | for (iq=0;iq<n;iq++) v[ip][iq]=0.0; |
---|
24 | v[ip][ip]=1.0; |
---|
25 | } |
---|
26 | for (ip=0;ip<n;ip++) { // Initialize b and d to the diagonal of a. |
---|
27 | b[ip]=d[ip]=a[ip][ip]; |
---|
28 | z[ip]=0.0; // This vector will accumulate terms of the form t*a[pq] as in equation (11.1.14). |
---|
29 | } |
---|
30 | |
---|
31 | *nrot=0; |
---|
32 | for (i=1;i<=50;i++) { |
---|
33 | sm=0.0; |
---|
34 | for (ip=0;ip<n-1;ip++) { // Sum off-diagonal elements. |
---|
35 | for (iq=ip+1;iq<n;iq++) |
---|
36 | sm += fabs(a[ip][iq]); |
---|
37 | } |
---|
38 | if (sm == 0.0) { // The normal return, which relies on quadratic convergence to machine underflow. |
---|
39 | |
---|
40 | // TODO |
---|
41 | //free_vector(z,1,n); |
---|
42 | //free_vector(b,1,n); |
---|
43 | return; |
---|
44 | } |
---|
45 | if (i < 4) |
---|
46 | tresh=0.2*sm/(n*n); // ...on the first three sweeps. |
---|
47 | else |
---|
48 | tresh=0.0; // ...thereafter. |
---|
49 | for (ip=0;ip<n-1;ip++) { |
---|
50 | for (iq=ip+1;iq<n;iq++) { |
---|
51 | g=100.0*fabs(a[ip][iq]); |
---|
52 | if (i > 4 && (float)(fabs(d[ip])+g) == (float)fabs(d[ip]) // After four sweeps, skip the rotation if the off-diagonal element is small. |
---|
53 | && (float)(fabs(d[iq])+g) == (float)fabs(d[iq])) |
---|
54 | a[ip][iq]=0.0; |
---|
55 | else if (fabs(a[ip][iq]) > tresh) { |
---|
56 | h=d[iq]-d[ip]; |
---|
57 | if ((float)(fabs(h)+g) == (float)fabs(h)) |
---|
58 | t=(a[ip][iq])/h; // t = 1/(2*theta) |
---|
59 | else { |
---|
60 | theta=0.5*h/(a[ip][iq]); Equation (11.1.10). |
---|
61 | t=1.0/(fabs(theta)+sqrt(1.0+theta*theta)); |
---|
62 | if (theta < 0.0) t = -t; |
---|
63 | } |
---|
64 | |
---|
65 | c=1.0/sqrt(1+t*t); |
---|
66 | s=t*c; |
---|
67 | tau=s/(1.0+c); |
---|
68 | h=t*a[ip][iq]; |
---|
69 | z[ip] -= h; |
---|
70 | z[iq] += h; |
---|
71 | d[ip] -= h; |
---|
72 | d[iq] += h; |
---|
73 | a[ip][iq]=0.0; |
---|
74 | |
---|
75 | for (j=0;j<ip-1;j++) { // Case of rotations 1 <= j < p. |
---|
76 | ROTATE(a,j,ip,j,iq) |
---|
77 | } |
---|
78 | for (j=ip+1;j<iq-1;j++) { // Case of rotations p < j < q. |
---|
79 | ROTATE(a,ip,j,j,iq) |
---|
80 | } |
---|
81 | for (j=iq+1;j<n;j++) { // Case of rotations q < j <= n. |
---|
82 | ROTATE(a,ip,j,iq,j) |
---|
83 | } |
---|
84 | for (j=0;j<n;j++) { |
---|
85 | ROTATE(v,j,ip,j,iq) |
---|
86 | } |
---|
87 | |
---|
88 | ++(*nrot); |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | for (ip=0;ip<n;ip++) { |
---|
94 | b[ip] += z[ip]; |
---|
95 | d[ip]=b[ip]; // Update d with the sum of t*a[pq], |
---|
96 | z[ip]=0.0; // and reinitialize z. |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | printf("Too many iterations in routine jacobi\n"); |
---|
101 | } |
---|