8 #ifndef META_OCEAN_MATH_MATRIX_H
9 #define META_OCEAN_MATH_MATRIX_H
18 template <
typename T>
class MatrixT;
314 template <MatrixProperty tMatrixProperty>
339 template <MatrixProperty tMatrixProperty>
632 explicit inline operator bool()
const;
650 void swapRows(
const size_t row0,
const size_t row1);
665 void addRows(
const size_t targetRow,
const size_t sourceRow,
const T scalar);
691 template <
typename T>
697 template <
typename T>
703 template <
typename T>
706 return rows_ * columns_;
709 template <
typename T>
712 ocean_assert(columns() == b.
rows());
713 ocean_assert(b.
columns() == 1);
716 return solve(b.
data(), x.
data(), matrixProperty);
719 template <
typename T>
720 template <typename MatrixT<T>::MatrixProperty tMatrixProperty>
723 ocean_assert(columns() == b.
rows());
724 ocean_assert(b.
columns() == 1);
727 return solve<tMatrixProperty>(b.
data(), x.
data());
730 template <
typename T>
733 switch (matrixProperty)
736 return solve<MP_SYMMETRIC>(b, x);
739 ocean_assert(matrixProperty == MP_UNKNOWN);
740 return solve<MP_UNKNOWN>(b, x);
744 template <
typename T>
747 return rank(data(), rows(), columns());
750 template <
typename T>
753 ocean_assert(rows_ == columns_);
759 ocean_assert(
false &&
"The matrix is a singular matrix.");
765 template <
typename T>
768 *
this = transposed();
771 template <
typename T>
776 for (
size_t n = 0u; n < elements(); ++n)
784 template <
typename T>
789 for (
size_t n = 0u; n < elements(); ++n)
791 result += values_[n];
797 template <
typename T>
803 template <
typename T>
809 template <
typename T>
815 template <
typename T>
818 return !(*
this == right);
821 template <
typename T>
824 *
this = *
this * right;
828 template <
typename T>
831 ocean_assert(row < rows());
832 return values_ + row * columns_;
835 template <
typename T>
838 ocean_assert(row < rows());
839 return values_ + row * columns_;
842 template <
typename T>
845 ocean_assert(row < rows_ && column < columns_);
847 return *(values_ + row * columns_ + column);
850 template <
typename T>
853 ocean_assert(row < rows_ && column < columns_);
855 return *(values_ + row * columns_ + column);
858 template <
typename T>
861 ocean_assert(index < elements());
862 return values_[index];
865 template <
typename T>
868 ocean_assert(index < elements());
869 return values_[index];
872 template <
typename T>
875 return values_ !=
nullptr;
MatrixT< T > subMatrix(const size_t row, const size_t column, const size_t rows, const size_t columns)
Returns a sub matrix of this one.
MatrixT(const size_t rows, const size_t columns, const T value)
Creates a new matrix with defined rows and columns.
~MatrixT()
Destructs a matrix and releases the elements.
T sum() const
Determines the sum of all elements of this matrix.
Definition: Matrix.h:785
MatrixT< T > & operator+=(const MatrixT< T > &right)
Adds and assigns two matrices.
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:751
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:843
MatrixT< T > diagonal() const
Returns a vector containing the values of the diagonal.
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:836
T Type
Definition of the used data type.
Definition: Matrix.h:69
MatrixT()=default
Creates a new matrix with no size.
T operator()(const size_t index) const
Element operator for the row aligned elements.
Definition: Matrix.h:859
size_t columns() const
Returns the count of columns.
Definition: Matrix.h:698
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:721
T & operator()(const size_t row, const size_t column)
Element operator allowing to access a specific elements of this matrix.
Definition: Matrix.h:851
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:829
bool operator!=(const MatrixT< T > &right) const
Returns whether two matrices are not identical up to a small epsilon.
Definition: Matrix.h:816
void elementwiseMultiplication(const MatrixT< T > &multiplier)
Performs an element-wise matrix multiplication meaning that each element of this matrix will be multi...
bool choleskyDecomposition(MatrixT< T > &lMatrix) const
Computes the Cholesky decomposition for this square matrix [m x m].
MatrixT< T > operator*(const T scalar) const
Multiplies the matrix with a scalar.
MatrixT< T > row(const size_t index) const
Returns a row of the matrix.
size_t rows_
Number of rows.
Definition: Matrix.h:682
void transpose()
Transposes this matrix.
Definition: Matrix.h:766
MatrixT< T > pseudoInverted(const T epsilon=NumericT< T >::eps()) const
Returns the pseudo inverse of this matrix by application of the singular value decomposition.
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.
MatrixT< T > selfSquareMatrix() const
Returns the matrix product of this matrix and the transposed matrix of this matrix.
const T * data() const
Returns a pointer to the internal values.
Definition: Matrix.h:798
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.
MatrixT< T > & operator=(MatrixT< T > &&matrix) noexcept
Move assign operator.
void addRows(const size_t targetRow, const size_t sourceRow, const T scalar)
Adds a multiple of a row to another one.
MatrixT< T > selfTransposedSquareMatrix() const
Returns the matrix product of transposed matrix of this matrix and this matrix.
MatrixT< T > transposedMultiply(const MatrixT< T > &right) const
Multiplies this transposed matrix with a second matrix.
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:745
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< T > transposed() const
Returns the transposed of this matrix.
size_t elements() const
Returns the number of entire elements, which is the product of rows and columns.
Definition: Matrix.h:704
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.
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.
T norm() const
Determines the L1 norm (sum of absolute elements) of this matrix.
Definition: Matrix.h:772
MatrixT< T > operator-(const MatrixT< T > &right) const
Subtracts two matrices.
MatrixT< T > operator*(const MatrixT< T > &right) const
Multiplies two matrices.
bool solve(const T *b, T *x, const MatrixProperty matrixProperty=MP_UNKNOWN) const
Solves the given linear system.
Definition: Matrix.h:731
T & operator()(const size_t index)
Element operator for the row aligned elements.
Definition: Matrix.h:866
size_t columns_
Number of columns.
Definition: Matrix.h:685
T * values_
Elements of the matrix.
Definition: Matrix.h:688
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...
MatrixT< T > & operator-=(const MatrixT< T > &right)
Subtracts and assigns two matrices.
bool solve(const T *b, T *x) const
Solves the given linear system.
MatrixT< T > operator+(const MatrixT< T > &right) const
Adds two matrices.
MatrixT< T > vector(const size_t column) const
Returns a column vector of the matrix.
MatrixProperty
Definition of specific properties of matrices.
Definition: Matrix.h:75
@ MP_UNKNOWN
No specific property known.
Definition: Matrix.h:77
@ MP_SYMMETRIC
The matrix is 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:810
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.
size_t rows() const
Returns the count of rows.
Definition: Matrix.h:692
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:822
T * data()
Returns a pointer to the internal values.
Definition: Matrix.h:804
bool solve(const MatrixT< T > &b, MatrixT< T > &x, const MatrixProperty matrixProperty=MP_UNKNOWN) const
Solves the given linear system.
Definition: Matrix.h:710
MatrixT< T > & operator*=(const T scalar)
Multiplies this matrix by a scalar.
This class provides basic numeric functionalities.
Definition: Numeric.h:57
static T abs(const T value)
Returns the absolute value of a given value.
Definition: Numeric.h:1220
MatrixT< Scalar > Matrix
Definition of the Matrix object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single ...
Definition: Matrix.h:18
MatrixT< float > MatrixF
Definition of the MatrixT template class using a single precision float data type.
Definition: Matrix.h:39
MatrixT< double > MatrixD
Definition of the MatrixT template class using a double precision float data type.
Definition: Matrix.h:32
std::vector< Matrix > Matrices
Definition of a vector holding matrix objects.
Definition: Matrix.h:46
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15