example/DenseMatrix/cxx_main_sym.cpp

Go to the documentation of this file.
00001 #include "Teuchos_SerialSymDenseMatrix.hpp"
00002 #include "Teuchos_SerialDenseMatrix.hpp"
00003 #include "Teuchos_SerialSpdDenseSolver.hpp"
00004 #include "Teuchos_SerialDenseHelpers.hpp"
00005 #include "Teuchos_RCP.hpp"
00006 #include "Teuchos_Version.hpp"
00007 
00008 int main(int argc, char* argv[])
00009 {
00010   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00011 
00012   // Creating a double-precision matrix can be done in several ways:
00013   // Create an empty matrix with no dimension
00014   Teuchos::SerialSymDenseMatrix<int,double> Empty_Matrix;
00015   // Create an empty 4x4 matrix
00016   Teuchos::SerialSymDenseMatrix<int,double> My_Matrix( 4 );
00017   // Basic copy of My_Matrix
00018   Teuchos::SerialSymDenseMatrix<int,double> My_Copy1( My_Matrix ),
00019     // (Deep) Copy of principle 3x3 submatrix of My_Matrix
00020     My_Copy2( Teuchos::Copy, My_Matrix, 3 ),
00021     // (Shallow) Copy of 3x3 submatrix of My_Matrix
00022     My_Copy3( Teuchos::View, My_Matrix, 3, 1 );
00023 
00024   // The matrix dimensions and strided storage information can be obtained:
00025   int rows, cols, stride;
00026   rows = My_Copy3.numRows();  // number of rows
00027   cols = My_Copy3.numCols();  // number of columns
00028   stride = My_Copy3.stride(); // storage stride
00029 
00030   // Matrices can change dimension:
00031   Empty_Matrix.shape( 3 );      // size non-dimensional matrices
00032   My_Matrix.reshape( 3 );       // resize matrices and save values
00033 
00034   // Filling matrices with numbers can be done in several ways:
00035   My_Matrix.random();             // random numbers
00036   My_Copy1.putScalar( 1.0 );      // every entry is 1.0
00037   My_Copy1 = 1.0;                 // every entry is 1.0 (still)
00038   My_Copy2(1,1) = 10.0;           // individual element access
00039   Empty_Matrix = My_Matrix;       // copy My_Matrix to Empty_Matrix 
00040 
00041   // Basic matrix arithmetic can be performed:
00042   Teuchos::SerialDenseMatrix<int,double> My_Prod( 4, 3 ), My_GenMatrix( 4, 3 );
00043   My_GenMatrix = 1.0;
00044   // Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix )
00045   My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, My_Matrix, My_GenMatrix, 0.0 );
00046   My_Copy2 += My_Matrix;   // Matrix addition
00047   My_Copy2 *= 0.5;         // Matrix scaling
00048   
00049   // Matrices can be compared:
00050   // Check if the matrices are equal in dimension and values
00051   if (Empty_Matrix == My_Matrix) {
00052     std::cout<< "The matrices are the same!" <<std::endl;
00053   }
00054   // Check if the matrices are different in dimension or values
00055   if (My_Copy2 != My_Matrix) {
00056     std::cout<< "The matrices are different!" <<std::endl;
00057   }
00058 
00059   // The norm of a matrix can be computed:
00060   double norm_one, norm_inf, norm_fro;
00061   norm_one = My_Matrix.normOne();        // one norm
00062   norm_inf = My_Matrix.normInf();        // infinity norm
00063   norm_fro = My_Matrix.normFrobenius();  // frobenius norm
00064 
00065   std::cout << std::endl << "|| My_Matrix ||_1 = " << norm_one << std::endl;
00066   std::cout << "|| My_Matrix ||_Inf = " << norm_inf << std::endl;
00067   std::cout << "|| My_Matrix ||_F = " << norm_fro << std::endl << std::endl;
00068 
00069   // A matrix can be factored and solved using Teuchos::SerialDenseSolver.
00070   Teuchos::SerialSpdDenseSolver<int,double> My_Solver;
00071   Teuchos::SerialSymDenseMatrix<int,double> My_Matrix2( 3 );
00072   My_Matrix2.random();
00073   Teuchos::SerialDenseMatrix<int,double> X(3,1), B(3,1);
00074   X = 1.0;
00075   B.multiply( Teuchos::LEFT_SIDE, 1.0, My_Matrix2, X, 0.0 );
00076   X = 0.0;  // Make sure the computed answer is correct.
00077 
00078   int info = 0;
00079   My_Solver.setMatrix( Teuchos::rcp( &My_Matrix2, false ) );
00080   My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) );
00081   info = My_Solver.factor();
00082   if (info != 0)
00083     std::cout << "Teuchos::SerialSpdDenseSolver::factor() returned : " << info << std::endl;
00084   info = My_Solver.solve();
00085   if (info != 0)
00086     std::cout << "Teuchos::SerialSpdDenseSolver::solve() returned : " << info << std::endl;
00087 
00088   // A matrix triple-product can be computed:  C = alpha*W'*A*W
00089   double alpha=0.5;
00090   Teuchos::SerialDenseMatrix<int,double> W(3,2);
00091   Teuchos::SerialSymDenseMatrix<int,double> A1(2), A2(3);
00092   A1(0,0) = 1.0, A1(1,1) = 2.0;
00093   A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00;
00094   W = 1.0;
00095 
00096   Teuchos::SerialSymDenseMatrix<int,double> C1(3), C2(2);
00097 
00098   Teuchos::symMatTripleProduct<int,double>( Teuchos::NO_TRANS, alpha, A1, W, C1);
00099   Teuchos::symMatTripleProduct<int,double>( Teuchos::TRANS, alpha, A2, W, C2 );
00100 
00101   // A matrix can be sent to the output stream:
00102   std::cout<< My_Matrix << std::endl;
00103   std::cout<< X << std::endl;
00104 
00105   return 0;
00106 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:13:59 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1