tmv.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/lawrap.h"
00029 #include "linalg/matrix.h"
00030 #include "linalg/vector.h"
00031 #include "util/exception.h"
00032 
00033 using namespace std;
00034 using namespace LinAlg;
00035 
00036 int main() try
00037 {
00038     // =============================================================== Matrix x Vector
00039 
00040     cout << "\n============================================== Matrix x Vector\n";
00041     
00042     Matrix<REAL> A(5,5,"3 1 3 1 8   1 5 9 -2 4   2 6 5 0 -7   1 2 2 3 1   1 2 3 1 -1");
00043     Vector<REAL> X(5,"-1 -1  1 0 2");
00044     Vector<REAL> Y(5); Y.SetValues(1);
00045 
00046     cout << "A = \n" << A << endl;
00047     cout << "X = \n" << X << endl;
00048     cout << "Y = \n" << Y << endl;
00049     
00050     double a =  1.1;
00051     double b = -8.0;
00052     Gemv(a,A,X,b,Y);
00053     cout << "Y := " << a << "*A*X + " << b << "*Y = \n" << Y << endl;
00054     
00055     // =============================================================== Matrix x Matrix
00056     
00057     cout << "\n============================================== Matrix x Matrix\n";
00058 
00059     Matrix<REAL> B(5,5,"4 1 2 1 1   1 5 9 0 3   2 4 2 -1 2   0 0 1 -1 1   2 0 0 -1 1");
00060     Matrix<REAL> C(5,5,"0 1 -1 0 1   1 2 3 1 1  -1 0 1 1 1   2 -1 0 1 1  -2 -1 -1 -1 0");
00061     Matrix<REAL> E(5,5); E.SetValues(1);
00062 
00063     cout << "B = \n" << B << endl;
00064     cout << "C = \n" << C << endl;
00065     cout << "E = \n" << E << endl;
00066 
00067     a = -2.0;
00068     b =  3.0;
00069     Gemm(a,B,C,b,E);
00070     cout << "E := " << a << "*B*C + " << b << "*E = \n" << E << endl;
00071 
00072     // =========================================================== Matrix^(T) x Matrix
00073     
00074     cout << "\n========================================== Matrix^(T) x Matrix\n";
00075 
00076     E.SetValues(1);
00077 
00078     Gemtm(a,B,C,b,E);
00079     cout << "E := " << a << "*(B^T)*C + " << b << "*E = \n" << E << endl;
00080 
00081     // =========================================================== Matrix x Matrix^(T)
00082     
00083     cout << "\n========================================== Matrix x Matrix^(T)\n";
00084 
00085     E.SetValues(1);
00086 
00087     Gemmt(a,B,C,b,E);
00088     cout << "E := " << a << "*B*(C^T) + " << b << "*E = \n" << E << endl;
00089 
00090     // ======================================================= Matrix^(T) x Matrix^(T)
00091     
00092     cout << "\n====================================== Matrix^(T) x Matrix^(T)\n";
00093 
00094     E.SetValues(1);
00095 
00096     Gemtmt(a,B,C,b,E);
00097     cout << "E := " << a << "*(B^T)*(C^T) + " << b << "*E = \n" << E << endl;
00098 
00099     // ======================================================= Vector x Vector^(T)
00100     
00101     cout << "\n========================================== Vector x Vector^(T)\n";
00102 
00103     Matrix<REAL> F(5,9); F.SetValues(0);
00104     Vector<REAL> Z(9,"4 3 2 1 0 -1 2 3 4");
00105     X.Reset("0 1 2 3 4");
00106 
00107     cout << "F = \n" << F << endl;
00108     cout << "X = \n" << X << endl;
00109     cout << "Z = \n" << Z << endl;
00110     
00111     Ger(1,X,Z,F);
00112     cout << "F := F + " << 1 << "*X*Z^T = \n" << F << endl;
00113 
00114     return 0;
00115 }
00116 catch (Exception * e) //{{{ 
00117 {
00118     e->Cout();
00119     if (e->IsFatal()) {delete e; exit(1);}
00120     delete e;
00121 }
00122 catch (char const * m)
00123 {
00124     std::cout << "Fatal: " << m << std::endl;
00125     exit (1);
00126 }
00127 catch (...)
00128 {
00129     std::cout << "Some exception (...) ocurred\n";
00130 } //}}} 
00131 
00132 // vim:fdm=marker

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