Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
numerics/example/DenseMatrix/cxx_main.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 
13 #include "Teuchos_RCP.hpp"
14 #include "Teuchos_Version.hpp"
15 
16 int main(int argc, char* argv[])
17 {
18  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
19 
20  // Creating a double-precision matrix can be done in several ways:
21  // Create an empty matrix with no dimension
23  // Create an empty 3x4 matrix
25  // Basic copy of My_Matrix
26  Teuchos::SerialDenseMatrix<int,double> My_Copy1( My_Matrix ),
27  // (Deep) Copy of principle 3x3 submatrix of My_Matrix
28  My_Copy2( Teuchos::Copy, My_Matrix, 3, 3 ),
29  // (Shallow) Copy of 2x3 submatrix of My_Matrix
30  My_Copy3( Teuchos::View, My_Matrix, 2, 3, 1, 1 );
31  // Create a double-precision vector:
33 
34  // The matrix dimensions and strided storage information can be obtained:
35  int rows, cols, stride;
36  rows = My_Copy3.numRows(); // number of rows
37  cols = My_Copy3.numCols(); // number of columns
38  stride = My_Copy3.stride(); // storage stride
39  TEUCHOS_ASSERT_EQUALITY(rows, 2);
40  TEUCHOS_ASSERT_EQUALITY(cols, 3);
41  TEUCHOS_ASSERT_EQUALITY(stride, 3);
42 
43  // Matrices can change dimension:
44  Empty_Matrix.shape( 3, 3 ); // size non-dimensional matrices
45  My_Matrix.reshape( 3, 3 ); // resize matrices and save values
46 
47  // Filling matrices with numbers can be done in several ways:
48  My_Matrix.random(); // random numbers
49  My_Copy1.putScalar( 1.0 ); // every entry is 1.0
50  My_Copy2(1,1) = 10.0; // individual element access
51  Empty_Matrix = My_Matrix; // copy My_Matrix to Empty_Matrix
52  x = 1.0; // every entry of vector is 1.0
53  y = 1.0;
54 
55  // Basic matrix arithmetic can be performed:
56  double d;
58  // Matrix multiplication ( My_Prod = 1.0*My_Matrix*My_Copy^T )
60  1.0, My_Matrix, My_Copy3, 0.0 );
61  My_Copy2 += My_Matrix; // Matrix addition
62  My_Copy2.scale( 0.5 ); // Matrix scaling
63  d = x.dot( y ); // Vector dot product
64  (void)d; // Not used!
65 
66  // The pointer to the array of matrix values can be obtained:
67  double *My_Array=0, *My_Column=0;
68  My_Array = My_Matrix.values(); // pointer to matrix values
69  My_Column = My_Matrix[2]; // pointer to third column values
70  (void)My_Array; // Not used!
71  (void)My_Column; // Not used!
72 
73  // The norm of a matrix can be computed:
74  double norm_one, norm_inf, norm_fro;
75  norm_one = My_Matrix.normOne(); // one norm
76  norm_inf = My_Matrix.normInf(); // infinity norm
77  norm_fro = My_Matrix.normFrobenius(); // frobenius norm
78  (void)norm_one; // Not used!
79  (void)norm_inf; // Not used!
80  (void)norm_fro; // Not used!
81 
82  // Matrices can be compared:
83  // Check if the matrices are equal in dimension and values
84  if (Empty_Matrix == My_Matrix) {
85  std::cout<< "The matrices are the same!" <<std::endl;
86  }
87  // Check if the matrices are different in dimension or values
88  if (My_Copy2 != My_Matrix) {
89  std::cout<< "The matrices are different!" <<std::endl;
90  }
91 
92  // A matrix can be factored and solved using Teuchos::SerialDenseSolver.
95  X.putScalar(1.0);
96  B.multiply( Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, My_Matrix, X, 0.0 );
97  X.putScalar(0.0); // Make sure the computed answer is correct.
98 
99  int info = 0;
100  My_Solver.setMatrix( Teuchos::rcp( &My_Matrix, false ) );
101  My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) );
102  info = My_Solver.factor();
103  if (info != 0)
104  std::cout << "Teuchos::SerialDenseSolver::factor() returned : " << info << std::endl;
105  info = My_Solver.solve();
106  if (info != 0)
107  std::cout << "Teuchos::SerialDenseSolver::solve() returned : " << info << std::endl;
108 
109  // A matrix can be sent to the output stream:
110  std::cout<< std::endl << printMat(My_Matrix) << std::endl;
111  std::cout<< printMat(X) << std::endl;
112 
113  return 0;
114 }
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
ScalarType * values() const
Data array access method.
Templated serial dense matrix class.
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
Return SerialBandDenseMatrix ostream manipulator Use as:
int solve()
Computes the solution X to AX = B for the this matrix and the B provided to SetVectors()..
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 scale(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
This class creates and provides basic support for dense vectors of templated type as a specialization...
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
Templated class for solving dense linear problems.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
std::string Teuchos_Version()
int main(int argc, char *argv[])
OrdinalType numCols() const
Returns the column dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
int random()
Set all values in the matrix to be random numbers.
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).
int reshape(OrdinalType numRows, OrdinalType numCols)
Reshaping method for changing the size of a SerialDenseMatrix, keeping the entries.
Templated serial dense vector class.
int factor()
Computes the in-place LU factorization of the matrix using the LAPACK routine _GETRF.
int shape(OrdinalType numRows, OrdinalType numCols)
Shape method for changing the size of a SerialDenseMatrix, initializing entries to zero...
#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.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
OrdinalType numRows() const
Returns the row dimension of this matrix.
A class for solving dense linear problems.
int setMatrix(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &A)
Sets the pointers for coefficient matrix.
This class creates and provides basic support for dense rectangular matrix of templated type...