1 | /******************************************************* |
---|
2 | A simple program that demonstrates NewMat10 library. |
---|
3 | The program defines a random symmetric matrix |
---|
4 | and computes its eigendecomposition. |
---|
5 | For further details read the NewMat10 Reference Manual |
---|
6 | ********************************************************/ |
---|
7 | |
---|
8 | |
---|
9 | #define WANT_STREAM |
---|
10 | #define WANT_MATH |
---|
11 | #define WANT_FSTREAM |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | #include <stdlib.h> |
---|
16 | #include <time.h> |
---|
17 | #include <string.h> |
---|
18 | |
---|
19 | // the following two are needed for printing |
---|
20 | #include <iostream.h> |
---|
21 | #include <iomanip.h> |
---|
22 | /************************************** |
---|
23 | The NewMat10 include files */ |
---|
24 | #include "include.h" |
---|
25 | #include "newmat.h" |
---|
26 | #include "newmatap.h" |
---|
27 | #include "newmatio.h" |
---|
28 | /***************************************/ |
---|
29 | |
---|
30 | |
---|
31 | #ifdef use_namespace |
---|
32 | using namespace RBD_LIBRARIES; |
---|
33 | #endif |
---|
34 | |
---|
35 | int main(int argc, char **argv) { |
---|
36 | |
---|
37 | |
---|
38 | SymmetricMatrix C(3); |
---|
39 | |
---|
40 | C(1,1) = 1; |
---|
41 | C(1,2) = 4; |
---|
42 | C(1,3) = 4; |
---|
43 | C(2,1) = 4; |
---|
44 | C(2,2) = 2; |
---|
45 | C(2,3) = 4; |
---|
46 | C(3,1) = 4; |
---|
47 | C(3,2) = 4; |
---|
48 | C(3,3) = 3; |
---|
49 | |
---|
50 | cout << "The symmetrix matrix C" << endl; |
---|
51 | cout << setw(5) << setprecision(0) << C << endl; |
---|
52 | |
---|
53 | Matrix V(3,3); // for eigenvectors |
---|
54 | DiagonalMatrix D(3); // for eigenvalues |
---|
55 | |
---|
56 | // the decomposition |
---|
57 | Jacobi(C, D, V); |
---|
58 | |
---|
59 | |
---|
60 | // Print the result |
---|
61 | cout << "The eigenvalues matrix:" << endl; |
---|
62 | cout << setw(10) << setprecision(5) << D << endl; |
---|
63 | cout << "The eigenvectors matrix:" << endl; |
---|
64 | cout << setw(10) << setprecision(5) << V << endl; |
---|
65 | |
---|
66 | return 0; |
---|
67 | /* |
---|
68 | int M = 3, N = 5; |
---|
69 | Matrix X(M,N); // Define an M x N general matrix |
---|
70 | |
---|
71 | // Fill X by random numbers between 0 and 9 |
---|
72 | // Note that indexing into matrices in NewMat is 1-based! |
---|
73 | srand(time(NULL)); |
---|
74 | for (int i = 1; i <= M; ++i) { |
---|
75 | for (int j = 1; j <= N; ++j) { |
---|
76 | X(i,j) = rand() % 10; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | SymmetricMatrix C; |
---|
81 | C << X * X.t(); // fill in C by X * X^t. |
---|
82 | // Works because we *know* that the result is symmetric |
---|
83 | |
---|
84 | cout << "The symmetrix matrix C" << endl; |
---|
85 | cout << setw(5) << setprecision(0) << C << endl; |
---|
86 | |
---|
87 | |
---|
88 | // compute eigendecomposition of C |
---|
89 | Matrix V(3,3); // for eigenvectors |
---|
90 | DiagonalMatrix D(3); // for eigenvalues |
---|
91 | |
---|
92 | // the decomposition |
---|
93 | Jacobi(C, D, V); |
---|
94 | |
---|
95 | // Print the result |
---|
96 | cout << "The eigenvalues matrix:" << endl; |
---|
97 | cout << setw(10) << setprecision(5) << D << endl; |
---|
98 | cout << "The eigenvectors matrix:" << endl; |
---|
99 | cout << setw(10) << setprecision(5) << V << endl; |
---|
100 | |
---|
101 | // Check that the first eigenvector indeed has the eigenvector property |
---|
102 | ColumnVector v1(3); |
---|
103 | v1(1) = V(1,1); |
---|
104 | v1(2) = V(2,1); |
---|
105 | v1(3) = V(3,1); |
---|
106 | |
---|
107 | ColumnVector Cv1 = C * v1; |
---|
108 | ColumnVector lambda1_v1 = D(1) * v1; |
---|
109 | |
---|
110 | cout << "The max-norm of the difference between C*v1 and lambda1*v1 is " << |
---|
111 | NormInfinity(Cv1 - lambda1_v1) << endl << endl; |
---|
112 | |
---|
113 | // Build the inverse and check the result |
---|
114 | Matrix Ci = C.i(); |
---|
115 | Matrix I = Ci * C; |
---|
116 | |
---|
117 | cout << "The inverse of C is" << endl; |
---|
118 | cout << setw(10) << setprecision(5) << Ci << endl; |
---|
119 | cout << "And the inverse times C is identity" << endl; |
---|
120 | cout << setw(10) << setprecision(5) << I << endl; |
---|
121 | |
---|
122 | // Example for multiple solves (see NewMat documentation) |
---|
123 | ColumnVector r1(3), r2(3); |
---|
124 | for (int i = 1; i <= 3; ++i) { |
---|
125 | r1(i) = rand() % 10; |
---|
126 | r2(i) = rand() % 10; |
---|
127 | } |
---|
128 | LinearEquationSolver CLU = C; // decomposes C |
---|
129 | ColumnVector s1 = CLU.i() * r1; |
---|
130 | ColumnVector s2 = CLU.i() * r2; |
---|
131 | |
---|
132 | cout << "solution for right hand side r1" << endl; |
---|
133 | cout << setw(10) << setprecision(5) << s1 << endl; |
---|
134 | cout << "solution for right hand side r2" << endl; |
---|
135 | cout << setw(10) << setprecision(5) << s2 << endl; |
---|
136 | */ |
---|
137 | |
---|
138 | return 0; |
---|
139 | } |
---|