378 |
jab |
1 |
/* LinAlg EigenSolutionSym test program. Andreas Bærentzen 2008.
|
|
|
2 |
|
|
|
3 |
This simple program generates 30 moderately sized dense symmetric real matrices and computes
|
|
|
4 |
the full eigensolution (eigenvalues and eigenvectors). For each matrix and each eigenvector it
|
|
|
5 |
multiplies that eigenvector back onto the matrix and checks the difference between the new vector
|
|
|
6 |
and the eigenvector times the eigenvalue. It also computes the difference between the eigenvalue
|
|
|
7 |
returned and the eigenvalue computed as the norm of a unit length eigenvector multiplied onto the
|
|
|
8 |
matrix.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
//disable the stl warning of debug information conflicting with names of longer than 255 char.
|
|
|
12 |
#pragma warning( disable :4786 )
|
|
|
13 |
|
|
|
14 |
#include <iostream>
|
|
|
15 |
#include <vector>
|
|
|
16 |
#include "LinAlg/Matrix.h"
|
|
|
17 |
#include "LinAlg/Vector.h"
|
|
|
18 |
#include "LinAlg/LapackFunc.h"
|
|
|
19 |
|
|
|
20 |
using namespace std;
|
|
|
21 |
using namespace LinAlg;
|
|
|
22 |
using namespace CGLA;
|
|
|
23 |
|
|
|
24 |
int main()
|
|
|
25 |
{
|
417 |
jrf |
26 |
gel_srand(0);
|
378 |
jab |
27 |
double max_err_eigen=0;
|
|
|
28 |
double max_err_eigen_vec=0;
|
|
|
29 |
for(int i=0;i<30;++i)
|
|
|
30 |
{
|
417 |
jrf |
31 |
int N = gel_rand()%50+2;
|
378 |
jab |
32 |
CMatrix mat(N,N,0);
|
|
|
33 |
for(int i=0;i<N;++i)
|
|
|
34 |
for(int j=0;j<N;++j)
|
|
|
35 |
{
|
417 |
jrf |
36 |
double x = gel_rand()/double(GEL_RAND_MAX)-0.5;
|
378 |
jab |
37 |
mat.set(i,j,x);
|
|
|
38 |
mat.set(j,i,x);
|
|
|
39 |
}
|
|
|
40 |
CVector b;
|
|
|
41 |
CMatrix Q=mat;
|
|
|
42 |
int info = EigenSolutionsSym(Q,b);
|
|
|
43 |
cout << "Info = " << info << endl;
|
|
|
44 |
for(int i=0;i<b.Length(); ++i)
|
|
|
45 |
{
|
|
|
46 |
CVector q;
|
|
|
47 |
Q.GetRow(q, i);
|
|
|
48 |
q /= q.Norm();
|
|
|
49 |
|
|
|
50 |
CVector q2;
|
|
|
51 |
q2 = mat * q;
|
|
|
52 |
double eigen_check = q2.Norm();
|
|
|
53 |
double err_eigen = (fabs(b[i])-fabs(eigen_check));
|
|
|
54 |
double err_eigen_vec = (q*b[i]-q2).Norm();
|
|
|
55 |
cout << "l(i)-Norm(Q*e(i)) " << err_eigen << endl;
|
|
|
56 |
cout << "Norm(e(i)*l(i)-Q*e(i)) " << err_eigen_vec << endl;
|
|
|
57 |
max_err_eigen = max(err_eigen, max_err_eigen);
|
|
|
58 |
max_err_eigen_vec = max(err_eigen_vec, max_err_eigen_vec);
|
|
|
59 |
}
|
|
|
60 |
cout << "-------" << endl;
|
|
|
61 |
}
|
|
|
62 |
cout << "Maximum eigenvalue error :" << max_err_eigen << endl;
|
|
|
63 |
cout << "Maximum eigenvector error :" << max_err_eigen_vec << endl;
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
}
|