nm_ex2.cpp
Go to the documentation of this file.
1 
16 
17 #define WANT_STREAM // include iostream and iomanipulators
18 
19 #include "newmatap.h" // newmat headers including advanced functions
20 #include "newmatio.h" // newmat headers including output functions
21 
22 #ifdef use_namespace
23 using namespace RBD_LIBRARIES;
24 #endif
25 
26 
27 int my_main() // called by main()
28 {
29  Tracer tr("my_main "); // for tracking exceptions
30 
31  int n = 7; // this is the order we will work with
32  int i, j;
33 
34  // declare a matrix
35  SymmetricMatrix H(n);
36 
37  // load values for Hilbert matrix
38  for (i = 1; i <= n; ++i) for (j = 1; j <= i; ++j)
39  H(i, j) = 1.0 / (i + j - 1);
40 
41  // print the matrix
42  cout << "SymmetricMatrix H" << endl;
43  cout << setw(10) << setprecision(7) << H << endl;
44 
45  // calculate its eigenvalues and eigenvectors and print them
47  eigenvalues(H, D, U);
48  cout << "Eigenvalues of H" << endl;
49  cout << setw(17) << setprecision(14) << D.as_column() << endl;
50  cout << "Eigenvector matrix, U" << endl;
51  cout << setw(10) << setprecision(7) << U << endl;
52 
53  // check orthogonality
54  cout << "U * U.t() (should be near identity)" << endl;
55  cout << setw(10) << setprecision(7) << (U * U.t()) << endl;
56 
57  // check decomposition
58  cout << "U * D * U.t() (should be near H)" << endl;
59  cout << setw(10) << setprecision(7) << (U * D * U.t()) << endl;
60 
61  return 0;
62 }
63 
64 
65 // call my_main() - use this to catch exceptions
66 // use macros for exception names for compatibility with simuated exceptions
67 int main()
68 {
69  Try { return my_main(); }
70  Catch(BaseException) { cout << BaseException::what() << "\n"; }
71  CatchAll { cout << "\nProgram fails - exception generated\n\n"; }
72  return 0;
73 }
74 
75 
#define Try
Definition: myexcept.h:190
#define Catch
Definition: myexcept.h:193
int main()
Definition: nm_ex2.cpp:67
#define CatchAll
Definition: myexcept.h:194
TransposedMatrix t() const
Definition: newmat6.cpp:320
The usual rectangular matrix.
Definition: newmat.h:625
static const char * what()
Definition: myexcept.h:93
void eigenvalues(const SymmetricMatrix &A, DiagonalMatrix &D, Matrix &Z)
Definition: evalue.cpp:287
Diagonal matrix.
Definition: newmat.h:896
int my_main()
Definition: nm_ex2.cpp:27
ColedMatrix as_column() const
Definition: newmat6.cpp:336
Symmetric matrix.
Definition: newmat.h:753


kni
Author(s): Martin Günther
autogenerated on Fri Jun 7 2019 22:06:45