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 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 #include "Teuchos_RCP.hpp"
15 #include "Teuchos_Version.hpp"
16 
17 int main(int argc, char* argv[])
18 {
19  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
20 
21  // Creating a double-precision matrix can be done in several ways:
22  // Create an empty matrix with no dimension
24  // Create an empty 4x4 matrix
26  // Basic copy of My_Matrix
27  Teuchos::SerialSymDenseMatrix<int,double> My_Copy1( My_Matrix ),
28  // (Deep) Copy of principle 3x3 submatrix of My_Matrix
29  My_Copy2( Teuchos::Copy, My_Matrix, 3 ),
30  // (Shallow) Copy of 3x3 submatrix of My_Matrix
31  My_Copy3( Teuchos::View, My_Matrix, 3, 1 );
32 
33  // The matrix dimensions and strided storage information can be obtained:
34  int rows, cols, stride;
35  rows = My_Copy3.numRows(); // number of rows
36  cols = My_Copy3.numCols(); // number of columns
37  stride = My_Copy3.stride(); // storage stride
38  TEUCHOS_ASSERT_EQUALITY(rows, 3);
39  TEUCHOS_ASSERT_EQUALITY(cols, 3);
40  TEUCHOS_ASSERT_EQUALITY(stride, 4);
41 
42  // Matrices can change dimension:
43  Empty_Matrix.shape( 3 ); // size non-dimensional matrices
44  My_Matrix.reshape( 3 ); // resize matrices and save values
45 
46  // Filling matrices with numbers can be done in several ways:
47  My_Matrix.random(); // random numbers
48  My_Copy1.putScalar( 1.0 ); // every entry is 1.0
49  My_Copy1 = 1.0; // every entry is 1.0 (still)
50  My_Copy2(1,1) = 10.0; // individual element access
51  Empty_Matrix = My_Matrix; // copy My_Matrix to Empty_Matrix
52 
53  // Basic matrix arithmetic can be performed:
54  Teuchos::SerialDenseMatrix<int,double> My_Prod( 4, 3 ), My_GenMatrix( 4, 3 );
55  My_GenMatrix = 1.0;
56  // Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix )
57  My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, My_Matrix, My_GenMatrix, 0.0 );
58  My_Copy2 += My_Matrix; // Matrix addition
59  My_Copy2 *= 0.5; // Matrix scaling
60 
61  // Matrices can be compared:
62  // Check if the matrices are equal in dimension and values
63  if (Empty_Matrix == My_Matrix) {
64  std::cout<< "The matrices are the same!" <<std::endl;
65  }
66  // Check if the matrices are different in dimension or values
67  if (My_Copy2 != My_Matrix) {
68  std::cout<< "The matrices are different!" <<std::endl;
69  }
70 
71  // The norm of a matrix can be computed:
72  double norm_one, norm_inf, norm_fro;
73  norm_one = My_Matrix.normOne(); // one norm
74  norm_inf = My_Matrix.normInf(); // infinity norm
75  norm_fro = My_Matrix.normFrobenius(); // frobenius norm
76 
77  std::cout << std::endl << "|| My_Matrix ||_1 = " << norm_one << std::endl;
78  std::cout << "|| My_Matrix ||_Inf = " << norm_inf << std::endl;
79  std::cout << "|| My_Matrix ||_F = " << norm_fro << std::endl << std::endl;
80 
81  // A matrix can be factored and solved using Teuchos::SerialDenseSolver.
84  My_Matrix2.random();
86  X = 1.0;
87  B.multiply( Teuchos::LEFT_SIDE, 1.0, My_Matrix2, X, 0.0 );
88  X = 0.0; // Make sure the computed answer is correct.
89 
90  int info = 0;
91  My_Solver.setMatrix( Teuchos::rcp( &My_Matrix2, false ) );
92  My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) );
93  info = My_Solver.factor();
94  if (info != 0)
95  std::cout << "Teuchos::SerialSpdDenseSolver::factor() returned : " << info << std::endl;
96  info = My_Solver.solve();
97  if (info != 0)
98  std::cout << "Teuchos::SerialSpdDenseSolver::solve() returned : " << info << std::endl;
99 
100  // A matrix triple-product can be computed: C = alpha*W'*A*W
101  double alpha=0.5;
104  A1(0,0) = 1.0, A1(1,1) = 2.0;
105  A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00;
106  W = 1.0;
107 
109 
110  Teuchos::symMatTripleProduct<int,double>( Teuchos::NO_TRANS, alpha, A1, W, C1);
111  Teuchos::symMatTripleProduct<int,double>( Teuchos::TRANS, alpha, A2, W, C2 );
112 
113  // A matrix can be sent to the output stream:
114  std::cout<< printMat(My_Matrix) << std::endl;
115  std::cout<< printMat(X) << std::endl;
116 
117  return 0;
118 }
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.
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
Return SerialBandDenseMatrix ostream manipulator Use as:
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...