8#ifndef META_OCEAN_MATH_MATRIX_H
9#define META_OCEAN_MATH_MATRIX_H
18template <
typename T>
class MatrixT;
314 template <MatrixProperty tMatrixProperty>
339 template <MatrixProperty tMatrixProperty>
643 explicit inline operator bool()
const;
661 void swapRows(
const size_t row0,
const size_t row1);
676 void addRows(
const size_t targetRow,
const size_t sourceRow,
const T scalar);
724 ocean_assert(b.
columns() == 1);
731template <typename MatrixT<T>::MatrixProperty tMatrixProperty>
735 ocean_assert(b.
columns() == 1);
744 switch (matrixProperty)
771 ocean_assert(
false &&
"The matrix is a singular matrix.");
788 for (
size_t n = 0u; n <
elements(); ++n)
801 for (
size_t n = 0u; n <
elements(); ++n)
848 return !(*
this == right);
854 *
this = *
this * right;
This class implements a matrix with arbitrary size.
Definition Matrix.h:63
MatrixT< T > operator-(const MatrixT< T > &right) const
Subtracts two matrices.
MatrixT(const size_t rows, const size_t columns, const T value)
Creates a new matrix with defined rows and columns.
MatrixT< T > selfSquareMatrix() const
Returns the matrix product of this matrix and the transposed matrix of this matrix.
~MatrixT()
Destructs a matrix and releases the elements.
T sum() const
Determines the sum of all elements of this matrix.
Definition Matrix.h:797
MatrixT< T > transposedMultiply(const MatrixT< T > &right) const
Multiplies this transposed matrix with a second matrix.
void selfTransposedSquareMatrix(MatrixT< T > &result) const
Returns the matrix product of transposed matrix of this matrix and this matrix.
void weightedSelfTransposedSquareMatrix(const MatrixT< T > &weightDiagonal, MatrixT< T > &result) const
Returns the matrix product of transposed matrix of this matrix and this matrix and applies a further ...
MatrixT(const size_t rows, const size_t columns, const T *source)
Creates a new matrix with defined rows and columns.
MatrixT< T > inverted() const
Returns the inverted of this matrix.
Definition Matrix.h:763
T operator()(const size_t row, const size_t column) const
Element operator allowing to access a specific elements of this matrix.
Definition Matrix.h:873
static size_t rank(const T *data, const size_t rows, const size_t columns)
Computes the rank of given matrix data.
T * operator[](const size_t row)
Element operator for the row aligned elements.
Definition Matrix.h:866
MatrixT()=default
Creates a new matrix with no size.
MatrixT< T > pseudoInverted(const T epsilon=NumericT< T >::eps()) const
Returns the pseudo inverse of this matrix by application of the singular value decomposition.
T operator()(const size_t index) const
Element operator for the row aligned elements.
Definition Matrix.h:889
MatrixT< Scalar > subMatrix(const size_t row, const size_t column, const size_t rows, const size_t columns)
void multiplyRow(const size_t row, const T scalar)
Multiplies a row with a scalar.
bool solve(const MatrixT< T > &b, MatrixT< T > &x) const
Solves the given linear system.
Definition Matrix.h:732
T & operator()(const size_t row, const size_t column)
Element operator allowing to access a specific elements of this matrix.
Definition Matrix.h:881
MatrixT< T > vector(const size_t column) const
Returns a column vector of the matrix.
bool nonNegativeMatrixFactorization(MatrixT< T > &subcomponents, MatrixT< T > &weights, const size_t components=0u, const unsigned int iterations=100u, const T convergenceThreshold=T(0.0001)) const
Performs a non-negative matrix factorization with multiplicative update rules V = W * H,...
const T * operator[](const size_t row) const
Returns the pointer to the elements of a specified row.
Definition Matrix.h:859
bool operator!=(const MatrixT< T > &right) const
Returns whether two matrices are not identical up to a small epsilon.
Definition Matrix.h:846
void elementwiseMultiplication(const MatrixT< T > &multiplier)
Performs an element-wise matrix multiplication meaning that each element of this matrix will be multi...
bool isValid() const
Returns whether this matrix holds at least one element.
Definition Matrix.h:820
bool choleskyDecomposition(MatrixT< T > &lMatrix) const
Computes the Cholesky decomposition for this square matrix [m x m].
MatrixT< T > & operator-=(const MatrixT< T > &right)
Subtracts and assigns two matrices.
MatrixT< T > & operator*=(const T scalar)
Multiplies this matrix by a scalar.
size_t rows_
Definition Matrix.h:693
void transpose()
Transposes this matrix.
Definition Matrix.h:778
void multiplyColumn(const size_t column, const T scalar)
Multiplies a column with a scalar.
bool qrDecomposition(MatrixT< T > &qMatrix, MatrixT< T > *rMatrix=nullptr) const
Computes the QR decomposition for this matrix [m x n] while m >= n must hold.
void swapRows(const size_t row0, const size_t row1)
Swaps two rows.
MatrixT(const size_t rows, const size_t columns, const MatrixT< T > &subMatrix, const size_t row, const size_t column, const T value=T(0.0))
Creates a new matrix with defined rows and columns and a given sub-matrix.
const T * data() const
Returns a pointer to the internal values.
Definition Matrix.h:828
bool selfSquareDiagonalMatrixMultiply(const MatrixT< T > &weightDiagonal, const MatrixT< T > &right, MatrixT< T > &result) const
Interprets this matrix as diagonal matrix and multiplies a second matrix on the right of the interpre...
MatrixT(const size_t rows, const size_t columns, const T *source, const bool valuesRowAligned)
Creates a new matrix with defined rows and columns.
void addRows(const size_t targetRow, const size_t sourceRow, const T scalar)
Adds a multiple of a row to another one.
MatrixT(const size_t rows, const size_t columns)
Creates a new matrix with defined rows and columns.
size_t rank() const
Computes the rank of this matrix.
Definition Matrix.h:757
MatrixT< T > operator+(const MatrixT< T > &right) const
Adds two matrices.
void transposedMultiply(const MatrixT< T > &right, MatrixT< T > &result) const
Multiplies this transposed matrix with a second matrix.
MatrixT(const MatrixT< T > &matrix)
Copies a matrix.
MatrixT< Scalar > row(const size_t index) const
Scalar Type
Definition Matrix.h:69
MatrixT< T > selfTransposedSquareMatrix() const
Returns the matrix product of transposed matrix of this matrix and this matrix.
size_t elements() const
Returns the number of entire elements, which is the product of rows and columns.
Definition Matrix.h:715
bool isSymmetric(const T eps=NumericT< T >::eps()) const
Returns whether this matrix is symmetric (and whether this matrix is a square matrix).
bool isEqual(const MatrixT< T > &matrix, const T eps=NumericT< T >::eps()) const
Returns whether two matrices are almost identical up to a specified epsilon.
MatrixT< Scalar > diagonal() const
bool invert()
Inverts this matrix.
bool singularValueDecomposition(MatrixT< T > &u, MatrixT< T > &w, MatrixT< T > &v) const
Computes the singular value decomposition for this matrix.
bool eigenSystem(MatrixT< T > &values, MatrixT< T > &vectors) const
Computes the eigen system of this matrix.
void elementwiseDivision(const MatrixT< T > &denominator)
Performs an element-wise matrix division meaning that each element of this matrix will be divided by ...
MatrixT(MatrixT< T > &&matrix) noexcept
Move constructor.
MatrixT< T > operator*(const MatrixT< T > &right) const
Multiplies two matrices.
T norm() const
Determines the L1 norm (sum of absolute elements) of this matrix.
Definition Matrix.h:784
MatrixT< T > operator*(const T scalar) const
Multiplies the matrix with a scalar.
bool solve(const T *b, T *x, const MatrixProperty matrixProperty=MP_UNKNOWN) const
Solves the given linear system.
Definition Matrix.h:742
T & operator()(const size_t index)
Element operator for the row aligned elements.
Definition Matrix.h:896
MatrixT< T > & operator+=(const MatrixT< T > &right)
Adds and assigns two matrices.
size_t columns_
Definition Matrix.h:696
Scalar * values_
Definition Matrix.h:699
MatrixT(const size_t rows, const size_t columns, const MatrixT< T > &diagonal)
Creates a new matrix with defined rows and columns and initializes the diagonal with small sub matric...
bool solve(const T *b, T *x) const
Solves the given linear system.
MatrixProperty
Definition of specific properties of matrices.
Definition Matrix.h:75
@ MP_UNKNOWN
Definition Matrix.h:77
@ MP_SYMMETRIC
Definition Matrix.h:79
void swapColumns(const size_t column0, const size_t column1)
Swaps two columns.
bool operator==(const MatrixT< T > &right) const
Returns whether two matrices are identical up to a small epsilon.
Definition Matrix.h:840
MatrixT< T > & operator=(const MatrixT< T > &matrix)
Assigns a matrix to this one.
MatrixT(const size_t rows, const size_t columns, bool toIdentity)
Creates a new matrix with defined rows and columns.
MatrixT< T > transposed() const
Returns the transposed of this matrix.
void release()
Releases the matrix and removes all elements.
Definition Matrix.h:810
void resize(const size_t rows, const size_t columns)
Resizes this matrix.
bool selfSquareDiagonalMatrixMultiply(const MatrixT< T > &right, MatrixT< T > &result) const
Interprets this matrix as diagonal matrix and multiplies a second matrix on the right of the interpre...
MatrixT< T > & operator*=(const MatrixT< T > &right)
Multiplies and assigns two matrices.
Definition Matrix.h:852
T * data()
Returns a pointer to the internal values.
Definition Matrix.h:834
MatrixT< T > & operator=(MatrixT< T > &&matrix) noexcept
Move assign operator.
bool solve(const MatrixT< T > &b, MatrixT< T > &x, const MatrixProperty matrixProperty=MP_UNKNOWN) const
Solves the given linear system.
Definition Matrix.h:721
static T abs(const T value)
Returns the absolute value of a given value.
Definition Numeric.h:1220
static constexpr T eps()
Returns a small epsilon.
MatrixT< Scalar > Matrix
Definition of the Matrix object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single ...
Definition Matrix.h:25
std::vector< Matrix > Matrices
Definition of a vector holding matrix objects.
Definition Matrix.h:46
MatrixT< double > MatrixD
Definition of the MatrixT template class using a double precision float data type.
Definition Matrix.h:32
MatrixT< float > MatrixF
Definition of the MatrixT template class using a single precision float data type.
Definition Matrix.h:39
The namespace covering the entire Ocean framework.
Definition Accessor.h:15