-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpower_method_prb.cpp
363 lines (309 loc) · 7.72 KB
/
power_method_prb.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
# include <complex>
using namespace std;
# include "power_method.hpp"
int main ( );
void test01 ( );
void test02 ( );
void test03 ( );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for POWER_METHOD_PRB.
//
// Discussion:
//
// POWER_METHOD_PRB tests the POWER_METHOD library.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 30 August 2008
//
// Author:
//
// John Burkardt
//
{
timestamp ( );
cout << "\n";
cout << "POWER_METHOD_PRB\n";
cout << " C++ version:\n";
cout << " Test the POWER_METHOD library.\n";
test01 ( );
test02 ( );
test03 ( );
//
// Terminate.
//
cout << "\n";
cout << "POWER_METHOD_PRB:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
void test01 ( void )
//****************************************************************************80
//
// Purpose:
//
// TEST01 uses POWER_METHOD on the Fibonacci2 matrix.
//
// Discussion:
//
// This matrix, despite having a single dominant eigenvalue, will generally
// converge only very slowly under the power method. This has to do with
// the fact that the matrix has only 3 eigenvectors.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 20 July 2008
//
// Author:
//
// John Burkardt
//
{
double *a;
double cos_x1x2;
double ctime;
double ctime1;
double ctime2;
int i;
int it_max;
int it_num;
double lambda;
int n = 50;
double norm;
double phi;
int seed;
double sin_x1x2;
double tol;
double *x;
double *x2;
a = fibonacci2 ( n );
seed = 123456789;
x = r8vec_uniform_01 ( n, &seed );
it_max = 300;
tol = 0.000001;
phi = ( 1.0 + sqrt ( 5.0 ) ) / 2.0;
cout << "\n";
cout << "TEST01\n";
cout << " Use POWER_METHOD on the Fibonacci2 matrix.\n";
cout << "\n";
cout << " Matrix order N = " << n << "\n";
cout << " Maximum iterations = " << it_max << "\n";
cout << " Error tolerance = " << tol << "\n";
ctime1 = cpu_time ( );
power_method ( n, a, x, it_max, tol, &lambda, &it_num );
ctime2 = cpu_time ( );
ctime = ctime2 - ctime1;
cout << "\n";
cout << " Number of iterations = " << it_num << "\n";
cout << " CPU time = " << ctime << "\n";
cout << " Estimated eigenvalue = " << setprecision(14) << lambda << "\n";
cout << " Correct value = " << setprecision(14) << phi << "\n";
cout << " Error = " << r8_abs ( lambda - phi ) << "\n";
//
// X2 is the exact eigenvector.
//
x2 = new double[n];
x2[0] = 1.0;
for ( i = 1; i < n; i++ )
{
x2[i] = phi * x2[i-1];
}
norm = r8vec_norm_l2 ( n, x2 );
for ( i = 0; i < n; i++ )
{
x2[i] = x2[i] / norm;
}
//
// The sine of the angle between X and X2 is a measure of error.
//
cos_x1x2 = r8vec_dot ( n, x, x2 );
sin_x1x2 = sqrt ( ( 1.0 - cos_x1x2 ) * ( 1.0 + cos_x1x2 ) );
cout << "\n";
cout << " Sine of angle between true and estimated vectors = " << sin_x1x2 << "\n";
delete [] a;
delete [] x;
delete [] x2;
return;
}
//****************************************************************************80
void test02 ( void )
//****************************************************************************80
//
// Purpose:
//
// TEST02 uses POWER_METHOD2 on the Fibonacci2 matrix.
//
// Discussion:
//
// This matrix, despite having a single dominant eigenvalue, will generally
// converge only very slowly under the power method. This has to do with
// the fact that the matrix has only 3 eigenvectors.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 30 August 2008
//
// Author:
//
// John Burkardt
//
{
double *a;
double ctime;
double ctime1;
double ctime2;
int i;
int it_max;
int it_num;
complex <double> lambda;
int n = 50;
double phi;
int seed;
double tol;
complex <double> *v;
double *x;
a = fibonacci2 ( n );
v = new complex<double> [n];
seed = 123456789;
x = r8vec_uniform_01 ( n, &seed );
it_max = 300;
tol = 0.000001;
phi = ( 1.0 + sqrt ( 5.0 ) ) / 2.0;
cout << "\n";
cout << "TEST02\n";
cout << " Use POWER_METHOD2 on the Fibonacci2 matrix.\n";
cout << "\n";
cout << " Matrix order N = " << n << "\n";
cout << " Maximum iterations = " << it_max << "\n";
cout << " Error tolerance = " << tol << "\n";
ctime1 = cpu_time ( );
power_method2 ( n, a, x, it_max, tol, &lambda, v, &it_num );
ctime2 = cpu_time ( );
ctime = ctime2 - ctime1;
cout << "\n";
cout << " Number of iterations = " << it_num << "\n";
cout << " CPU time = " << ctime << "\n";
cout << " Estimated eigenvalue = "
<< " " << setprecision(14) << real ( lambda )
<< " " << setprecision(14) << imag ( lambda ) << "\n";
cout << " Correct value = " << setprecision(14) << phi << "\n";
cout << " Error = " << abs ( lambda - phi ) << "\n";
delete [] a;
delete [] v;
delete [] x;
return;
}
//****************************************************************************80
void test03 ( void )
//****************************************************************************80
//
// Purpose:
//
// TEST03 uses POWER_METHOD2 on the TRIS matrix.
//
// Discussion:
//
// This matrix, despite having a single dominant eigenvalue, will generally
// converge only very slowly under the power method. This has to do with
// the fact that the matrix has only 3 eigenvectors.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 30 August 2008
//
// Author:
//
// John Burkardt
//
{
double *a;
double alpha;
double beta;
double ctime;
double ctime1;
double ctime2;
double gamma;
int i;
int it_max;
int it_num;
complex <double> lambda;
complex <double> lambda_max;
complex <double> *lambda_vec;
int n = 50;
int seed;
double tol;
complex <double> *v;
double *x;
alpha = -1.0;
beta = 10.0;
gamma = 8.0;
a = tris ( n, n, alpha, beta, gamma );
v = new complex<double> [n];
seed = 123456789;
x = r8vec_uniform_01 ( n, &seed );
it_max = 4000;
tol = 0.000001;
cout << "\n";
cout << "TEST03\n";
cout << " Use POWER_METHOD2 on the TRIS (tridiagonal scalar) matrix.\n";
cout << "\n";
cout << " Matrix order N = " << n << "\n";
cout << " Maximum iterations = " << it_max << "\n";
cout << " Error tolerance = " << tol << "\n";
ctime1 = cpu_time ( );
power_method2 ( n, a, x, it_max, tol, &lambda, v, &it_num );
ctime2 = cpu_time ( );
ctime = ctime2 - ctime1;
cout << "\n";
cout << " Number of iterations = " << it_num << "\n";
cout << " CPU time = " << ctime << "\n";
cout << " Estimated eigenvalue = "
<< setprecision(14) << real ( lambda )
<< " " << setprecision(14) << imag ( lambda ) << "\n";
lambda_vec = tris_eigenvalues ( n, alpha, beta, gamma );
lambda_max = lambda_vec[0];
for ( i = 1; i < n; i++ )
{
if ( abs ( lambda_max ) < abs ( lambda_vec[i] ) )
{
lambda_max = lambda_vec[i];
}
}
cout << " Correct max eigenvalue = "
<< setprecision(14) << real ( lambda_max )
<< " " << setprecision(14) << imag ( lambda_max ) << "\n";
cout << " Error = " << abs ( lambda - lambda_max ) << "\n";
delete [] a;
delete [] lambda_vec;
delete [] v;
delete [] x;
return;
}