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 
00073   // Matrices can change dimension:
00074   Empty_Matrix.shape( 3 );      // size non-dimensional matrices
00075   My_Matrix.reshape( 3 );       // resize matrices and save values
00076 
00077   // Filling matrices with numbers can be done in several ways:
00078   My_Matrix.random();             // random numbers
00079   My_Copy1.putScalar( 1.0 );      // every entry is 1.0
00080   My_Copy1 = 1.0;                 // every entry is 1.0 (still)
00081   My_Copy2(1,1) = 10.0;           // individual element access
00082   Empty_Matrix = My_Matrix;       // copy My_Matrix to Empty_Matrix 
00083 
00084   // Basic matrix arithmetic can be performed:
00085   Teuchos::SerialDenseMatrix<int,double> My_Prod( 4, 3 ), My_GenMatrix( 4, 3 );
00086   My_GenMatrix = 1.0;
00087   // Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix )
00088   My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, My_Matrix, My_GenMatrix, 0.0 );
00089   My_Copy2 += My_Matrix;   // Matrix addition
00090   My_Copy2 *= 0.5;         // Matrix scaling
00091   
00092   // Matrices can be compared:
00093   // Check if the matrices are equal in dimension and values
00094   if (Empty_Matrix == My_Matrix) {
00095     std::cout<< "The matrices are the same!" <<std::endl;
00096   }
00097   // Check if the matrices are different in dimension or values
00098   if (My_Copy2 != My_Matrix) {
00099     std::cout<< "The matrices are different!" <<std::endl;
00100   }
00101 
00102   // The norm of a matrix can be computed:
00103   double norm_one, norm_inf, norm_fro;
00104   norm_one = My_Matrix.normOne();        // one norm
00105   norm_inf = My_Matrix.normInf();        // infinity norm
00106   norm_fro = My_Matrix.normFrobenius();  // frobenius norm
00107 
00108   std::cout << std::endl << "|| My_Matrix ||_1 = " << norm_one << std::endl;
00109   std::cout << "|| My_Matrix ||_Inf = " << norm_inf << std::endl;
00110   std::cout << "|| My_Matrix ||_F = " << norm_fro << std::endl << std::endl;
00111 
00112   // A matrix can be factored and solved using Teuchos::SerialDenseSolver.
00113   Teuchos::SerialSpdDenseSolver<int,double> My_Solver;
00114   Teuchos::SerialSymDenseMatrix<int,double> My_Matrix2( 3 );
00115   My_Matrix2.random();
00116   Teuchos::SerialDenseMatrix<int,double> X(3,1), B(3,1);
00117   X = 1.0;
00118   B.multiply( Teuchos::LEFT_SIDE, 1.0, My_Matrix2, X, 0.0 );
00119   X = 0.0;  // Make sure the computed answer is correct.
00120 
00121   int info = 0;
00122   My_Solver.setMatrix( Teuchos::rcp( &My_Matrix2, false ) );
00123   My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) );
00124   info = My_Solver.factor();
00125   if (info != 0)
00126     std::cout << "Teuchos::SerialSpdDenseSolver::factor() returned : " << info << std::endl;
00127   info = My_Solver.solve();
00128   if (info != 0)
00129     std::cout << "Teuchos::SerialSpdDenseSolver::solve() returned : " << info << std::endl;
00130 
00131   // A matrix triple-product can be computed:  C = alpha*W'*A*W
00132   double alpha=0.5;
00133   Teuchos::SerialDenseMatrix<int,double> W(3,2);
00134   Teuchos::SerialSymDenseMatrix<int,double> A1(2), A2(3);
00135   A1(0,0) = 1.0, A1(1,1) = 2.0;
00136   A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00;
00137   W = 1.0;
00138 
00139   Teuchos::SerialSymDenseMatrix<int,double> C1(3), C2(2);
00140 
00141   Teuchos::symMatTripleProduct<int,double>( Teuchos::NO_TRANS, alpha, A1, W, C1);
00142   Teuchos::symMatTripleProduct<int,double>( Teuchos::TRANS, alpha, A2, W, C2 );
00143 
00144   // A matrix can be sent to the output stream:
00145   std::cout<< My_Matrix << std::endl;
00146   std::cout<< X << std::endl;
00147 
00148   return 0;
00149 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines