Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
DenseMatrix/cxx_main_sym.cpp

This is an example of how to use the Teuchos::SerialSymDenseMatrix class.

// @HEADER
// *****************************************************************************
// Teuchos: Common Tools Package
//
// Copyright 2004 NTESS and the Teuchos contributors.
// SPDX-License-Identifier: BSD-3-Clause
// *****************************************************************************
// @HEADER
#include "Teuchos_RCP.hpp"
#include "Teuchos_Version.hpp"
int main(int argc, char* argv[])
{
std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
// Creating a double-precision matrix can be done in several ways:
// Create an empty matrix with no dimension
// Create an empty 4x4 matrix
// Basic copy of My_Matrix
// (Deep) Copy of principle 3x3 submatrix of My_Matrix
My_Copy2( Teuchos::Copy, My_Matrix, 3 ),
// (Shallow) Copy of 3x3 submatrix of My_Matrix
My_Copy3( Teuchos::View, My_Matrix, 3, 1 );
// The matrix dimensions and strided storage information can be obtained:
int rows, cols, stride;
rows = My_Copy3.numRows(); // number of rows
cols = My_Copy3.numCols(); // number of columns
stride = My_Copy3.stride(); // storage stride
// Matrices can change dimension:
Empty_Matrix.shape( 3 ); // size non-dimensional matrices
My_Matrix.reshape( 3 ); // resize matrices and save values
// Filling matrices with numbers can be done in several ways:
My_Matrix.random(); // random numbers
My_Copy1.putScalar( 1.0 ); // every entry is 1.0
My_Copy1 = 1.0; // every entry is 1.0 (still)
My_Copy2(1,1) = 10.0; // individual element access
Empty_Matrix = My_Matrix; // copy My_Matrix to Empty_Matrix
// Basic matrix arithmetic can be performed:
Teuchos::SerialDenseMatrix<int,double> My_Prod( 4, 3 ), My_GenMatrix( 4, 3 );
My_GenMatrix = 1.0;
// Matrix multiplication ( My_Prod = 1.0*My_GenMatrix*My_Matrix )
My_Prod.multiply( Teuchos::RIGHT_SIDE, 1.0, My_Matrix, My_GenMatrix, 0.0 );
My_Copy2 += My_Matrix; // Matrix addition
My_Copy2 *= 0.5; // Matrix scaling
// Matrices can be compared:
// Check if the matrices are equal in dimension and values
if (Empty_Matrix == My_Matrix) {
std::cout<< "The matrices are the same!" <<std::endl;
}
// Check if the matrices are different in dimension or values
if (My_Copy2 != My_Matrix) {
std::cout<< "The matrices are different!" <<std::endl;
}
// The norm of a matrix can be computed:
double norm_one, norm_inf, norm_fro;
norm_one = My_Matrix.normOne(); // one norm
norm_inf = My_Matrix.normInf(); // infinity norm
norm_fro = My_Matrix.normFrobenius(); // frobenius norm
std::cout << std::endl << "|| My_Matrix ||_1 = " << norm_one << std::endl;
std::cout << "|| My_Matrix ||_Inf = " << norm_inf << std::endl;
std::cout << "|| My_Matrix ||_F = " << norm_fro << std::endl << std::endl;
// A matrix can be factored and solved using Teuchos::SerialDenseSolver.
My_Matrix2.random();
X = 1.0;
B.multiply( Teuchos::LEFT_SIDE, 1.0, My_Matrix2, X, 0.0 );
X = 0.0; // Make sure the computed answer is correct.
int info = 0;
My_Solver.setMatrix( Teuchos::rcp( &My_Matrix2, false ) );
My_Solver.setVectors( Teuchos::rcp( &X, false ), Teuchos::rcp( &B, false ) );
info = My_Solver.factor();
if (info != 0)
std::cout << "Teuchos::SerialSpdDenseSolver::factor() returned : " << info << std::endl;
info = My_Solver.solve();
if (info != 0)
std::cout << "Teuchos::SerialSpdDenseSolver::solve() returned : " << info << std::endl;
// A matrix triple-product can be computed: C = alpha*W'*A*W
double alpha=0.5;
A1(0,0) = 1.0, A1(1,1) = 2.0;
A2(0,0) = 1.0, A2(1,1) = 2.0, A2(2,2) = 3.00;
W = 1.0;
// A matrix can be sent to the output stream:
std::cout<< printMat(My_Matrix) << std::endl;
std::cout<< printMat(X) << std::endl;
return 0;
}
Reference-counted pointer class and non-member templated function implementations.
Non-member helper functions on the templated serial, dense matrix/vector classes.
Templated serial dense matrix class.
Templated class for constructing and using Hermitian positive definite dense matrices.
Templated serial, dense, symmetric matrix class.
This class creates and provides basic support for dense rectangular matrix of templated type.
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.
A class for constructing and using Hermitian positive definite dense matrices.
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).
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...
int shape(OrdinalType numRowsCols)
Set dimensions of a Teuchos::SerialSymDenseMatrix object; init values to zero.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
OrdinalType numCols() const
Returns the column dimension of this 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.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
void symMatTripleProduct(ETransp transw, const ScalarType alpha, const SerialSymDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &W, SerialSymDenseMatrix< OrdinalType, ScalarType > &B)
A templated, non-member, helper function for computing the matrix triple-product: B = alpha*W^T*A*W o...
int reshape(OrdinalType numRowsCols)
Reshape a Teuchos::SerialSymDenseMatrix object.
OrdinalType numRows() const
Returns the row dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-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 ...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.