Teuchos Package Browser (Single Doxygen Collection) Version of the Day
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 
00074   // Matrices can change dimension:
00075   Empty_Matrix.shape( 3, 3 );      // size non-dimensional matrices
00076   My_Matrix.reshape( 3, 3 );       // resize matrices and save values
00077 
00078   // Filling matrices with numbers can be done in several ways:
00079   My_Matrix.random();             // random numbers
00080   My_Copy1.putScalar( 1.0 );      // every entry is 1.0 
00081   My_Copy2(1,1) = 10.0;           // individual element access
00082   Empty_Matrix = My_Matrix;       // copy My_Matrix to Empty_Matrix 
00083   x = 1.0;                        // every entry of vector is 1.0
00084   y = 1.0;
00085 
00086   // Basic matrix arithmetic can be performed:
00087   double d;
00088   Teuchos::SerialDenseMatrix<int,double> My_Prod( 3, 2 );
00089   // Matrix multiplication ( My_Prod = 1.0*My_Matrix*My_Copy^T )
00090   My_Prod.multiply( Teuchos::NO_TRANS, Teuchos::TRANS, 
00091         1.0, My_Matrix, My_Copy3, 0.0 );
00092   My_Copy2 += My_Matrix;         // Matrix addition
00093   My_Copy2.scale( 0.5 );         // Matrix scaling
00094   d = x.dot( y );                // Vector dot product  
00095   
00096   // The pointer to the array of matrix values can be obtained:
00097   double *My_Array=0, *My_Column=0;
00098   My_Array = My_Matrix.values();   // pointer to matrix values
00099   My_Column = My_Matrix[2];        // pointer to third column values
00100 
00101   // The norm of a matrix can be computed:
00102   double norm_one, norm_inf, norm_fro;
00103   norm_one = My_Matrix.normOne();        // one norm
00104   norm_inf = My_Matrix.normInf();        // infinity norm
00105   norm_fro = My_Matrix.normFrobenius();  // frobenius norm
00106 
00107   // Matrices can be compared:
00108   // Check if the matrices are equal in dimension and values
00109   if (Empty_Matrix == My_Matrix) {
00110     std::cout<< "The matrices are the same!" <<std::endl;
00111   }
00112   // Check if the matrices are different in dimension or values
00113   if (My_Copy2 != My_Matrix) {
00114     std::cout<< "The matrices are different!" <<std::endl;
00115   }
00116 
00117   // A matrix can be factored and solved using Teuchos::SerialDenseSolver.
00118   Teuchos::SerialDenseSolver<int,double> My_Solver;
00119   Teuchos::SerialDenseMatrix<int,double> X(3,1), B(3,1);
00120   X.putScalar(1.0);
00121   B.multiply( Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, My_Matrix, X, 0.0 );  
00122   X.putScalar(0.0);  // Make sure the computed answer is correct.
00123 
00124   int info = 0;
00125   My_Solver.setMatrix( Teuchos::rcp( &My_Matrix, false ) );
00126   My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) );
00127   info = My_Solver.factor();
00128   if (info != 0) 
00129     std::cout << "Teuchos::SerialDenseSolver::factor() returned : " << info << std::endl;
00130   info = My_Solver.solve();
00131   if (info != 0) 
00132     std::cout << "Teuchos::SerialDenseSolver::solve() returned : " << info << std::endl;
00133 
00134   // A matrix can be sent to the output stream:
00135   std::cout<< std::endl << My_Matrix << std::endl;
00136   std::cout<< X << std::endl;
00137 
00138   return 0;
00139 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines