Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/newmat/eigen.cpp @ 4565

Last change on this file since 4565 was 4565, checked in by patrick, 19 years ago

orxonox/trunk: added the newmat library to the project. needs some translation in directory, temp under util/newmat. is needed by the collision detection engine to perform lin alg operations such as eigenvector decomposition. perhaps we will make our own library to do that later.

File size: 2.8 KB
Line 
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
32using namespace RBD_LIBRARIES;
33#endif
34
35int main(int argc, char **argv) {
36
37  int M = 3, N = 5;
38  Matrix X(M,N); // Define an M x N general matrix
39
40  // Fill X by random numbers between 0 and 9
41  // Note that indexing into matrices in NewMat is 1-based!
42  srand(time(NULL));
43  for (int i = 1; i <= M; ++i) {
44    for (int j = 1; j <= N; ++j) {
45      X(i,j) = rand() % 10;
46    }
47  }
48
49  SymmetricMatrix C;
50  C << X * X.t(); // fill in C by X * X^t.
51  // Works because we *know* that the result is symmetric
52
53  cout << "The symmetrix matrix C" << endl;
54  cout << setw(5) << setprecision(0) << C << endl;
55       
56
57  // compute eigendecomposition of C
58  Matrix                        V(3,3); // for eigenvectors
59  DiagonalMatrix        D(3);   // for eigenvalues
60
61  // the decomposition
62  Jacobi(C, D, V);
63       
64  // Print the result
65  cout << "The eigenvalues matrix:" << endl;
66  cout << setw(10) << setprecision(5) << D << endl;
67  cout << "The eigenvectors matrix:" << endl;
68  cout << setw(10) << setprecision(5) << V << endl;
69
70  // Check that the first eigenvector indeed has the eigenvector property
71  ColumnVector v1(3);
72  v1(1) = V(1,1);
73  v1(2) = V(2,1);
74  v1(3) = V(3,1);
75
76  ColumnVector Cv1 = C * v1;
77  ColumnVector lambda1_v1 = D(1) * v1;
78
79  cout << "The max-norm of the difference between C*v1 and lambda1*v1 is " <<
80    NormInfinity(Cv1 - lambda1_v1) << endl << endl;
81
82  // Build the inverse and check the result
83  Matrix Ci = C.i();
84  Matrix I  = Ci * C;
85
86  cout << "The inverse of C is" << endl;
87  cout << setw(10) << setprecision(5) << Ci << endl;
88  cout << "And the inverse times C is identity" << endl;
89  cout << setw(10) << setprecision(5) << I << endl;
90
91  // Example for multiple solves (see NewMat documentation)
92  ColumnVector r1(3), r2(3);
93  for (int i = 1; i <= 3; ++i) {
94    r1(i) = rand() % 10;
95    r2(i) = rand() % 10;
96  }
97  LinearEquationSolver CLU = C; // decomposes C
98  ColumnVector s1 = CLU.i() * r1;
99  ColumnVector s2 = CLU.i() * r2;
100
101  cout << "solution for right hand side r1" << endl;
102  cout << setw(10) << setprecision(5) << s1 << endl;
103  cout << "solution for right hand side r2" << endl;
104  cout << setw(10) << setprecision(5) << s2 << endl;
105       
106  return 0;
107}
Note: See TracBrowser for help on using the repository browser.