CTL  0.6.1
Computed Tomography Library
Public Member Functions | Static Public Member Functions | Related Functions | List of all members
CTL::mat::Matrix< Rows, Cols > Class Template Reference

This template class is an abstraction of a small matrix with a size known at compile time. More...

#include <matrix.h>

Inheritance diagram for CTL::mat::Matrix< Rows, Cols >:
Inheritance graph
[legend]
Collaboration diagram for CTL::mat::Matrix< Rows, Cols >:
Collaboration graph
[legend]

Public Member Functions

 Matrix (double fillValue)
 
 Matrix (const double(&initArray)[Rows *Cols])
 
template<typename... Doubles, typename = typename std::enable_if<sizeof...(Doubles) + 1u == Rows * Cols>::type>
 Matrix (double firstElement, Doubles... matrixElements)
 
template<uint fromRow, uint toRow, uint fromCol, uint toCol>
auto subMat () const -> Matrix< details::rangeDim(fromRow, toRow), details::rangeDim(fromCol, toCol)>
 
template<uint from, uint to>
auto subMat () const -> Matrix< details::vecRangeDim< Rows >(from, to), details::vecRangeDim< Cols >(from, to)>
 
template<uint i>
Matrix< 1, Cols > row () const
 
template<uint j>
Matrix< Rows, 1 > column () const
 
void fill (double fillValue)
 
void normalize ()
 
Matrix< Rows, Cols > normalized () const
 
double trace () const
 
Matrix< Cols, Rows > transposed () const
 
template<uint NewRows, uint NewCols>
Matrix< NewRows, NewCols > reshaped () const
 
Matrix< Rows *Cols, 1 > reshapedAsVector () const
 
Matrix< Rows, Cols > operator- () const
 
Matrix< Rows, Cols > & operator *= (double scalar)
 
Matrix< Rows, Cols > & operator/= (double scalar)
 
Matrix< Rows, Cols > & operator+= (const Matrix< Rows, Cols > &rhs)
 
Matrix< Rows, Cols > & operator-= (const Matrix< Rows, Cols > &rhs)
 
Matrix< Rows, Cols > & operator%= (const Matrix< Rows, Cols > &rhs)
 
Matrix< Rows, Cols > & operator|= (const Matrix< Rows, Cols > &rhs)
 
Matrix< Rows, Cols > operator * (double scalar) const
 
Matrix< Rows, Cols > operator/ (double scalar) const
 
Matrix< Rows, Cols > operator+ (const Matrix< Rows, Cols > &rhs) const
 
Matrix< Rows, Cols > operator- (const Matrix< Rows, Cols > &rhs) const
 
template<uint Cols2>
Matrix< Rows, Cols2 > operator * (const Matrix< Cols, Cols2 > &rhs) const
 
Matrix< Rows, Cols > operator% (const Matrix< Rows, Cols > &rhs) const
 
Matrix< Rows, Cols > operator| (const Matrix< Rows, Cols > &rhs) const
 
- Public Member Functions inherited from CTL::mat::MatrixBase< Rows, Cols >
 MatrixBase (double fillValue)
 
 MatrixBase (const double(&initArray)[Rows *Cols])
 
template<typename... Doubles>
 MatrixBase (double firstElement, Doubles... matrixElements)
 
double * operator[] (uint row)
 
const double * operator[] (uint row) const
 
double & operator() (uint row, uint column)
 
double operator() (uint row, uint column) const
 
double & at (uint row, uint column) noexcept(false)
 
double at (uint row, uint column) const noexcept(false)
 
template<uint row, uint column>
double & get () noexcept
 
template<uint row, uint column>
double get () const noexcept
 
double & operator() (uint n)
 
double operator() (uint n) const
 
double & at (uint n)
 
double at (uint n) const
 
template<uint n>
double & get () noexcept
 
template<uint n>
double get () const noexcept
 
double * data ()
 
const double * data () const
 
const double * constData () const
 
double * begin ()
 
const double * begin () const
 
const double * constBegin () const
 
double * end ()
 
const double * end () const
 
const double * constEnd () const
 
constexpr size_t size () const
 
std::string info (const char *lineModifier="") const
 
double norm () const
 
bool operator== (const MatrixBase< Rows, Cols > &rhs) const
 
bool operator!= (const MatrixBase< Rows, Cols > &rhs) const
 

Static Public Member Functions

template<class Container >
static Matrix< Rows, Cols > fromContainer (const Container &vector, size_t NthMat, bool *ok=nullptr)
 

Related Functions

(Note that these are not member functions.)

template<uint Rows, uint Cols>
Matrix< Rows, Cols > operator * (double scalar, const Matrix< Rows, Cols > &rhs)
 
template<uint N>
double dot (const Matrix< N, 1 > &vector1, const Matrix< N, 1 > &vector2)
 
template<uint N>
double dot (const Matrix< 1, N > &vector1, const Matrix< 1, N > &vector2)
 
template<uint N>
double dot (const Matrix< N, 1 > &vector1, const Matrix< 1, N > &vector2)
 
template<uint N>
double dot (const Matrix< 1, N > &vector1, const Matrix< N, 1 > &vector2)
 
template<uint N>
Matrix< N, N > diag (const Matrix< N, 1 > &diagElements)
 
template<uint Rows, uint Cols>
Matrix< Rows, Cols > diag (const Matrix< details::minDim(Rows, Cols), 1 > &diagElements)
 
template<uint Rows, uint Cols>
auto diag (const Matrix< Rows, Cols > &m) -> Matrix< details::minDim(Rows, Cols), 1 >
 
template<uint N>
Matrix< N, N > eye ()
 
template<uint Rows, uint Cols>
Matrix< Rows, Cols > eye ()
 
template<uint Rows, uint Cols1, uint Cols2>
Matrix< Rows, Cols1+Cols2 > horzcat (const Matrix< Rows, Cols1 > &m1, const Matrix< Rows, Cols2 > &m2)
 
template<uint Rows1, uint Rows2, uint Cols>
Matrix< Rows1+Rows2, Cols > vertcat (const Matrix< Rows1, Cols > &m1, const Matrix< Rows2, Cols > &m2)
 

Additional Inherited Members

- Static Public Attributes inherited from CTL::mat::MatrixBase< Rows, Cols >
static char & SEPARATOR_CHARACTER_FOR_INFO_STRING = details::SeparatorCharacter<>::c
 global separator character for formatting
 

Detailed Description

template<uint Rows, uint Cols>
class CTL::mat::Matrix< Rows, Cols >

This template class is an abstraction of a small matrix with a size known at compile time.

Basic algebraic operations are provided and dimension checks are carried out during compilation. No heap allocation is performed. Elements are stored in row major order.

Constructor & Destructor Documentation

◆ Matrix() [1/3]

template<uint Rows, uint Cols>
CTL::mat::Matrix< Rows, Cols >::Matrix ( double  fillValue)
explicit

Construct an instance and initialize all elements with a fillValue.

// 4x4 matrix where each element equals one:
Matrix<4, 4> M{ 1.0 };

◆ Matrix() [2/3]

template<uint Rows, uint Cols>
CTL::mat::Matrix< Rows, Cols >::Matrix ( const double(&)  initArray[Rows *Cols])
inlineexplicit

Construct an instance from a C-style array. The array length must match the total number of matrix elements, otherwise it results in a compilation error.

double ar[4] = { 1., 2., 3., 4. };
Matrix<2, 2> M{ ar };

◆ Matrix() [3/3]

template<uint Rows, uint Cols>
template<typename... Doubles, typename >
CTL::mat::Matrix< Rows, Cols >::Matrix ( double  firstElement,
Doubles...  matrixElements 
)
inline

Construct an instance from a list of arguments that specifies each element in row major order. The length of the argument list must match the total number of matrix elements, otherwise it results in a compilation error.

Matrix<3, 3> M{ 1.1, 1.2, 1.3,
2.1, 2.2, 2.3,
3.1, 3.2, 3.3 };

Member Function Documentation

◆ column()

template<uint Rows, uint Cols>
template<uint j>
Matrix< Rows, 1 > CTL::mat::Matrix< Rows, Cols >::column ( ) const

Returns the j'th column of the matrix. Performes compile time boundary check.

◆ fill()

template<uint Rows, uint Cols>
void CTL::mat::Matrix< Rows, Cols >::fill ( double  fillValue)

Sets all elements to fillValue.

◆ fromContainer()

template<uint Rows, uint Cols>
template<class Container >
Matrix< Rows, Cols > CTL::mat::Matrix< Rows, Cols >::fromContainer ( const Container &  vector,
size_t  NthMat,
bool *  ok = nullptr 
)
static

Returns a Rows x Cols matrix that is extracted from a vector that stores these matrices in row major order. The NthMat matrix is read from the container, meaning that NthMat*Rows*Cols elements are skipped. Optionally, a pointer ok to a boolean can be passed to check if the reading was successful. It is set to false if the vector size would be exceeded. In this case it returns a zero-initialized matrix.

