Teuchos Package Browser (Single Doxygen Collection) Version of the Day
example/DenseMatrix/cxx_main_sym.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 //
00005 //                    Teuchos: Common Tools Package
00006 //                 Copyright (2004) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00026 //
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 #include "Teuchos_SerialSymDenseMatrix.hpp"
00032 #include "Teuchos_SerialDenseMatrix.hpp"
00033 #include "Teuchos_SerialSpdDenseSolver.hpp"
00034 #include "Teuchos_SerialDenseHelpers.hpp"
00035 #include "Teuchos_RCP.hpp"
00036 #include "Teuchos_Version.hpp"
00037 
00038 int main(int argc, char* argv[])
00039 {
00040   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00041 
00042   // Creating a double-precision matrix can be done in several ways:
00043   // Create an empty matrix with no dimension
00044   Teuchos::SerialSymDenseMatrix<int,double> Empty_Matrix;
00045   // Create an empty 4x4 matrix
00046   Teuchos::SerialSymDenseMatrix<int,double> My_Matrix( 4 );
00047   // Basic copy of My_Matrix
00048   Teuchos::SerialSymDenseMatrix<int,double> My_Copy1( My_Matrix ),
00049     // (Deep) Copy of principle 3x3 submatrix of My_Matrix
00050     My_Copy2( Teuchos::Copy, My_Matrix, 3 ),
00051     // (Shallow) Copy of 3x3 submatrix of My_Matrix
00052     My_Copy3( Teuchos::View, My_Matrix, 3, 1 );
00053 
00054   // The matrix dimensions and strided storage information can be obtained:
00055   int rows, cols, stride;
00056   rows = My_Copy3.numRows();  // number of rows
00057   cols = My_Copy3.numCols();  // number of columns
00058   stride = My_Copy3.stride(); // storage stride
00059 
00060   // Matrices can change dimension:
00061   Empty_Matrix.shape( 3 );      // size non-dimensional matrices
00062   My_Matrix.reshape( 3 );       // resize matrices and save values
00063 
00064   // Filling matrices with numbers can be done in several ways:
00065   My_Matrix.random();             // random numbers
00066   My_Copy1.putScalar( 1.0 );      // every entry is 1.0
00067   My_Copy1 = 1.0;                 // every entry is 1.0 (still)
00068   My_Copy2(1,1) = 10.0;           // individual element access
00069   Empty_Matrix = My_Matrix;       // copy My_Matrix to Empty_Matrix 
00070 
00071   // Basic matrix arithmetic can be performed:
00072   Teuchos::SerialDenseMatrix<int,double> My_Prod( 4, 3 ), My_GenMatrix( 4, 3 );
00073   My_GenMatrix = 1.0;
00074   // Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix )
00075   My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, My_Matrix, My_GenMatrix, 0.0 );
00076   My_Copy2 += My_Matrix;   // Matrix addition
00077   My_Copy2 *= 0.5;         // Matrix scaling
00078   
00079   // Matrices can be compared:
00080   // Check if the matrices are equal in dimension and values
00081   if (Empty_Matrix == My_Matrix) {
00082     std::cout<< "The matrices are the same!" <<std::endl;
00083   }
00084   // Check if the matrices are different in dimension or values
00085   if (My_Copy2 != My_Matrix) {
00086     std::cout<< "The matrices are different!" <<std::endl;
00087   }
00088 
00089   // The norm of a matrix can be computed:
00090   double norm_one, norm_inf, norm_fro;
00091   norm_one = My_Matrix.normOne();        // one norm
00092   norm_inf = My_Matrix.normInf();        // infinity norm
00093   norm_fro = My_Matrix.normFrobenius();  // frobenius norm
00094 
00095   std::cout << std::endl << "|| My_Matrix ||_1 = " << norm_one << std::endl;
00096   std::cout << "|| My_Matrix ||_Inf = " << norm_inf << std::endl;
00097   std::cout << "|| My_Matrix ||_F = " << norm_fro << std::endl << std::endl;
00098 
00099   // A matrix can be factored and solved using Teuchos::SerialDenseSolver.
00100   Teuchos::SerialSpdDenseSolver<int,double> My_Solver;
00101   Teuchos::SerialSymDenseMatrix<int,double> My_Matrix2( 3 );
00102   My_Matrix2.random();
00103   Teuchos::SerialDenseMatrix<int,double> X(3,1), B(3,1);
00104   X = 1.0;
00105   B.multiply( Teuchos::LEFT_SIDE, 1.0, My_Matrix2, X, 0.0 );
00106   X = 0.0;  // Make sure the computed answer is correct.
00107 
00108   int info = 0;
00109   My_Solver.setMatrix( Teuchos::rcp( &My_Matrix2, false ) );
00110   My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) );
00111   info = My_Solver.factor();
00112   if (info != 0)
00113     std::cout << "Teuchos::SerialSpdDenseSolver::factor() returned : " << info << std::endl;
00114   info = My_Solver.solve();
00115   if (info != 0)
00116     std::cout << "Teuchos::SerialSpdDenseSolver::solve() returned : " << info << std::endl;
00117 
00118   // A matrix triple-product can be computed:  C = alpha*W'*A*W
00119   double alpha=0.5;
00120   Teuchos::SerialDenseMatrix<int,double> W(3,2);
00121   Teuchos::SerialSymDenseMatrix<int,double> A1(2), A2(3);
00122   A1(0,0) = 1.0, A1(1,1) = 2.0;
00123   A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00;
00124   W = 1.0;
00125 
00126   Teuchos::SerialSymDenseMatrix<int,double> C1(3), C2(2);
00127 
00128   Teuchos::symMatTripleProduct<int,double>( Teuchos::NO_TRANS, alpha, A1, W, C1);
00129   Teuchos::symMatTripleProduct<int,double>( Teuchos::TRANS, alpha, A2, W, C2 );
00130 
00131   // A matrix can be sent to the output stream:
00132   std::cout<< My_Matrix << std::endl;
00133   std::cout<< X << std::endl;
00134 
00135   return 0;
00136 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines