-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyjacobi.cc
105 lines (98 loc) · 2.28 KB
/
myjacobi.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
/* Written by C. Micheletti, [email protected] */
/* Last revision January 2007 */
/* ============================================ */
void myjacobi (double a[][3], int n, double *d, double v[][3], int *nrot)
{
/* modificata 24/1/2007 per eliminare controlli di uguaglianza tra floats */
int j, iq, ip, i;
double tresh, theta, tau, t, sm, s, h, g, c;
double b[3], z[3];
#define ROTATE(a,i,j,k,l) g=a[i][j];h=a[k][l];a[i][j]=g-s*(h+g*tau);a[k][l]=h+s*(g-h*tau);
for (ip = 0; ip <= (n - 1); ip++)
{
for (iq = 0; iq <= (n - 1); iq++)
v[ip][iq] = 0.0;
v[ip][ip] = 1.0;
}
for (ip = 0; ip <= (n - 1); ip++)
{
b[ip] = d[ip] = a[ip][ip];
z[ip] = 0.0;
}
*nrot = 0;
for (i = 1; i <= 500; i++)
{
sm = 0.0;
for (ip = 0; ip <= n - 2; ip++)
{
for (iq = ip + 1; iq <= (n - 1); iq++)
sm += fabs (a[ip][iq]);
}
if (sm == 0.0)
{
return;
}
if (i < 4)
tresh = 0.2 * sm / (n * n);
else
tresh = 0.0;
for (ip = 0; ip <= n - 2; ip++)
{
for (iq = ip + 1; iq <= (n - 1); iq++)
{
g = 100.0 * fabs (a[ip][iq]);
if (i > 4 && (fabs ((fabs (d[ip]) + g) - fabs (d[ip])) < 1.0e-6)
&& (fabs( (fabs(d[iq]) + g)- fabs (d[iq])) < 1.0e-6))
a[ip][iq] = 0.0;
else if (fabs (a[ip][iq]) > tresh)
{
h = d[iq] - d[ip];
if ( fabs((fabs (h) + g) - fabs (h)) < 1.0e-6)
t = (a[ip][iq]) / h;
else
{
theta = 0.5 * h / (a[ip][iq]);
t = 1.0 / (fabs (theta) + sqrt (1.0 + theta * theta));
if (theta < 0.0)
t = -t;
}
c = 1.0 / sqrt (1 + t * t);
s = t * c;
tau = s / (1.0 + c);
h = t * a[ip][iq];
z[ip] -= h;
z[iq] += h;
d[ip] -= h;
d[iq] += h;
a[ip][iq] = 0.0;
for (j = 0; j <= ip - 1; j++)
{
ROTATE (a, j, ip, j, iq)
}
for (j = ip + 1; j <= iq - 1; j++)
{
ROTATE (a, ip, j, j, iq)
}
for (j = iq + 1; j <= (n - 1); j++)
{
ROTATE (a, ip, j, iq, j)
}
for (j = 0; j <= (n - 1); j++)
{
ROTATE (v, j, ip, j, iq)
}
++(*nrot);
}
}
}
for (ip = 0; ip <= (n - 1); ip++)
{
b[ip] += z[ip];
d[ip] = b[ip];
z[ip] = 0.0;
}
}
printf ("Too many iterations in routine JACOBI %lf",sm);
/* exit (1); */
#undef ROTATE
}