-
Notifications
You must be signed in to change notification settings - Fork 0
/
p5.cc
136 lines (130 loc) · 2.85 KB
/
p5.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
#include <stdio.h>
#define MAX_SIZE 10
#include <math.h>
void swap(double *x, double *y) {
double t = *x;
*x = *y;
*y = t;
}
int Solve(int n, double a[][MAX_SIZE], double x[], double b[]) {
double y[MAX_SIZE];
for(int i = 0; i < n; i++) {
double sum = 0;
for(int j = 0; j < i; j++)
sum += a[i][j] * y[j];
y[i] = b[i] - sum;
}
for(int i = n-1; i >= 0; i--) {
double sum = 0;
for(int j = n-1; j > i; j--)
sum += a[i][j] * x[j];
x[i] = (y[i] - sum) / a[i][i];
}
}
int doolittle_LU(int n, double a[][MAX_SIZE]) {
for(int i = 0; i < n; i++) {
if(fabs(a[i][i]) < 1e-15)
return 0;
for(int j = i; j < n; j++) {
double sum = 0;
for(int k = 0; k < i; k++)
sum += a[i][k] * a[k][j];
a[i][j] -= sum;
}
for(int j = i+1; j < n; j++) {
double sum = 0;
for(int k = 0; k < i; k++)
sum += a[j][k] * a[k][i];
a[j][i] = (a[j][i] - sum) / a[i][i];
}
}
return 1;
}
int EigenV(int n, double b[][MAX_SIZE], double *lambda, double v[], double TOL, int MAXN) {
double l0 = *lambda;
double a[MAX_SIZE][MAX_SIZE];
int k = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) a[i][j] = b[i][j];
for(int i = 0; i < n; i++)
a[i][i] -= *lambda;
if(!doolittle_LU(n, a)) {
return -1;
}
for (int i = 0; i < n; i++) {
if (a[i][i] == 0) {
return -1;
}
}
for(int i = 0; i < n; i++)
if(fabs(v[i]) > fabs(v[k])) k = i;
double tmp = v[k];
for(int i = 0; i < n; i++)
v[i] /= tmp;
double err = 0;
double u[MAX_SIZE];
for(int times = 0; times < MAXN; times++) {
for(int i = 0; i < n; i++)
u[i] = v[i];
// for(int i = 0; i < n; i++)
// printf("%lf ", u[i]);
// puts("----");
// for(int i = 0; i < n; i++)
// printf("%lf ", v[i]);
// puts("===");
Solve(n, a, v, u);
*lambda = 1.0 / v[k];
for(int i = 0; i < n; i++)
if(fabs(v[i]) > fabs(v[k])) k = i;
double tmp = v[k];
for(int i = 0; i < n; i++)
v[i] /= tmp;
err = 0;
for(int i = 0; i < n; i++) {
if(fabs(u[i] - v[i]) > err) err = fabs(u[i] - v[i]);
}
// printf("%.12lf\n", err);
// printf("%.12lf\n", TOL);
if (err < TOL) {
*lambda += l0;
return 1;
}
// printf("%d\n", times);
}
return 0;
}
int main()
{
// test();
// return 0;
int n, MAXN, m, i, j, k;
double a[MAX_SIZE][MAX_SIZE], v[MAX_SIZE];
double lambda, TOL;
scanf("%d", &n);
for (i=0; i<n; i++)
for (j=0; j<n; j++)
scanf("%lf", &a[i][j]);
scanf("%lf %d", &TOL, &MAXN);
scanf("%d", &m);
for (i=0; i<m; i++) {
scanf("%lf", &lambda);
for (j=0; j<n; j++)
scanf("%lf", &v[j]);
switch (EigenV(n, a, &lambda, v, TOL, MAXN)) {
case -1:
printf("%12.8f is an eigenvalue.\n", lambda );
break;
case 0:
printf("Maximum number of iterations exceeded.\n");
break;
case 1:
printf("%12.8f\n", lambda );
for (k=0; k<n; k++)
printf("%12.8f ", v[k]);
printf("\n");
break;
}
}
return 0;
}
/* Your function will be put here */