◆ normalize()

template<uint Rows, uint Cols>
void CTL::mat::Matrix< Rows, Cols >::normalize ( )

Normalizes the current instance of a vector in place by dividing the elements by its Euclidian norm. This transforms the vector into a unit vector. If the macro ENABLE_FROBENIUS_NORM has been defined before including the matrix.h header this function can be applied for arbitrary matrices (not only vectors). In this case, the matrix is normalized by its Frobenius norm.

◆ normalized()

template<uint Rows, uint Cols>
Matrix< Rows, Cols > CTL::mat::Matrix< Rows, Cols >::normalized ( ) const

Returns a (unit) vector (or Matrix if Frobenius norm is enables) that is normalized by its Euclidian norm (or Frobenius norm).

To enable the Frobenius norm, ENABLE_FROBENIUS_NORM has to be defined before including the matrix.h header.

See also
normalize()

◆ operator *()

template<uint Rows, uint Cols>
template<uint Cols2>
Matrix< Rows, Cols2 > CTL::mat::Matrix< Rows, Cols >::operator * ( const Matrix< Cols, Cols2 > &  rhs) const

Returns the result of a standard matrix multiplication.

◆ operator%()

template<uint Rows, uint Cols>
Matrix< Rows, Cols > CTL::mat::Matrix< Rows, Cols >::operator% ( const Matrix< Rows, Cols > &  rhs) const

Returns the result of a the Hadamard product, i.e. the element-wise multiplication by corresponding elements of the rhs, which results in a matrix with same dimensions.

◆ operator%=()

template<uint Rows, uint Cols>
Matrix< Rows, Cols > & CTL::mat::Matrix< Rows, Cols >::operator%= ( const Matrix< Rows, Cols > &  rhs)

Performs the Hadamard product with rhs and assigns the result to the instance.

◆ operator|()

template<uint Rows, uint Cols>
Matrix< Rows, Cols > CTL::mat::Matrix< Rows, Cols >::operator| ( const Matrix< Rows, Cols > &  rhs) const

Returns the result of a the Hadamard division, i.e. the element-wise division by corresponding elements of the rhs, which results in a matrix with same dimensions.

◆ operator|=()

template<uint Rows, uint Cols>
Matrix< Rows, Cols > & CTL::mat::Matrix< Rows, Cols >::operator|= ( const Matrix< Rows, Cols > &  rhs)

Performs the Hadamard division with rhs and assigns the result to the instance.

◆ reshaped()

template<uint Rows, uint Cols>
template<uint NewRows, uint NewCols>
Matrix< NewRows, NewCols > CTL::mat::Matrix< Rows, Cols >::reshaped ( ) const

Returns a reshaped version of the matrix with the new size NewRows X NewCols. The order of elements in the internal array is retained. The returned matrix must contain the same number of elements, otherwise it results in a compiler error.

See also
reshapedAsVector()

◆ reshapedAsVector()

template<uint Rows, uint Cols>
Matrix< Rows *Cols, 1 > CTL::mat::Matrix< Rows, Cols >::reshapedAsVector ( ) const

Returns a reshaped version of the matrix with the new size NewRows*NewCols X 1, i.e. a column vector. The order of elements in the internal array is retained.

See also
reshaped()

◆ row()

template<uint Rows, uint Cols>
template<uint i>
Matrix< 1, Cols > CTL::mat::Matrix< Rows, Cols >::row ( ) const

Returns the i'th row of the matrix. Performes compile time boundary check.

◆ subMat() [1/2]

template<uint Rows, uint Cols>
template<uint fromRow, uint toRow, uint fromCol, uint toCol>
auto CTL::mat::Matrix< Rows, Cols >::subMat ( ) const -> Matrix<details::rangeDim(fromRow, toRow), details::rangeDim(fromCol, toCol)>

subMat<fromRow, toRow, fromCol, toCol>() returns a submatrix specified by the boundary indices for the rows (fromRow, toRow) and for the columns (fromCol, toCol). For instance M.subMat<0,1, 1,1>() will return the first to elements of the second column of M as a 2x1 matrix (vector) if the dimensions of the matrix M are at least 2x2, otherwise (in case the dimensions are exceeded) it results in a compiler error. Except the size of the matrix from which is extracted, there are no further limitation for choosing the boundaries (the template arguments). The dimension of the returned matrix is abs(toRow-fromRow+1) x abs(toCol-fromCol+1). If a 'from' index is larger than a 'to' index, the submatrix is extracted in reverse order, e.g. M.subMat<1,0, 0,1>() returns the upper left 2x2 submatrix with its rows flipped in the up-down direction.

// Example for extracting a single element from a 2x2 matrix (bottom right corner).
const Matrix<2, 2> mat{ 11., 12.,
21., 22. };
bool test = (mat.subMat<1,1, 1,1>() == 22.); // test is `true`

◆ subMat() [2/2]

template<uint Rows, uint Cols>
template<uint from, uint to>
auto CTL::mat::Matrix< Rows, Cols >::subMat ( ) const -> Matrix<details::vecRangeDim<Rows>(from, to), details::vecRangeDim<Cols>(from, to)>

This subvector extraction is a simplified overload of subMat<fromRow, toRow, fromCol, toCol>() that is more handy to use for vectors. The behavior of subMat<from, to>() is equivalent to subMat<from, to, 0, 0>() for column vectors or subMat<0, 0, from, to>() for row vectors.

◆ trace()

template<uint Rows, uint Cols>
double CTL::mat::Matrix< Rows, Cols >::trace ( ) const

Returns the sum of elements on the main diagonal.

◆ transposed()

template<uint Rows, uint Cols>
Matrix< Cols, Rows > CTL::mat::Matrix< Rows, Cols >::transposed ( ) const

Returns the transposed matrix.

Friends And Related Function Documentation

◆ diag() [1/3]

template<uint N>
Matrix< N, N > diag ( const Matrix< N, 1 > &  diagElements)
related

Returns a diagonal N x N matrix with diagElements on the main diagonal.

◆ diag() [2/3]

template<uint Rows, uint Cols>
Matrix< Rows, Cols > diag ( const Matrix< details::minDim(Rows, Cols), 1 > &  diagElements)
related

Returns a diagonal Rows x Cols matrix with diagElements on the main diagonal.

◆ diag() [3/3]

template<uint Rows, uint Cols>
auto diag ( const Matrix< Rows, Cols > &  m) -> Matrix<details::minDim(Rows, Cols), 1>
related

Extracts and returns the main diagonal elements of m as a column vector.

◆ dot() [1/4]

template<uint N>
double dot ( const Matrix< N, 1 > &  vector1,
const Matrix< N, 1 > &  vector2 
)
related

Returns the dot product (scalar product) of the vectors vector1 and vector2.

◆ dot() [2/4]

template<uint N>
double dot ( const Matrix< 1, N > &  vector1,
const Matrix< 1, N > &  vector2 
)
related

Returns the dot product (scalar product) of the vectors vector1 and vector2.

◆ dot() [3/4]

template<uint N>
double dot ( const Matrix< N, 1 > &  vector1,
const Matrix< 1, N > &  vector2 
)
related

Returns the dot product (scalar product) of the vectors vector1 and vector2.

◆ dot() [4/4]

template<uint N>
double dot ( const Matrix< 1, N > &  vector1,
const Matrix< N, 1 > &  vector2 
)
related

Returns the dot product (scalar product) of the vectors vector1 and vector2.

◆ eye() [1/2]

template<uint N>
Matrix< N, N > eye ( )
related

Returns an N x N identity matrix.

◆ eye() [2/2]

template<uint Rows, uint Cols>
Matrix< Rows, Cols > eye ( )
related

Returns a Rows x Cols matrix with ones on the main diagonal and zeros elsewhere.

◆ horzcat()

template<uint Rows, uint Cols1, uint Cols2>
Matrix< Rows, Cols1+Cols2 > horzcat ( const Matrix< Rows, Cols1 > &  m1,
const Matrix< Rows, Cols2 > &  m2 
)
related

Returns a (Rows) x (Cols1 + Cols2) matrix that is a horizontal concatenation of m1 and m2.

◆ operator *()

template<uint Rows, uint Cols>
Matrix< Rows, Cols > operator * ( double  scalar,
const Matrix< Rows, Cols > &  rhs 
)
related

Returns the result of a scalar multiplication of scalar with the Matrix rhs.

◆ vertcat()

template<uint Rows1, uint Rows2, uint Cols>
Matrix< Rows1+Rows2, Cols > vertcat ( const Matrix< Rows1, Cols > &  m1,
const Matrix< Rows2, Cols > &  m2 
)
related

Returns a (Rows1 + Rows2) x (Cols) matrix that is a vertical concatenation of m1 and m2.


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