IFEM  90A354
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
utl::matrix< T > Class Template Reference

Two-dimensional rectangular matrix with some algebraic operations. More...

#include <matrix.h>

Inheritance diagram for utl::matrix< T >:
Inheritance graph
[legend]
Collaboration diagram for utl::matrix< T >:
Collaboration graph
[legend]

Public Member Functions

 matrix ()
 Constructor creating an empty matrix.
 
 matrix (vector< T > &vec)
 Constructor using an external vector for matrix element storage.
 
 matrix (size_t r, size_t c)
 Constructor creating a matrix of dimension \(r \times c\).
 
 matrix (const matrix< T > &mat, bool transposed=false)
 Copy constructor, optionally creates the transpose of mat.
 
virtual ~matrix ()
 Empty destructor.
 
void resize (size_t r, size_t c, bool forceClear=false)
 Resize the matrix to dimension \(r \times c\). More...
 
matrix< T > & expandRows (int incRows)
 Increase or decrease the number of rows in the matrix.
 
bool augmentRows (const matrix< T > &B)
 Increase the number of rows by augmenting the given matrix.
 
bool augmentCols (const matrix< T > &B)
 Increase the number of columns by augmenting the given matrix.
 
size_t rows () const
 Query number of matrix rows.
 
size_t cols () const
 Query number of matrix columns.
 
matrix< T > & operator= (const matrix< T > &A)
 Assignment operator.
 
matrix< T > & operator= (const std::vector< T > &X)
 Overloaded assignment operator.
 
T & operator() (size_t r, size_t c)
 Index-1 based element access. More...
 
const T & operator() (size_t r, size_t c) const
 Index-1 based element reference. More...
 
vector< T > getRow (size_t r) const
 Extract a row from the matrix.
 
std::vector< T > getColumn (size_t c) const
 Extract a column from the matrix.
 
void fill (const std::vector< T > &v, size_t n, size_t m=0)
 Fill the matrix with vector data.
 
void fillColumn (size_t c, const std::vector< T > &data)
 Fill a column of the matrix.
 
void fillColumn (size_t c, const T *data)
 Fill a column of the matrix.
 
void fillRow (size_t r, const T *data)
 Fill a row of the matrix.
 
void fillBlock (const matrix< T > &block, size_t r, size_t c, bool transposed=false)
 Fill a block of the matrix with another matrix.
 
void addBlock (const matrix< T > &block, T s, size_t r, size_t c, bool transposed=false)
 Add a scalar multiple of another matrix to a block of the matrix.
 
void extractBlock (matrix< T > &block, size_t r, size_t c, bool addTo=false, bool transposed=false) const
 Extract a block of the matrix to another matrix.
 
matrix< T > & diag (T d, size_t dim=0)
 Create a diagonal matrix.
 
matrix< T > & transpose ()
 Replace the current matrix by its transpose.
 
trace () const
 Return the trace of the matrix (sum of its diagonal elements).
 
rowsum (size_t r) const
 Return the sum of a matrix row.
 
colsum (size_t c) const
 Return the sum of a matrix column.
 
det () const
 Compute the determinant of a square matrix.
 
inverse (T tol=T(0))
 Compute the inverse of a square matrix. More...
 
bool isSymmetric (T tol=T(0)) const
 Check for symmetry. More...
 
matrix< T > & operator+= (const matrix< T > &A)
 Add the given matrix A to *this.
 
matrix< T > & operator-= (const matrix< T > &A)
 Subtract the given matrix A from *this.
 
matrix< T > & add (const matrix< T > &A, T alfa=T(1))
 Add the given matrix A scaled by alfa to *this.
 
matrix< T > & operator*= (T c)
 Multiplication with a scalar.
 
matrix< T > & operator/= (T d)
 Division by a scalar.
 
matrix< T > & multiply (T c)
 Multiplication of this matrix by a scalar c.
 
matrix< T > & multiply (const matrix< T > &A, const matrix< T > &B, bool transA=false, bool transB=false, bool addTo=false, const T &alpha=T(1))
 Matrix-matrix multiplication. More...
 
bool multiplyMat (const matrix< T > &A, const std::vector< T > &B, bool transA=false, bool addTo=false)
 Matrix-matrix multiplication. More...
 
bool multiplyMat (const std::vector< T > &A, const matrix< T > &B, bool transB=false, bool addTo=false)
 Matrix-matrix multiplication. More...
 
bool multiply (const std::vector< T > &X, std::vector< T > &Y, bool transA=false, char addTo=0) const
 Matrix-vector multiplication. More...
 
bool multiply (const std::vector< T > &X, std::vector< T > &Y, const T &alpha, const T &beta=T(0), bool transA=false, int stridex=1, int stridey=1, unsigned int ofsx=0, unsigned int ofsy=0) const
 Matrix-vector multiplication. More...
 
