tjacob.cpp

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #define _NMBFMT std::setw(12) <<
00023 
00024 #include <iostream>
00025 #include <cmath>
00026 #include <cassert>
00027 
00028 #include "linalg/lawrap.h"
00029 #include "linalg/matrix.h"
00030 #include "linalg/vector.h"
00031 #include "linalg/jacobirot.h"
00032 
00033 using namespace std;
00034 using namespace LinAlg;
00035 
00036 void FillEigenProjs(Matrix<REAL> const & Q, int N, Matrix<REAL> P[])
00037 {
00038     assert(Q.Rows()==N);
00039     assert(Q.Cols()==N);
00040     for (int k=0; k<N; ++k)
00041         for (int i=0; i<N; ++i)
00042         for (int j=0; j<N; ++j)
00043             P[k](i,j) = Q(i,k) * Q(j,k);
00044 }
00045 
00046 int main()
00047 {
00048     //Matrix<REAL> A(3,3,"2 0 1   0 3 0   1 0 4");
00049     Matrix<REAL> A(3,3,"4 5 6   5 8 7   6 7 9");
00050     Matrix<REAL> Q(3,3);
00051     Vector<REAL> v(3); // Eigenvalues
00052     Matrix<REAL> TMP(3,3);
00053     Matrix<REAL> * P = new Matrix<REAL> [3]; // Eigenprojectors
00054     for (int i=0; i<3; ++i) P[i].Set(3,3);
00055 
00056     cout << "A = \n" << A << std::endl;
00057 
00058     cout << "\n=========================================== JacobiRot Function\n";
00059 
00060     Matrix<REAL> AA(3,3); AA=A;
00061     int itrs = Sym::JacobiRot(A,Q,v);
00062     cout << "Iterations = " << itrs << endl;
00063     cout << "A = \n"        << A    << endl;
00064     cout << "Q = \n"        << Q    << endl;
00065     cout << "v = \n"        << v    << endl;
00066 
00067     FillEigenProjs(Q,3,P);
00068     TMP.SetValues(0.0);
00069     for (int i=0; i<3; ++i)
00070     for (int j=0; j<3; ++j)
00071         TMP(i,j) = TMP(i,j) + v(0)*P[0](i,j)
00072                             + v(1)*P[1](i,j)
00073                             + v(2)*P[2](i,j);
00074     cout << "Sum{eigenvals * eigenprojs} = \n" << TMP << endl;
00075 
00076 #ifdef USE_LAPACK
00077     cout << "\n=========================================== using LAPACK\n";
00078 
00079     A=AA;
00080     Syevr(A,v,Q);
00081     cout << "A = \n" << A << endl;
00082     cout << "v = \n" << v << endl;
00083     cout << "Q = \n" << Q << endl;
00084 
00085     FillEigenProjs(Q,3,P);
00086     TMP.SetValues(0.0);
00087     for (int i=0; i<3; ++i)
00088     for (int j=0; j<3; ++j)
00089         TMP(i,j) = TMP(i,j) + v(0)*P[0](i,j)
00090                             + v(1)*P[1](i,j)
00091                             + v(2)*P[2](i,j);
00092     cout << "Sum{eigenvals * eigenprojs} = \n" << TMP << endl;
00093 #endif
00094 
00095     delete [] P;
00096     return 0;
00097 }

Generated on Wed Jan 24 15:56:26 2007 for MechSys by  doxygen 1.4.7