|
Teuchos Package Browser (Single Doxygen Collection) Version of the Day
|
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 // This library is free software; you can redistribute it and/or modify 00012 // it under the terms of the GNU Lesser General Public License as 00013 // published by the Free Software Foundation; either version 2.1 of the 00014 // License, or (at your option) any later version. 00015 // 00016 // This library is distributed in the hope that it will be useful, but 00017 // WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 // Lesser General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU Lesser General Public 00022 // License along with this library; if not, write to the Free Software 00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00024 // USA 00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00026 // 00027 // *********************************************************************** 00028 // @HEADER 00029 */ 00030 00031 #include "Teuchos_SerialDenseMatrix.hpp" 00032 #include "Teuchos_SerialDenseVector.hpp" 00033 #include "Teuchos_SerialDenseSolver.hpp" 00034 #include "Teuchos_RCP.hpp" 00035 #include "Teuchos_Version.hpp" 00036 00037 int main(int argc, char* argv[]) 00038 { 00039 std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; 00040 00041 // Creating a double-precision matrix can be done in several ways: 00042 // Create an empty matrix with no dimension 00043 Teuchos::SerialDenseMatrix<int,double> Empty_Matrix; 00044 // Create an empty 3x4 matrix 00045 Teuchos::SerialDenseMatrix<int,double> My_Matrix( 3, 4 ); 00046 // Basic copy of My_Matrix 00047 Teuchos::SerialDenseMatrix<int,double> My_Copy1( My_Matrix ), 00048 // (Deep) Copy of principle 3x3 submatrix of My_Matrix 00049 My_Copy2( Teuchos::Copy, My_Matrix, 3, 3 ), 00050 // (Shallow) Copy of 2x3 submatrix of My_Matrix 00051 My_Copy3( Teuchos::View, My_Matrix, 2, 3, 1, 1 ); 00052 // Create a double-precision vector: 00053 Teuchos::SerialDenseVector<int,double> x(3), y(3); 00054 00055 // The matrix dimensions and strided storage information can be obtained: 00056 int rows, cols, stride; 00057 rows = My_Copy3.numRows(); // number of rows 00058 cols = My_Copy3.numCols(); // number of columns 00059 stride = My_Copy3.stride(); // storage stride 00060 00061 // Matrices can change dimension: 00062 Empty_Matrix.shape( 3, 3 ); // size non-dimensional matrices 00063 My_Matrix.reshape( 3, 3 ); // resize matrices and save values 00064 00065 // Filling matrices with numbers can be done in several ways: 00066 My_Matrix.random(); // random numbers 00067 My_Copy1.putScalar( 1.0 ); // every entry is 1.0 00068 My_Copy2(1,1) = 10.0; // individual element access 00069 Empty_Matrix = My_Matrix; // copy My_Matrix to Empty_Matrix 00070 x = 1.0; // every entry of vector is 1.0 00071 y = 1.0; 00072 00073 // Basic matrix arithmetic can be performed: 00074 double d; 00075 Teuchos::SerialDenseMatrix<int,double> My_Prod( 3, 2 ); 00076 // Matrix multiplication ( My_Prod = 1.0*My_Matrix*My_Copy^T ) 00077 My_Prod.multiply( Teuchos::NO_TRANS, Teuchos::TRANS, 00078 1.0, My_Matrix, My_Copy3, 0.0 ); 00079 My_Copy2 += My_Matrix; // Matrix addition 00080 My_Copy2.scale( 0.5 ); // Matrix scaling 00081 d = x.dot( y ); // Vector dot product 00082 00083 // The pointer to the array of matrix values can be obtained: 00084 double *My_Array=0, *My_Column=0; 00085 My_Array = My_Matrix.values(); // pointer to matrix values 00086 My_Column = My_Matrix[2]; // pointer to third column values 00087 00088 // The norm of a matrix can be computed: 00089 double norm_one, norm_inf, norm_fro; 00090 norm_one = My_Matrix.normOne(); // one norm 00091 norm_inf = My_Matrix.normInf(); // infinity norm 00092 norm_fro = My_Matrix.normFrobenius(); // frobenius norm 00093 00094 // Matrices can be compared: 00095 // Check if the matrices are equal in dimension and values 00096 if (Empty_Matrix == My_Matrix) { 00097 std::cout<< "The matrices are the same!" <<std::endl; 00098 } 00099 // Check if the matrices are different in dimension or values 00100 if (My_Copy2 != My_Matrix) { 00101 std::cout<< "The matrices are different!" <<std::endl; 00102 } 00103 00104 // A matrix can be factored and solved using Teuchos::SerialDenseSolver. 00105 Teuchos::SerialDenseSolver<int,double> My_Solver; 00106 Teuchos::SerialDenseMatrix<int,double> X(3,1), B(3,1); 00107 X.putScalar(1.0); 00108 B.multiply( Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, My_Matrix, X, 0.0 ); 00109 X.putScalar(0.0); // Make sure the computed answer is correct. 00110 00111 int info = 0; 00112 My_Solver.setMatrix( Teuchos::rcp( &My_Matrix, false ) ); 00113 My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) ); 00114 info = My_Solver.factor(); 00115 if (info != 0) 00116 std::cout << "Teuchos::SerialDenseSolver::factor() returned : " << info << std::endl; 00117 info = My_Solver.solve(); 00118 if (info != 0) 00119 std::cout << "Teuchos::SerialDenseSolver::solve() returned : " << info << std::endl; 00120 00121 // A matrix can be sent to the output stream: 00122 std::cout<< std::endl << My_Matrix << std::endl; 00123 std::cout<< X << std::endl; 00124 00125 return 0; 00126 }
1.7.4