walberla::math::MatrixMxN< Type > Class Template Reference

## Detailed Description

### template<typename Type> class walberla::math::MatrixMxN< Type >

Efficient implementation of a $M \times N$ matrix.

The MatrixMxN class is the representation of a dynamic $M \times N$ matrix with a total of $M \cdot N$ dynamically allocated elements. These elements can be directly accessed with the 1D subscript operator or with the 2D function operator. The matrix is stored in a row-wise fashion:

        \f[\left(\begin{array}{*{5}{c}}
0           & 1             & 2             & \cdots & N-1         \\
N           & N+1           & N+2           & \cdots & 2 \cdot N-1 \\
\vdots      & \vdots        & \vdots        & \ddots & \vdots      \\
M \cdot N-N & M \cdot N-N+1 & M \cdot N-N+2 & \cdots & M \cdot N-1 \\
\end{array}\right)\f]


MatrixMxN can be used with any non-cv-qualified element type. The arithmetic operators for matrix/matrix, matrix/vector and matrix/element operations with the same element type work for any element type as long as the element type supports the arithmetic operation. Arithmetic operations between matrices, vectors and elements of different element types are only supported for all data types supported by the MathTrait class template (for details see the MathTrait class description).

MatrixMxN< double > a, b, c;
MatrixMxN< float > d;
MatrixMxN< std::complex<double> > e, f, g;
MatrixMxN< std::complex<float> > h;
... // Appropriate resizing
c = a + b; // OK: Same element type, supported
c = a + d; // OK: Different element types, supported by the MathTrait class template
g = e + f; // OK: Same element type, supported
g = e + h; // Error: Different element types, not supported by the MathTrait class template

#include <Types.h>

## Public Types

using This = MatrixMxN< Type >
Type of this MatrixMxN instance. More...

using ResultType = This
Result type for expression template evaluations. More...

using ElementType = Type
Type of the matrix elements. More...

using CompositeType = const MatrixMxN &
Data type for composite expression templates. More...

## Public Member Functions

template<typename Other , size_t M, size_t N>
MatrixMxN< Type > & operator= (const Other(&rhs)[M][N])
Array assignment to all matrix elements. More...

template<typename Other >
MatrixMxN< Type > & scale (Other scalar)
Scaling of the matrix by the scalar value scalar ( $A=B*s$). More...

Constructors
constexpr MatrixMxN ()
The default constructor for MatrixMxN. More...

constexpr MatrixMxN (size_t m, size_t n)
Constructor for a matrix of size $m \times n$. More...

constexpr MatrixMxN (size_t m, size_t n, Type init)
Constructor for a homogenous initialization of all $m \times n$ matrix elements. More...

constexpr MatrixMxN (const MatrixMxN &m)
The copy constructor for MatrixMxN. More...

template<typename Other , size_t M, size_t N>
constexpr MatrixMxN (const Other(&rhs)[M][N])
Array initialization of all matrix elements. More...

Destructor
~MatrixMxN ()
The destructor for MatrixMxN. More...

Operators
template<typename Other , size_t M, size_t N>
MatrixMxNoperator= (const Other(&rhs)[M][N])

MatrixMxNoperator= (Type set)
Homogenous assignment to all matrix elements. More...

MatrixMxNoperator= (const MatrixMxN &set)
Copy assignment operator for MatrixMxN. More...

Type & operator[] (size_t index)

const Type & operator[] (size_t index) const

Type & operator() (size_t i, size_t j)

const Type & operator() (size_t i, size_t j) const

Utility functions
size_t rows () const
Returns the current number of rows of the matrix. More...

size_t columns () const
Returns the current number of columns of the matrix. More...

size_t capacity () const
Returns the maximum capacity of the matrix. More...

size_t nonZeros () const
Returns the total number of non-zero elements in the matrix. More...

size_t nonZeros (size_t i) const
Returns the number of non-zero elements in the specified row. More...

void reset ()
Reset to the default initial values. More...

void clear ()
Clearing the $M \times N$ matrix. More...

void resize (size_t m, size_t n, bool preserve=true)
Changing the size of the matrix. More...

void extend (size_t m, size_t n, bool preserve=true)
Extending the size of the matrix. More...

void reserve (size_t elements)
Setting the minimum capacity of the matrix. More...

MatrixMxNtranspose ()
Transposing the matrix. More...

bool isDiagonal () const
Checks if the matrix is diagonal. More...

bool isSymmetric () const
Checks if the matrix is symmetric. More...

template<typename Other >
MatrixMxNscale (Other scalar)

void swap (MatrixMxN &m)
Swapping the contents of two matrices. More...

Expression template evaluation functions
template<typename Other >
bool isAliased (const Other *alias) const
Returns whether the matrix is aliased with the given address alias. More...

## Private Attributes

Member variables
size_t m_
The current number of rows of the matrix. More...

size_t n_
The current number of columns of the matrix. More...

size_t capacity_
The maximum capacity of the matrix. More...

Type *WALBERLA_RESTRICT v_
The dynamically allocated matrix elements. More...

## ◆ CompositeType

template<typename Type >
 using walberla::math::MatrixMxN< Type >::CompositeType = const MatrixMxN &

Data type for composite expression templates.

## ◆ ElementType

template<typename Type >
 using walberla::math::MatrixMxN< Type >::ElementType = Type

Type of the matrix elements.

## ◆ ResultType

template<typename Type >
 using walberla::math::MatrixMxN< Type >::ResultType = This

Result type for expression template evaluations.

## ◆ This

template<typename Type >
 using walberla::math::MatrixMxN< Type >::This = MatrixMxN

Type of this MatrixMxN instance.

## ◆ MatrixMxN() [1/5]

template<typename Type >
 constexpr walberla::math::MatrixMxN< Type >::MatrixMxN
inlineexplicitconstexpr

The default constructor for MatrixMxN.

## ◆ MatrixMxN() [2/5]

template<typename Type >
 constexpr walberla::math::MatrixMxN< Type >::MatrixMxN ( size_t m, size_t n )
inlineexplicitconstexpr

Constructor for a matrix of size $m \times n$.

No element initialization is performed!

Parameters
 m The number of rows of the matrix. n The number of columns of the matrix.

Note: This constructor is only responsible to allocate the required dynamic memory. No element initialization is performed!

## ◆ MatrixMxN() [3/5]

template<typename Type >
 constexpr walberla::math::MatrixMxN< Type >::MatrixMxN ( size_t m, size_t n, Type init )
inlineexplicitconstexpr

Constructor for a homogenous initialization of all $m \times n$ matrix elements.

Parameters
 m The number of rows of the matrix. n The number of columns of the matrix. init The initial value of the matrix elements.

All matrix elements are initialized with the specified value.

## ◆ MatrixMxN() [4/5]

template<typename Type >
 constexpr walberla::math::MatrixMxN< Type >::MatrixMxN ( const MatrixMxN< Type > & m )
inlineconstexpr

The copy constructor for MatrixMxN.

Parameters
 m Matrix to be copied.

The copy constructor is explicitly defined due to the required dynamic memory management and in order to enable/facilitate NRV optimization.

## ◆ MatrixMxN() [5/5]

template<typename Type >
template<typename Other , size_t M, size_t N>
 constexpr walberla::math::MatrixMxN< Type >::MatrixMxN ( const Other(&) rhs[M][N] )
inlineconstexpr

Array initialization of all matrix elements.

Parameters
 rhs $M \times N$ dimensional array for the initialization.
Returns
Reference to the assigned matrix.

This constructor offers the option to directly initialize the elements of the matrix:

const real init[3][3] = { { 1, 2, 3 },
{ 4, 5 },
{ 7, 8, 9 } };
MatrixMxN<real> A = init;

The matrix is sized according to the size of the array and initialized with the given values. Missing values are initialized with zero (as e.g. the value 6 in the example).

## ◆ ~MatrixMxN()

template<typename Type >
 walberla::math::MatrixMxN< Type >::~MatrixMxN
inline

The destructor for MatrixMxN.

## ◆ capacity()

template<typename Type >
 size_t walberla::math::MatrixMxN< Type >::capacity
inline

Returns the maximum capacity of the matrix.

Returns
The capacity of the matrix.

## ◆ clear()

template<typename Type >
 void walberla::math::MatrixMxN< Type >::clear
inline

Clearing the $M \times N$ matrix.

Returns
void

After the clear() function, the size of the matrix is 0.

## ◆ columns()

template<typename Type >
 size_t walberla::math::MatrixMxN< Type >::columns
inline

Returns the current number of columns of the matrix.

Returns
The number of columns of the matrix.

## ◆ extend()

template<typename Type >
 void walberla::math::MatrixMxN< Type >::extend ( size_t m, size_t n, bool preserve = true )
inline

Extending the size of the matrix.

Parameters
 m Number of additional rows. n Number of additional columns. preserve true if the old values of the matrix should be preserved, false if not.
