nm_ex2.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00016 
00017 #define WANT_STREAM            // include iostream and iomanipulators
00018 
00019 #include "newmatap.h"          // newmat headers including advanced functions
00020 #include "newmatio.h"          // newmat headers including output functions
00021 
00022 #ifdef use_namespace
00023 using namespace RBD_LIBRARIES;
00024 #endif
00025 
00026 
00027 int my_main()                  // called by main()
00028 {
00029    Tracer tr("my_main ");      // for tracking exceptions
00030    
00031    int n = 7;                  // this is the order we will work with
00032    int i, j;
00033 
00034    // declare a matrix
00035    SymmetricMatrix H(n);
00036    
00037    // load values for Hilbert matrix
00038    for (i = 1; i <= n; ++i) for (j = 1; j <= i; ++j)
00039       H(i, j) = 1.0 / (i + j - 1);
00040 
00041    // print the matrix
00042    cout << "SymmetricMatrix H" << endl;
00043    cout << setw(10) << setprecision(7) << H << endl;
00044    
00045    // calculate its eigenvalues and eigenvectors and print them
00046    Matrix U; DiagonalMatrix D;
00047    eigenvalues(H, D, U);
00048    cout << "Eigenvalues of H" << endl;
00049    cout << setw(17) << setprecision(14) << D.as_column() << endl;
00050    cout << "Eigenvector matrix, U" << endl;
00051    cout << setw(10) << setprecision(7) << U << endl;
00052 
00053    // check orthogonality
00054    cout << "U * U.t() (should be near identity)" << endl;   
00055    cout << setw(10) << setprecision(7) << (U * U.t()) << endl;
00056    
00057    // check decomposition
00058    cout << "U * D * U.t() (should be near H)" << endl;   
00059    cout << setw(10) << setprecision(7) << (U * D * U.t()) << endl;
00060    
00061    return 0;
00062 }
00063 
00064 
00065 // call my_main() - use this to catch exceptions
00066 // use macros for exception names for compatibility with simuated exceptions
00067 int main()
00068 {
00069    Try  { return my_main(); }
00070    Catch(BaseException) { cout << BaseException::what() << "\n"; }
00071    CatchAll { cout << "\nProgram fails - exception generated\n\n"; }
00072    return 0;
00073 }
00074 
00075 


kni
Author(s): Martin Günther
autogenerated on Thu Aug 27 2015 13:40:07