DenseMatrix/cxx_main.cpp

This is an example of how to use the Teuchos::SerialDenseMatrix class.

00001 #include "Teuchos_SerialDenseMatrix.hpp"
00002 #include "Teuchos_SerialDenseSolver.hpp"
00003 #include "Teuchos_RCP.hpp"
00004 #include "Teuchos_Version.hpp"
00005 
00006 int main(int argc, char* argv[])
00007 {
00008   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00009 
00010   // Creating a double-precision matrix can be done in several ways:
00011   // Create an empty matrix with no dimension
00012   Teuchos::SerialDenseMatrix<int,double> Empty_Matrix;
00013   // Create an empty 3x4 matrix
00014   Teuchos::SerialDenseMatrix<int,double> My_Matrix( 3, 4 );
00015   // Basic copy of My_Matrix
00016   Teuchos::SerialDenseMatrix<int,double> My_Copy1( My_Matrix ),
00017     // (Deep) Copy of principle 3x3 submatrix of My_Matrix
00018     My_Copy2( Teuchos::Copy, My_Matrix, 3, 3 ),
00019     // (Shallow) Copy of 2x3 submatrix of My_Matrix
00020     My_Copy3( Teuchos::View, My_Matrix, 2, 3, 1, 1 );
00021 
00022   // The matrix dimensions and strided storage information can be obtained:
00023   int rows, cols, stride;
00024   rows = My_Copy3.numRows();  // number of rows
00025   cols = My_Copy3.numCols();  // number of columns
00026   stride = My_Copy3.stride(); // storage stride
00027 
00028   // Matrices can change dimension:
00029   Empty_Matrix.shape( 3, 3 );      // size non-dimensional matrices
00030   My_Matrix.reshape( 3, 3 );       // resize matrices and save values
00031 
00032   // Filling matrices with numbers can be done in several ways:
00033   My_Matrix.random();             // random numbers
00034   My_Copy1.putScalar( 1.0 );      // every entry is 1.0
00035   My_Copy2(1,1) = 10.0;           // individual element access
00036   Empty_Matrix = My_Matrix;       // copy My_Matrix to Empty_Matrix 
00037 
00038   // Basic matrix arithmetic can be performed:
00039   Teuchos::SerialDenseMatrix<int,double> My_Prod( 3, 2 );
00040   // Matrix multiplication ( My_Prod = 1.0*My_Matrix*My_Copy^T )
00041   My_Prod.multiply( Teuchos::NO_TRANS, Teuchos::TRANS, 
00042         1.0, My_Matrix, My_Copy3, 0.0 );
00043   My_Copy2 += My_Matrix;         // Matrix addition
00044   My_Copy2.scale( 0.5 );         // Matrix scaling
00045   
00046   // The pointer to the array of matrix values can be obtained:
00047   double *My_Array=0, *My_Column=0;
00048   My_Array = My_Matrix.values();   // pointer to matrix values
00049   My_Column = My_Matrix[2];        // pointer to third column values
00050 
00051   // The norm of a matrix can be computed:
00052   double norm_one, norm_inf, norm_fro;
00053   norm_one = My_Matrix.normOne();        // one norm
00054   norm_inf = My_Matrix.normInf();        // infinity norm
00055   norm_fro = My_Matrix.normFrobenius();  // frobenius norm
00056 
00057   // Matrices can be compared:
00058   // Check if the matrices are equal in dimension and values
00059   if (Empty_Matrix == My_Matrix) {
00060     std::cout<< "The matrices are the same!" <<std::endl;
00061   }
00062   // Check if the matrices are different in dimension or values
00063   if (My_Copy2 != My_Matrix) {
00064     std::cout<< "The matrices are different!" <<std::endl;
00065   }
00066 
00067   // A matrix can be factored and solved using Teuchos::SerialDenseSolver.
00068   Teuchos::SerialDenseSolver<int,double> My_Solver;
00069   Teuchos::SerialDenseMatrix<int,double> X(3,1), B(3,1);
00070   X.putScalar(1.0);
00071   B.multiply( Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, My_Matrix, X, 0.0 );  
00072   X.putScalar(0.0);  // Make sure the computed answer is correct.
00073 
00074   int info = 0;
00075   My_Solver.setMatrix( Teuchos::rcp( &My_Matrix, false ) );
00076   My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) );
00077   info = My_Solver.factor();
00078   if (info != 0) 
00079     std::cout << "Teuchos::SerialDenseSolver::factor() returned : " << info << std::endl;
00080   info = My_Solver.solve();
00081   if (info != 0) 
00082     std::cout << "Teuchos::SerialDenseSolver::solve() returned : " << info << std::endl;
00083 
00084   // A matrix can be sent to the output stream:
00085   std::cout<< std::endl << My_Matrix << std::endl;
00086   std::cout<< X << std::endl;
00087 
00088   return 0;
00089 }

Generated on Wed May 12 21:40:31 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7