bool outer_product (const std::vector< T > &X, const std::vector< T > &Y, bool addTo=false, T alpha=T(1))
 Outer product between two vectors.
 
normInf () const
 Return the infinite norm of the matrix.
 
- Public Member Functions inherited from utl::matrixBase< T >
size_t dim (short int d=1) const
 Query dimensions.
 
size_t size () const
 Query total matrix size.
 
bool empty () const
 Check if the matrix is empty.
 
bool zero (T tol=T(0)) const
 Check if the matrix elements are all zero.
 
const vector< T > & toVec () const
 Type casting to a one-dimensional utl::vector, for access.
 
 operator const std::vector< T > & () const
 Type casting to a one-dimensional std::vector, for access.
 
 operator std::vector< T > & ()
 Type casting to a one-dimensional vector, for update.
 
T * ptr (size_t c=0)
 Access through pointer.
 
const T * ptr (size_t c=0) const
 Reference through pointer.
 
std::vector< T >::iterator begin ()
 Iterator to the start of the matrix elements.
 
std::vector< T >::iterator end ()
 Iterator to the end of the matrix elements.
 
void clear ()
 Clears the matrix and sets its dimension to zero.
 
void fill (T s)
 Fill the matrix with a scalar value.
 
void fill (const T *values, size_t n=0)
 Fill the matrix with data from an array.
 
matrixBase< T > & add (const matrixBase< T > &A, const T &alfa)
 Add the given matrix A scaled by alfa to *this.
 
matrixBase< T > & multiply (const T &c)
 Multiplication of this matrix by a scalar c.
 
norm2 (int inc=1) const
 Return the Euclidean norm of the matrix. More...
 
asum (int inc=1) const
 Return the sum of the absolute value of the matrix elements. More...
 
sum (int inc=1) const
 Return the sum of the matrix elements. More...
 

Protected Member Functions

void clearIfNrowChanged (size_t n1, size_t, size_t) override
 Clears the content if the number of rows changed.
 
- Protected Member Functions inherited from utl::matrixBase< T >
 matrixBase ()
 The constructor is protected to allow sub-class instances only.
 
 matrixBase (vector< T > &vec)
 Constructor using an external vector for matrix element storage.
 
 matrixBase (size_t n_1, size_t n_2, size_t n_3=1, size_t n_4=1)
 Constructor creating a matrix of dimension \(n_1 \times n_2 \times n_3 \times n_4\).
 
 matrixBase (const matrixBase< T > &mat, bool copyContent=true)
 Copy constructor. More...
 
void redim (size_t n_1, size_t n_2, size_t n_3, size_t n_4, bool forceClear)
 Resize the matrix to dimension \(n_1 \times n_2 \times n_3 \times n_4\). More...
 

Private Member Functions

bool compatible (const std::vector< T > &X, bool transA) const
 Check dimension compatibility for matrix-vector multiplication.
 
bool compatible (const matrix< T > &A, const matrix< T > &B, bool transA, bool transB, size_t &M, size_t &N, size_t &K)
 Check dimension compatibility for matrix-matrix multiplication.
 
bool compatible (const matrix< T > &A, const std::vector< T > &B, bool transA, size_t &M, size_t &N, size_t &K)
 Check dimension compatibility for matrix-matrix multiplication, when the matrix B is represented by a one-dimensional vector.
 
bool compatible (const std::vector< T > &A, const matrix< T > &B, bool transB, size_t &M, size_t &N, size_t &K)
 Check dimension compatibility for matrix-matrix multiplication, when the matrix A is represented by a one-dimensional vector.
 
bool compatible (const std::vector< T > &X, const std::vector< T > &Y)
 Check dimension compatibility for outer product multiplication.
 

Private Attributes

size_t & nrow
 Number of matrix rows.
 
size_t & ncol
 Number of matrix columns.
 

Additional Inherited Members

- Protected Attributes inherited from utl::matrixBase< T >
size_t n [4]
 Dimension of the matrix.
 
vector< T > & elem
 Actual matrix elements, stored column by column.
 

Detailed Description

template<class T>
class utl::matrix< T >

Two-dimensional rectangular matrix with some algebraic operations.

This is a 2D equivalent to the vector class. The matrix elements are stored column-wise in a one-dimensional array, such that its pointer might be passed to Fortran subroutines requiring 2D arrays as arguments.

Member Function Documentation

◆ inverse()

template<class T >
T utl::matrix< T >::inverse ( tol = T(0))
inline

Compute the inverse of a square matrix.

Parameters
[in]tolDivision by zero tolerance
Returns
Determinant of the matrix

