example/DenseMatrix/cxx_main.cpp

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