Returns
void

This function increases the matrix size by m rows and n columns. During this operation, new dynamic memory may be allocated in case the capacity of the matrix is too small. Therefore this function potentially changes all matrix elements. In order to preserve the old matrix values, the preserve flag can be set to true. However, new matrix elements are not initialized!

## ◆ isAliased()

template<typename Type >
template<typename Other >
 bool walberla::math::MatrixMxN< Type >::isAliased ( const Other * alias ) const
inline

Returns whether the matrix is aliased with the given address alias.

Parameters
 alias The alias to be checked.
Returns
true in case the alias corresponds to this matrix, false if not.

## ◆ isDiagonal()

template<typename Type >
 bool walberla::math::MatrixMxN< Type >::isDiagonal
inline

Checks if the matrix is diagonal.

Returns
true if the matrix is diagonal, false if not.

This function tests whether the matrix is diagonal, i.e. if the non-diagonal elements are default elements. In case of integral or floating point data types, a diagonal matrix has the form

                 \f[\left(\begin{array}{*{5}{c}}
aa     & 0      & 0      & \cdots & 0  \\
0      & bb     & 0      & \cdots & 0  \\
0      & 0      & cc     & \cdots & 0  \\
\vdots & \vdots & \vdots & \ddots & 0  \\
0      & 0      & 0      & 0      & mn \\
\end{array}\right)\f]


## ◆ isSymmetric()

template<typename Type >
 bool walberla::math::MatrixMxN< Type >::isSymmetric
inline

Checks if the matrix is symmetric.

Returns
true if the matrix is symmetric, false if not.

## ◆ nonZeros() [1/2]

template<typename Type >
 size_t walberla::math::MatrixMxN< Type >::nonZeros
inline

Returns the total number of non-zero elements in the matrix.

Returns
The number of non-zero elements in the sparse matrix.

## ◆ nonZeros() [2/2]

template<typename Type >
 size_t walberla::math::MatrixMxN< Type >::nonZeros ( size_t i ) const
inline

Returns the number of non-zero elements in the specified row.

Parameters
 i The index of the row.
Returns
The number of non-zero elements of row i.

## ◆ operator()() [1/2]

template<typename Type >
 Type & walberla::math::MatrixMxN< Type >::operator() ( size_t i, size_t j )
inline

Parameters
 i Access index for the row. The index has to be in the range $[0..M-1]$. j Access index for the column. The index has to be in the range $[0..N-1]$.
Returns
Reference to the accessed value.

## ◆ operator()() [2/2]

template<typename Type >
 const Type & walberla::math::MatrixMxN< Type >::operator() ( size_t i, size_t j ) const
inline

Parameters
 i Access index for the row. The index has to be in the range $[0..M-1]$. j Access index for the column. The index has to be in the range $[0..N-1]$.
Returns
Reference to the accessed value.

## ◆ operator=() [1/4]

template<typename Type >
 MatrixMxN< Type > & walberla::math::MatrixMxN< Type >::operator= ( const MatrixMxN< Type > & rhs )
inline

Copy assignment operator for MatrixMxN.

Parameters
 rhs Matrix to be copied.
Returns
Reference to the assigned matrix.

The matrix is resized according to the given $M \times N$ matrix and initialized as a copy of this matrix.

## ◆ operator=() [2/4]

template<typename Type >
template<typename Other , size_t M, size_t N>
 MatrixMxN& walberla::math::MatrixMxN< Type >::operator= ( const Other(&) rhs[M][N] )
inline

## ◆ operator=() [3/4]

template<typename Type >
template<typename Other , size_t M, size_t N>
 MatrixMxN& walberla::math::MatrixMxN< Type >::operator= ( const Other(&) rhs[M][N] )
inline

Array assignment to all matrix elements.

Parameters
 rhs $M \times N$ dimensional array for the assignment.
Returns
Reference to the assigned matrix.

This assignment operator offers the option to directly set all elements of the matrix:

const real init[3][3] = { { 1, 2, 3 },
{ 4, 5 },
{ 7, 8, 9 } };
MatrixMxN<real> A;
A = init;

The matrix is resized according to the size of the array and initialized with the given values. Missing values are initialized with zero (as e.g. the value 6 in the example).

## ◆ operator=() [4/4]

template<typename Type >
 MatrixMxN< Type > & walberla::math::MatrixMxN< Type >::operator= ( Type rhs )
inline

Homogenous assignment to all matrix elements.

Parameters
 rhs Scalar value to be assigned to all matrix elements.
Returns
Reference to the assigned matrix.

## ◆ operator[]() [1/2]

template<typename Type >
 Type & walberla::math::MatrixMxN< Type >::operator[] ( size_t index )
inline

Parameters
 index Access index. The index has to be in the range $[0..M \cdot N-1]$.
Returns
Reference to the accessed value.

## ◆ operator[]() [2/2]

template<typename Type >
 const Type & walberla::math::MatrixMxN< Type >::operator[] ( size_t index ) const
inline

Parameters
 index Access index. The index has to be in the range $[0..M \cdot N-1]$.
Returns
Reference to the accessed value.

## ◆ reserve()

template<typename Type >
 void walberla::math::MatrixMxN< Type >::reserve ( size_t elements )
inline

Setting the minimum capacity of the matrix.

Parameters
 elements The new minimum capacity of the sparse matrix.
Returns
void

This function increases the capacity of the sparse matrix to at least elements elements. The current values of the matrix elements are preserved.

## ◆ reset()

template<typename Type >
 void walberla::math::MatrixMxN< Type >::reset
inline

Reset to the default initial values.

Returns
void

## ◆ resize()

template<typename Type >
 void walberla::math::MatrixMxN< Type >::resize ( size_t m, size_t n, bool preserve = true )

Changing the size of the matrix.

Parameters
 m The new number of rows of the matrix. n The new number of columns of the matrix. preserve true if the old values of the matrix should be preserved, false if not.
Returns
void

This function resizes the matrix using the given size to $m \times n$. During this operation, new dynamic memory may be allocated in case the capacity of the matrix is too small. Therefore this function potentially changes all matrix elements. In order to preserve the old matrix values, the preserve flag can be set to true. However, new matrix elements are not initialized!
The following example illustrates the resize operation of a $2 \times 4$ matrix to a $4 \times 2$ matrix. The new, uninitialized elements are marked with x:

                       \f[
\left(\begin{array}{*{4}{c}}
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8 \\
\end{array}\right)

\Longrightarrow

\left(\begin{array}{*{2}{c}}
1 & 2 \\
5 & 6 \\
x & x \\
x & x \\
\end{array}\right)
\f]


## ◆ rows()

template<typename Type >
 size_t walberla::math::MatrixMxN< Type >::rows
inline

Returns the current number of rows of the matrix.

Returns
The number of rows of the matrix.

## ◆ scale() [1/2]

template<typename Type >
template<typename Other >
 MatrixMxN& walberla::math::MatrixMxN< Type >::scale ( Other scalar )
inline

## ◆ scale() [2/2]

template<typename Type >
template<typename Other >
 MatrixMxN& walberla::math::MatrixMxN< Type >::scale ( Other scalar )
inline

Scaling of the matrix by the scalar value scalar ( $A=B*s$).

Parameters
 scalar The scalar value for the matrix scaling.
Returns
Reference to the matrix.

## ◆ swap()

template<typename Type >
 void walberla::math::MatrixMxN< Type >::swap ( MatrixMxN< Type > & m )
inline

Swapping the contents of two matrices.

Parameters
 m The matrix to be swapped.
Returns
void
Exceptions
 no-throw guarantee.

## ◆ transpose()

template<typename Type >
 MatrixMxN< Type > & walberla::math::MatrixMxN< Type >::transpose
inline

Transposing the matrix.

Returns
Reference to the transposed matrix.

## ◆ capacity_

template<typename Type >
 size_t walberla::math::MatrixMxN< Type >::capacity_
private

The maximum capacity of the matrix.

## ◆ m_

template<typename Type >
 size_t walberla::math::MatrixMxN< Type >::m_
private

The current number of rows of the matrix.

## ◆ n_

template<typename Type >
 size_t walberla::math::MatrixMxN< Type >::n_
private

The current number of columns of the matrix.

## ◆ v_

template<typename Type >
 Type* WALBERLA_RESTRICT walberla::math::MatrixMxN< Type >::v_
private

The dynamically allocated matrix elements.

Access to the matrix elements is gained via the subscript or function call operator. The order of the elements is

$\left(\begin{array}{*{5}{c}} 0 & 1 & 2 & \cdots & N-1 \\ N & N+1 & N+2 & \cdots & 2 \cdot N-1 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ M \cdot N-N & M \cdot N-N+1 & M \cdot N-N+2 & \cdots & M \cdot N-1 \\ \end{array}\right)$

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