References utl::matrix< T >::det().

Referenced by utl::invert(), and utl::Jacobian().

◆ isSymmetric()

template<class T >
bool utl::matrix< T >::isSymmetric ( tol = T(0)) const
inline

Check for symmetry.

Parameters
[in]tolComparison tolerance

References utl::matrixBase< T >::elem, utl::matrix< T >::ncol, and utl::matrix< T >::nrow.

Referenced by utl::operator<<().

◆ multiply() [1/3]

template<class T >
matrix< T > & utl::matrix< T >::multiply ( const matrix< T > &  A,
const matrix< T > &  B,
bool  transA = false,
bool  transB = false,
bool  addTo = false,
const T &  alpha = T(1) 
)
inline

Matrix-matrix multiplication.

Performs one of the following operations (C = *this):

  1. \( {\bf C} = {\bf A} {\bf B} \)
  2. \( {\bf C} = {\bf A}^T {\bf B} \)
  3. \( {\bf C} = {\bf A} {\bf B}^T \)
  4. \( {\bf C} = {\bf A}^T {\bf B}^T \)
  5. \( {\bf C} = {\bf C} + {\bf A} {\bf B} \)
  6. \( {\bf C} = {\bf C} + {\bf A}^T {\bf B} \)
  7. \( {\bf C} = {\bf C} + {\bf A} {\bf B}^T \)
  8. \( {\bf C} = {\bf C} + {\bf A}^T {\bf B}^T \)

References K, and M.

◆ multiply() [2/3]

template<class T >
bool utl::matrix< T >::multiply ( const std::vector< T > &  X,
std::vector< T > &  Y,
bool  transA = false,
char  addTo = 0 
) const
inline

Matrix-vector multiplication.

Performs one of the following operations (A = *this):

  1. \( {\bf Y} = {\bf A} {\bf X} \)
  2. \( {\bf Y} = {\bf A}^T {\bf X} \)
  3. \( {\bf Y} = {\bf Y} + {\bf A} {\bf X} \)
  4. \( {\bf Y} = {\bf Y} + {\bf A}^T {\bf X} \)
  5. \( {\bf Y} = {\bf Y} - {\bf A} {\bf X} \)
  6. \( {\bf Y} = {\bf Y} - {\bf A}^T {\bf X} \)

◆ multiply() [3/3]

template<class T >
bool utl::matrix< T >::multiply ( const std::vector< T > &  X,
std::vector< T > &  Y,
const T &  alpha,
const T &  beta = T(0),
bool  transA = false,
int  stridex = 1,
int  stridey = 1,
unsigned int  ofsx = 0,
unsigned int  ofsy = 0 
) const
inline

Matrix-vector multiplication.

Performs the following operations (A = *this):

  1. \( {\bf Y} = {\alpha}{\bf A} {\bf X} + {\beta}{\bf Y}\)
  2. \( {\bf Y} = {\alpha}{\bf A}^T {\bf X} + {\beta}{\bf Y}\)

◆ multiplyMat() [1/2]

template<class T >
bool utl::matrix< T >::multiplyMat ( const matrix< T > &  A,
const std::vector< T > &  B,
bool  transA = false,
bool  addTo = false 
)
inline

Matrix-matrix multiplication.

Performs one of the following operations (C = *this):

  1. \( {\bf C} = {\bf A} {\bf B} \)
  2. \( {\bf C} = {\bf A}^T {\bf B} \)
  3. \( {\bf C} = {\bf C} + {\bf A} {\bf B} \)
  4. \( {\bf C} = {\bf C} + {\bf A}^T {\bf B} \)

The matrix B is here represented by a one-dimensional vector, and its number of rows is assumed to match the number of columns in A (or its transpose) and its number of columns is then the total vector length divided by the number of rows.

References K, and M.

◆ multiplyMat() [2/2]

template<class T >
bool utl::matrix< T >::multiplyMat ( const std::vector< T > &  A,
const matrix< T > &  B,
bool  transB = false,
bool  addTo = false 
)
inline

Matrix-matrix multiplication.

Performs one of the following operations (C = *this):

  1. \( {\bf C} = {\bf A} {\bf B} \)
  2. \( {\bf C} = {\bf A} {\bf B}^T \)
  3. \( {\bf C} = {\bf C} + {\bf A} {\bf B} \)
  4. \( {\bf C} = {\bf C} + {\bf A} {\bf B}^T \)

The matrix A is here represented by a one-dimensional vector, and its number of columns is assumed to match the number of rows in B (or its transpose) and its number of rows is then the total vector length divided by the number of columns.

References K, and M.

◆ operator()() [1/2]

template<class T >
T& utl::matrix< T >::operator() ( size_t  r,
size_t  c 
)
inline

