Teuchos Package Browser (Single Doxygen Collection) Version of the Day
example/DenseMatrix/cxx_main_sym.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_SerialSymDenseMatrix.hpp"
00045 #include "Teuchos_SerialDenseMatrix.hpp"
00046 #include "Teuchos_SerialSpdDenseSolver.hpp"
00047 #include "Teuchos_SerialDenseHelpers.hpp"
00048 #include "Teuchos_RCP.hpp"
00049 #include "Teuchos_Version.hpp"
00050 
00051 int main(int argc, char* argv[])
00052 {
00053   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00054 
00055   // Creating a double-precision matrix can be done in several ways:
00056   // Create an empty matrix with no dimension
00057   Teuchos::SerialSymDenseMatrix<int,double> Empty_Matrix;
00058   // Create an empty 4x4 matrix
00059   Teuchos::SerialSymDenseMatrix<int,double> My_Matrix( 4 );
00060   // Basic copy of My_Matrix
00061   Teuchos::SerialSymDenseMatrix<int,double> My_Copy1( My_Matrix ),
00062     // (Deep) Copy of principle 3x3 submatrix of My_Matrix
00063     My_Copy2( Teuchos::Copy, My_Matrix, 3 ),
00064     // (Shallow) Copy of 3x3 submatrix of My_Matrix
00065     My_Copy3( Teuchos::View, My_Matrix, 3, 1 );
00066 
00067   // The matrix dimensions and strided storage information can be obtained:
00068   int rows, cols, stride;
00069   rows = My_Copy3.numRows();  // number of rows
00070   cols = My_Copy3.numCols();  // number of columns
00071   stride = My_Copy3.stride(); // storage stride
00072   TEUCHOS_ASSERT_EQUALITY(rows, 3);
00073   TEUCHOS_ASSERT_EQUALITY(cols, 3);
00074   TEUCHOS_ASSERT_EQUALITY(stride, 4);
00075 
00076   // Matrices can change dimension:
00077   Empty_Matrix.shape( 3 );      // size non-dimensional matrices
00078   My_Matrix.reshape( 3 );       // resize matrices and save values
00079 
00080   // Filling matrices with numbers can be done in several ways:
00081   My_Matrix.random();             // random numbers
00082   My_Copy1.putScalar( 1.0 );      // every entry is 1.0
00083   My_Copy1 = 1.0;                 // every entry is 1.0 (still)
00084   My_Copy2(1,1) = 10.0;           // individual element access
00085   Empty_Matrix = My_Matrix;       // copy My_Matrix to Empty_Matrix 
00086 
00087   // Basic matrix arithmetic can be performed:
00088   Teuchos::SerialDenseMatrix<int,double> My_Prod( 4, 3 ), My_GenMatrix( 4, 3 );
00089   My_GenMatrix = 1.0;
00090   // Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix )
00091   My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, My_Matrix, My_GenMatrix, 0.0 );
00092   My_Copy2 += My_Matrix;   // Matrix addition
00093   My_Copy2 *= 0.5;         // Matrix scaling
00094   
00095   // Matrices can be compared:
00096   // Check if the matrices are equal in dimension and values
00097   if (Empty_Matrix == My_Matrix) {
00098     std::cout<< "The matrices are the same!" <<std::endl;
00099   }
00100   // Check if the matrices are different in dimension or values
00101   if (My_Copy2 != My_Matrix) {
00102     std::cout<< "The matrices are different!" <<std::endl;
00103   }
00104 
00105   // The norm of a matrix can be computed:
00106   double norm_one, norm_inf, norm_fro;
00107   norm_one = My_Matrix.normOne();        // one norm
00108   norm_inf = My_Matrix.normInf();        // infinity norm
00109   norm_fro = My_Matrix.normFrobenius();  // frobenius norm
00110 
00111   std::cout << std::endl << "|| My_Matrix ||_1 = " << norm_one << std::endl;
00112   std::cout << "|| My_Matrix ||_Inf = " << norm_inf << std::endl;
00113   std::cout << "|| My_Matrix ||_F = " << norm_fro << std::endl << std::endl;
00114 
00115   // A matrix can be factored and solved using Teuchos::SerialDenseSolver.
00116   Teuchos::SerialSpdDenseSolver<int,double> My_Solver;
00117   Teuchos::SerialSymDenseMatrix<int,double> My_Matrix2( 3 );
00118   My_Matrix2.random();
00119   Teuchos::SerialDenseMatrix<int,double> X(3,1), B(3,1);
00120   X = 1.0;
00121   B.multiply( Teuchos::LEFT_SIDE, 1.0, My_Matrix2, X, 0.0 );
00122   X = 0.0;  // Make sure the computed answer is correct.
00123 
00124   int info = 0;
00125   My_Solver.setMatrix( Teuchos::rcp( &My_Matrix2, 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::SerialSpdDenseSolver::factor() returned : " << info << std::endl;
00130   info = My_Solver.solve();
00131   if (info != 0)
00132     std::cout << "Teuchos::SerialSpdDenseSolver::solve() returned : " << info << std::endl;
00133 
00134   // A matrix triple-product can be computed:  C = alpha*W'*A*W
00135   double alpha=0.5;
00136   Teuchos::SerialDenseMatrix<int,double> W(3,2);
00137   Teuchos::SerialSymDenseMatrix<int,double> A1(2), A2(3);
00138   A1(0,0) = 1.0, A1(1,1) = 2.0;
00139   A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00;
00140   W = 1.0;
00141 
00142   Teuchos::SerialSymDenseMatrix<int,double> C1(3), C2(2);
00143 
00144   Teuchos::symMatTripleProduct<int,double>( Teuchos::NO_TRANS, alpha, A1, W, C1);
00145   Teuchos::symMatTripleProduct<int,double>( Teuchos::TRANS, alpha, A2, W, C2 );
00146 
00147   // A matrix can be sent to the output stream:
00148   std::cout<< My_Matrix << std::endl;
00149   std::cout<< X << std::endl;
00150 
00151   return 0;
00152 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines