DenseMatrix/cxx_main.cpp

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

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

Generated on Tue Oct 20 12:45:25 2009 for Teuchos - Trilinos Tools Package by doxygen 1.4.7