Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
example/DenseMatrix/cxx_main_sym.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
48 #include "Teuchos_RCP.hpp"
49 #include "Teuchos_Version.hpp"
50 
51 int main(int argc, char* argv[])
52 {
53  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
54 
55  // Creating a double-precision matrix can be done in several ways:
56  // Create an empty matrix with no dimension
58  // Create an empty 4x4 matrix
60  // Basic copy of My_Matrix
61  Teuchos::SerialSymDenseMatrix<int,double> My_Copy1( My_Matrix ),
62  // (Deep) Copy of principle 3x3 submatrix of My_Matrix
63  My_Copy2( Teuchos::Copy, My_Matrix, 3 ),
64  // (Shallow) Copy of 3x3 submatrix of My_Matrix
65  My_Copy3( Teuchos::View, My_Matrix, 3, 1 );
66 
67  // The matrix dimensions and strided storage information can be obtained:
68  int rows, cols, stride;
69  rows = My_Copy3.numRows(); // number of rows
70  cols = My_Copy3.numCols(); // number of columns
71  stride = My_Copy3.stride(); // storage stride
72  TEUCHOS_ASSERT_EQUALITY(rows, 3);
73  TEUCHOS_ASSERT_EQUALITY(cols, 3);
74  TEUCHOS_ASSERT_EQUALITY(stride, 4);
75 
76  // Matrices can change dimension:
77  Empty_Matrix.shape( 3 ); // size non-dimensional matrices
78  My_Matrix.reshape( 3 ); // resize matrices and save values
79 
80  // Filling matrices with numbers can be done in several ways:
81  My_Matrix.random(); // random numbers
82  My_Copy1.putScalar( 1.0 ); // every entry is 1.0
83  My_Copy1 = 1.0; // every entry is 1.0 (still)
84  My_Copy2(1,1) = 10.0; // individual element access
85  Empty_Matrix = My_Matrix; // copy My_Matrix to Empty_Matrix
86 
87  // Basic matrix arithmetic can be performed:
88  Teuchos::SerialDenseMatrix<int,double> My_Prod( 4, 3 ), My_GenMatrix( 4, 3 );
89  My_GenMatrix = 1.0;
90  // Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix )
91  My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, My_Matrix, My_GenMatrix, 0.0 );
92  My_Copy2 += My_Matrix; // Matrix addition
93  My_Copy2 *= 0.5; // Matrix scaling
94 
95  // Matrices can be compared:
96  // Check if the matrices are equal in dimension and values
97  if (Empty_Matrix == My_Matrix) {
98  std::cout<< "The matrices are the same!" <<std::endl;
99  }
100  // Check if the matrices are different in dimension or values
101  if (My_Copy2 != My_Matrix) {
102  std::cout<< "The matrices are different!" <<std::endl;
103  }
104 
105  // The norm of a matrix can be computed:
106  double norm_one, norm_inf, norm_fro;
107  norm_one = My_Matrix.normOne(); // one norm
108  norm_inf = My_Matrix.normInf(); // infinity norm
109  norm_fro = My_Matrix.normFrobenius(); // frobenius norm
110 
111  std::cout << std::endl << "|| My_Matrix ||_1 = " << norm_one << std::endl;
112  std::cout << "|| My_Matrix ||_Inf = " << norm_inf << std::endl;
113  std::cout << "|| My_Matrix ||_F = " << norm_fro << std::endl << std::endl;
114 
115  // A matrix can be factored and solved using Teuchos::SerialDenseSolver.
118  My_Matrix2.random();
120  X = 1.0;
121  B.multiply( Teuchos::LEFT_SIDE, 1.0, My_Matrix2, X, 0.0 );
122  X = 0.0; // Make sure the computed answer is correct.
123 
124  int info = 0;
125  My_Solver.setMatrix( Teuchos::rcp( &My_Matrix2, false ) );
126  My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) );
127  info = My_Solver.factor();
128  if (info != 0)
129  std::cout << "Teuchos::SerialSpdDenseSolver::factor() returned : " << info << std::endl;
130  info = My_Solver.solve();
131  if (info != 0)
132  std::cout << "Teuchos::SerialSpdDenseSolver::solve() returned : " << info << std::endl;
133 
134  // A matrix triple-product can be computed: C = alpha*W'*A*W
135  double alpha=0.5;
138  A1(0,0) = 1.0, A1(1,1) = 2.0;
139  A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00;
140  W = 1.0;
141 
143 
144  Teuchos::symMatTripleProduct<int,double>( Teuchos::NO_TRANS, alpha, A1, W, C1);
145  Teuchos::symMatTripleProduct<int,double>( Teuchos::TRANS, alpha, A2, W, C2 );
146 
147  // A matrix can be sent to the output stream:
148  std::cout<< My_Matrix << std::endl;
149  std::cout<< X << std::endl;
150 
151  return 0;
152 }
A class for constructing and using Hermitian positive definite dense matrices.
Non-member helper functions on the templated serial, dense matrix/vector classes. ...
Templated serial dense matrix class.
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
Multiply A * B and add them to this; this = beta * this + alpha*A*B.
int shape(OrdinalType numRowsCols)
Set dimensions of a Teuchos::SerialSymDenseMatrix object; init values to zero.
int factor()
Computes the in-place Cholesky factorization of the matrix using the LAPACK routine DPOTRF...
This class creates and provides basic support for symmetric, positive-definite dense matrices of temp...
OrdinalType numRows() const
Returns the row dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
int reshape(OrdinalType numRowsCols)
Reshape a Teuchos::SerialSymDenseMatrix object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
int setMatrix(const RCP< SerialSymDenseMatrix< OrdinalType, ScalarType > > &A_in)
Sets the pointers for coefficient matrix.
int solve()
Computes the solution X to AX = B for the this matrix and the B provided to SetVectors()..
int setVectors(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &X, const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &B)
Sets the pointers for left and right hand side vector(s).
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
int random(const ScalarType bias=0.1 *Teuchos::ScalarTraits< ScalarType >::one())
Set all values in the active area (upper/lower triangle) of this matrix to be random numbers...
std::string Teuchos_Version()
Templated serial, dense, symmetric matrix class.
int main(int argc, char *argv[])
Templated class for constructing and using Hermitian positive definite dense matrices.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
OrdinalType numCols() const
Returns the column dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
Reference-counted pointer class and non-member templated function implementations.
This class creates and provides basic support for dense rectangular matrix of templated type...