Index-1 based element access.

Assuming column-wise storage as in Fortran.

◆ operator()() [2/2]

template<class T >
const T& utl::matrix< T >::operator() ( size_t  r,
size_t  c 
) const
inline

Index-1 based element reference.

Assuming column-wise storage as in Fortran.

◆ resize()

template<class T >
void utl::matrix< T >::resize ( size_t  r,
size_t  c,
bool  forceClear = false 
)
inline

Resize the matrix to dimension \(r \times c\).

Will erase the previous content, but only if both the total matrix size and the number of rows in the matrix are changed. It is therefore possible to add or remove a given number of columns to the matrix without loosing the contents of the remaining columns. If forceClear is true, the old matrix content is always erased.

References utl::matrixBase< T >::redim().

Referenced by ASMs1D::assembleL2matrices(), Legendre::basisDerivatives(), ASMu3D::BasisFunctionCache::calculatePrm(), Lagrange::computeBasis(), SPRMatrix::convert(), DenseMatrix::DenseMatrix(), utl::matrix< T >::diag(), SplineField::evalMapping(), LRSplineField::evalMapping(), ASMs2D::evalProjSolution(), ASMs3D::evalProjSolution(), ASMu2D::evalProjSolution(), ASMu3D::evalProjSolution(), ASMs1D::evalProjSolution(), ASMs2DTri::evalSolution(), ASMs1DLag::evalSolution(), ASMs1DSpec::evalSolution(), ASMs2DSpec::evalSolution(), ASMs3DSpec::evalSolution(), ASMs3DmxLag::evalSolution(), ASMsupel::evalSolution(), ASMs2DmxLag::evalSolution(), ASMs2Dmx::evalSolution(), ASMs3Dmx::evalSolution(), ASMs2DLag::evalSolution(), ASMs3DLag::evalSolution(), ASMs2D::evalSolution(), ASMs3D::evalSolution(), ASMu2Dmx::evalSolution(), ASMu3Dmx::evalSolution(), ASMu2D::evalSolution(), ASMu3D::evalSolution(), ASMs1D::evalSolution(), ASMs2Dmx::evalSolutionPiola(), ASMu2Dmx::evalSolutionPiola(), L2FuncIntegrand::evaluate(), ASMu3D::evaluateBasis(), SplineUtils::extractBasis(), ASMs1D::extractBasis(), ASMbase::extractElmRes(), utl::matrix< T >::fill(), utl::gather(), ASMu2D::getCoordinates(), ASMs1D::getElementCoordinates(), ASMs2D::getElementCoordinates(), ASMs3D::getElementCoordinates(), ASMu3D::getElementCoordinates(), ASMs1DLag::getElementCoordinates(), ASMs2D::getElementCoordinatesPrm(), ASMs3D::getElementCoordinatesPrm(), utl::getGmat(), BlockElmMats::getNewtonMatrix(), ASMs2D::getNodalCoordinates(), ASMs3D::getNodalCoordinates(), ASMs1D::getNodalCoordinates(), ASMs1DLag::getNodalCoordinates(), ASMs2DLag::getNodalCoordinates(), ASMsupel::getNodalCoordinates(), ASMu3D::getNodalCoordinates(), ASMbase::getSolution(), ASMmxBase::getSolutionMx(), ASMbase::globalL2projection(), LagrangeFields2D::gradFE(), LagrangeFields3D::gradFE(), LRSplineFields2D::gradFE(), LRSplineFields2Dmx::gradFE(), LRSplineFields3D::gradFE(), LRSplineFields3Dmx::gradFE(), SplineFields2Dmx::gradFE(), SplineFields3Dmx::gradFE(), utl::Hessian(), LRSplineField2D::hessianFE(), LRSplineField3D::hessianFE(), SplineField2D::hessianFE(), SplineField3D::hessianFE(), DenseMatrix::initAssembly(), GlbForceVec::initNodeMap(), ASMs1D::integrate(), utl::JacobianGradient(), ASMLagBase::nodalField(), utl::operator>>(), SIMsupel::parse(), MxFiniteElement::piolaGradient(), SIMbase::project(), AlgEqSystem::readRecoveryMatrix(), DenseMatrix::redim(), TimeIntegration::SIMExplicitRK< Solver >::SIMExplicitRK(), TimeIntegration::SIMExplicitRKE< Solver >::SIMExplicitRKE(), SIMbase::solutionNorms(), GlbL2::solve(), eig::solve(), PETScMatrix::solveEig(), DenseMatrix::solveEig(), SPRMatrix::solveEig(), AlgEqSystem::staticCondensation(), and SIMoutput::writeGlvN().


The documentation for this class was generated from the following file: