teigen.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 <iomanip>
00027 
00028 #include "linalg/matrix.h"
00029 #include "linalg/vector.h"
00030 #include "linalg/lawrap.h"
00031 
00032 using namespace std;
00033 using namespace LinAlg;
00034 
00035 int main()
00036 {
00037 
00038     // ================================================================== Eigenproblem
00039 
00040     cout << "\n=========================================== EigenValues/Vectors\n";
00041     
00042     Matrix<REAL> P(5,5,"10 2 3 4 5  2 20 3 4 5  3 3 30 4 5  4 4 4 40 5  5 5 5 5 50");
00043     Vector<REAL> v(5); // Eigenvalues
00044     Matrix<REAL> Q(5,5); // Eigenvectors (columns of Q)
00045 
00046     Matrix<REAL> R(5,5); R = P;
00047     Syevr(R,v,Q);
00048 
00049     cout << "P = \n" << P << endl;
00050     cout << "R = \n" << R << endl;
00051     cout << "v = \n" << v << endl;
00052     cout << "Q = \n" << Q << endl;
00053 
00054     Vector<REAL> * V = new Vector<REAL> [5];
00055     for (int i=0; i<5; ++i) V[i].Set(5);
00056     Vector<REAL> S(5);
00057 
00058     for (int j=0; j<5; ++j)
00059         for (int i=0; i<5; ++i)
00060             V[j](i) = Q(i,j);
00061 
00062     for (int i=0; i<5; ++i)
00063     {
00064         Gemv(1,P,V[i],0,S);
00065         for (int j=0; j<5; ++j)
00066             S(j) = S(j) - v(i)*V[i](j);
00067         cout << "S = P*V" << i << " - v" << i << "*V" << i << "\n";
00068         cout << S << endl;
00069     }
00070 
00071     delete [] V;
00072     
00073     cout << "\n============================================== EigenProjectors\n";
00074 
00075     Matrix<REAL> F(4,4,"5 2 3 1   2 8 1 1   3 1 8 5   1 1 5 10");
00076     Vector<REAL> G(4);    // Eigenvalues
00077     Matrix<REAL> * H = new Matrix<REAL> [4];
00078     for (int i=0; i<4; ++i) H[i].Set(4,4); // Eigenprojectors
00079 
00080     cout << "F = \n" << F << endl;
00081 
00082     Syep(F,G,H);
00083     for (int i=0; i<4; ++i)
00084         cout << "H[" << i << "] =\n" << H[i] << endl;
00085 
00086     Matrix<REAL> I(4,4);
00087     for (int i=0; i<4; ++i)
00088     for (int j=0; j<4; ++j)
00089         for (int k=0; k<4; ++k)
00090             I(i,j) = I(i,j) + G(k)*H[k](i,j);
00091     cout << "I = v0*H0 + v1*H1 + v2*H2 + v3*H3 =\n" << I << endl;
00092     
00093     Matrix<REAL> T(4,4);
00094     for (int i=0; i<4; ++i)
00095     for (int j=0; j<4; ++j)
00096     {
00097         if (i==j)
00098         {
00099             T = H[i];
00100             Gemm(1,H[i],H[j],-1,T);
00101             cout << "H" << i << "*H" << j << " - H" << i << " =\n";
00102             cout << T << endl;
00103         }
00104         else
00105         {
00106             Gemm(1,H[i],H[j],0,T);
00107             cout << "H" << i << "*H" << j << " =\n" << T << endl;
00108         }
00109     }
00110 
00111     delete [] H;
00112     
00113     cout << "\n=========================================== Isotropic Functions\n";
00114 
00115     Matrix<REAL> IF(4,4,"5 2 3 1   2 8 1 1   3 1 8 5   1 1 5 10");
00116 
00117     Syif(F,IF,exp);
00118     cout << "exp(F) = \n" << IF << endl;
00119 
00120     F.Reset("5 2 3 1   2 8 1 1   3 1 8 5   1 1 5 10");
00121     cout << "F = \n" << F << endl;
00122 
00123     Syif(F,IF,sqrt);
00124     cout << "sqrt(F) = \n" << IF << endl;
00125     
00126     // Check
00127     Matrix<REAL> RR(4,4);
00128     Gemm(1,IF,IF,0,RR);
00129     cout << "sqrt(F)*sqrt(F) = \n" << RR << endl;
00130     
00131     cout << "\n=========================================== Isotropic Functions\n";
00132 
00133     int n = 7;
00134     Matrix<REAL> AA(7,7);
00135     for (int i=0; i<n-1; ++i) AA(i+1,i) = -1;
00136     for (int i=0; i<n-1; ++i) AA(i,i+1) = -1;
00137     for (int i=1; i<n-1; ++i) AA(i,i)   = 2;
00138     AA(0,0) = 1;
00139     AA(n-1,n-1) = 1;
00140 
00141     cout << "AA = \n" << AA << endl;
00142     
00143     Matrix<REAL> sqAA(7,7);
00144     Syif(AA,sqAA,sqrt);
00145 
00146     cout << "sqrt(AA) = \n" << sqAA << endl;
00147 
00148     // Check
00149     Matrix<REAL> RRR(7,7);
00150     Gemm(1,sqAA,sqAA,0,RRR);
00151     cout << "sqrt(AA)*sqrt(AA) = \n" << RRR << endl;
00152 
00153     return 0;
00154 }

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