Teuchos Package Browser (Single Doxygen Collection) Version of the Day
numerics/example/DenseMatrix/cxx_main.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 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "Teuchos_SerialDenseMatrix.hpp"
00045 #include "Teuchos_SerialDenseVector.hpp"
00046 #include "Teuchos_SerialDenseSolver.hpp"
00047 #include "Teuchos_RCP.hpp"
00048 #include "Teuchos_Version.hpp"
00049 
00050 int main(int argc, char* argv[])
00051 {
00052   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00053 
00054   // Creating a double-precision matrix can be done in several ways:
00055   // Create an empty matrix with no dimension
00056   Teuchos::SerialDenseMatrix<int,double> Empty_Matrix;
00057   // Create an empty 3x4 matrix
00058   Teuchos::SerialDenseMatrix<int,double> My_Matrix( 3, 4 );
00059   // Basic copy of My_Matrix
00060   Teuchos::SerialDenseMatrix<int,double> My_Copy1( My_Matrix ),
00061   // (Deep) Copy of principle 3x3 submatrix of My_Matrix
00062   My_Copy2( Teuchos::Copy, My_Matrix, 3, 3 ),
00063   // (Shallow) Copy of 2x3 submatrix of My_Matrix
00064   My_Copy3( Teuchos::View, My_Matrix, 2, 3, 1, 1 );
00065   // Create a double-precision vector:
00066   Teuchos::SerialDenseVector<int,double> x(3), y(3);
00067 
00068   // The matrix dimensions and strided storage information can be obtained:
00069   int rows, cols, stride;
00070   rows = My_Copy3.numRows();  // number of rows
00071   cols = My_Copy3.numCols();  // number of columns
00072   stride = My_Copy3.stride(); // storage stride
00073   TEUCHOS_ASSERT_EQUALITY(rows, 2);
00074   TEUCHOS_ASSERT_EQUALITY(cols, 3);
00075   TEUCHOS_ASSERT_EQUALITY(stride, 3);
00076 
00077   // Matrices can change dimension:
00078   Empty_Matrix.shape( 3, 3 );      // size non-dimensional matrices
00079   My_Matrix.reshape( 3, 3 );       // resize matrices and save values
00080 
00081   // Filling matrices with numbers can be done in several ways:
00082   My_Matrix.random();             // random numbers
00083   My_Copy1.putScalar( 1.0 );      // every entry is 1.0 
00084   My_Copy2(1,1) = 10.0;           // individual element access
00085   Empty_Matrix = My_Matrix;       // copy My_Matrix to Empty_Matrix 
00086   x = 1.0;                        // every entry of vector is 1.0
00087   y = 1.0;
00088 
00089   // Basic matrix arithmetic can be performed:
00090   double d;
00091   Teuchos::SerialDenseMatrix<int,double> My_Prod( 3, 2 );
00092   // Matrix multiplication ( My_Prod = 1.0*My_Matrix*My_Copy^T )
00093   My_Prod.multiply( Teuchos::NO_TRANS, Teuchos::TRANS, 
00094         1.0, My_Matrix, My_Copy3, 0.0 );
00095   My_Copy2 += My_Matrix;         // Matrix addition
00096   My_Copy2.scale( 0.5 );         // Matrix scaling
00097   d = x.dot( y );                // Vector dot product  
00098   (void)d; // Not used! 
00099   
00100   // The pointer to the array of matrix values can be obtained:
00101   double *My_Array=0, *My_Column=0;
00102   My_Array = My_Matrix.values();   // pointer to matrix values
00103   My_Column = My_Matrix[2];        // pointer to third column values
00104   (void)My_Array; // Not used!
00105   (void)My_Column; // Not used!
00106 
00107   // The norm of a matrix can be computed:
00108   double norm_one, norm_inf, norm_fro;
00109   norm_one = My_Matrix.normOne();        // one norm
00110   norm_inf = My_Matrix.normInf();        // infinity norm
00111   norm_fro = My_Matrix.normFrobenius();  // frobenius norm
00112   (void)norm_one; // Not used!
00113   (void)norm_inf; // Not used!
00114   (void)norm_fro; // Not used!
00115 
00116   // Matrices can be compared:
00117   // Check if the matrices are equal in dimension and values
00118   if (Empty_Matrix == My_Matrix) {
00119     std::cout<< "The matrices are the same!" <<std::endl;
00120   }
00121   // Check if the matrices are different in dimension or values
00122   if (My_Copy2 != My_Matrix) {
00123     std::cout<< "The matrices are different!" <<std::endl;
00124   }
00125 
00126   // A matrix can be factored and solved using Teuchos::SerialDenseSolver.
00127   Teuchos::SerialDenseSolver<int,double> My_Solver;
00128   Teuchos::SerialDenseMatrix<int,double> X(3,1), B(3,1);
00129   X.putScalar(1.0);
00130   B.multiply( Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, My_Matrix, X, 0.0 );  
00131   X.putScalar(0.0);  // Make sure the computed answer is correct.
00132 
00133   int info = 0;
00134   My_Solver.setMatrix( Teuchos::rcp( &My_Matrix, false ) );
00135   My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) );
00136   info = My_Solver.factor();
00137   if (info != 0) 
00138     std::cout << "Teuchos::SerialDenseSolver::factor() returned : " << info << std::endl;
00139   info = My_Solver.solve();
00140   if (info != 0) 
00141     std::cout << "Teuchos::SerialDenseSolver::solve() returned : " << info << std::endl;
00142 
00143   // A matrix can be sent to the output stream:
00144   std::cout<< std::endl << My_Matrix << std::endl;
00145   std::cout<< X << std::endl;
00146 
00147   return 0;
00148 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines