-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectop.cc
187 lines (142 loc) · 4.2 KB
/
vectop.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
double scal_d (double *a, double *b, int dim)
{
int i;
double temp;
temp = 0.0;
for (i = 0; i < dim; i++)
{
temp += a[i] * b[i];
}
return (temp);
}
/*******************************/
double norm_d (double *a, int dim)
{
return (sqrt (scal_d (a, a, dim)));
}
/*******************************/
double dist_d (double *a, double *b, int dim)
{
int i;
double temp;
temp = 0.0;
for (i = 0; i < dim; i++)
{
temp += (a[i] - b[i]) * (a[i] - b[i]);
}
temp = sqrt (temp);
return (temp);
}
/*******************************/
void my_invert_symmetric_matrix(double **m,double **invm, int size, double *eigenvalues, double **eigenvectors){
int p,q,r,nrot,i,j,l, nsweeps;
double theta, t, c, s, tau, Sum, a, avg_entry;
double *rowp,*rowq;
rowp = d1t(size);
rowq = d1t(size);
for(i=0; i < size; i++){
eigenvectors[i][i]=1.0;
for(j=i+1; j < size; j++){
eigenvectors[i][j]=0.0;
eigenvectors[j][i]=0.0;
}
}
nrot =0;
for(nsweeps=0;;nsweeps++){
if (nsweeps > 50){
printf("Error. Reached maximum number of sweeps for Jacobi convergence. Exiting...\n");
exit(1);
}
Sum=0.0;
avg_entry=0.0;
for(i=0; i < size; i++){
for(j=i+1; j < size; j++){
Sum += 2.0 * m[i][j]*m[i][j];
avg_entry += 2.0*fabs(m[i][j]);
}
}
avg_entry = avg_entry/(size*(size-1.0));
for(p=0; p < size; p++){
for(q=p+1; q < size; q++){
/* For the first sweeps only perform rotations to kill the
off-diagonal matrix elements which are larger than average
*/
if ((nsweeps <3) && (fabs(m[p][q]) < avg_entry)) continue;
theta = (m[q][q]-m[p][p])/(2.0*m[p][q]);
if (fabs(theta) > 1.0e+10) t = 1.0/(2*theta);
else t = 1.0/(fabs(theta) + sqrt (theta*theta+1.0));
if (theta < 0.0) t = -t;
c = 1.0/sqrt(t*t+1);
s = t*c;
tau = s/(1.0+c);
/* Apply the Jacobi rotation to get the new M matrix */
for(r=0; r < size; r++){
rowp[r]=m[r][p];
rowq[r]=m[r][q];
}
m[p][p] = rowp[p]- t*rowp[q];
m[q][q] = rowq[q]+ t*rowq[p];
for(r=0; r < size; r++){
if (r==p) continue;
if (r==q) continue;
m[r][p]= rowp[r] - s*(rowq[r] + tau*rowp[r]);
m[p][r]= m[r][p];
m[r][q]= rowq[r] + s*(rowp[r] - tau*rowq[r]);
m[q][r]= m[r][q];
}
m[p][q]=0.0;
m[q][p]=0.0;
Sum = Sum - 2*rowp[q]*rowp[q];
/* Combine the various rotations to get the matrix of eigenvectors
*/
for(r=0; r < size; r++){
rowp[r]=eigenvectors[p][r];
rowq[r]=eigenvectors[q][r];
}
for(r=0; r < size; r++){
eigenvectors[p][r] = rowp[r] - s*(rowq[r] + tau*rowp[r]);
eigenvectors[q][r] = rowq[r] + s*(rowp[r] - tau*rowq[r]);
}
if (Sum < 1.0e-12) goto end;
nrot++;
}
}
}
end:
/* the diagonal elements of the rotated matrix contain the
eigenvalues */
for(r=0; r < size; r++){
eigenvalues[r] = m[r][r];
}
/* Construct the inverse matrix by doing the spectral decomposition
and eliminating the null-eigenvalue space */
for (j=0;j<size;j++) {
for (i=0;i<size;i++) {
invm[j][i]=0.0;
for (l=0;l<size;l++) {
if (fabs(eigenvalues[l])>0.0000001) invm[j][i]+= 1.0/eigenvalues[l]*eigenvectors[l][j]*eigenvectors[l][i];
}
}
}
printf("End matrix inversion. Sorting eigenvalues... \n");
/* Now let's sort the eigenvalues (and eigenvectors) in descending
order */
for(j=1; j < size; j++){
i = j-1;
a = eigenvalues[j];
for(r=0; r < size; r++) rowp[r]=eigenvectors[j][r];
while ((i >=0) && (eigenvalues[i] < a)){
eigenvalues[i+1] = eigenvalues[i];
for(r=0; r < size; r++){
eigenvectors[i+1][r]=eigenvectors[i][r];
}
i--;
}
eigenvalues[i+1]=a;
for(r=0; r < size; r++){
eigenvectors[i+1][r]= rowp[r];
}
}
free_d1t(rowp);
free_d1t(rowq);
}