template class mrpt::math::MatrixBase
Overview
Base CRTP class for all MRPT matrices.
See MatrixVectorBase
See also:
#include <mrpt/math/MatrixBase.h> template <typename Scalar, class Derived> class MatrixBase: public mrpt::math::MatrixVectorBase { public: // typedefs typedef matrix_index_t Index_t; typedef matrix_dim_t size_type_t; // methods auto col(Index_t colIdx); auto col(Index_t colIdx) const; auto row(Index_t rowIdx); auto row(Index_t rowIdx) const; template <typename VectorLike> void extractRow( Index_t rowIdx, VectorLike& v ) const; template <typename VectorLike> VectorLike extractRow(Index_t rowIdx) const; template <typename VectorLike> void extractColumn( Index_t colIdx, VectorLike& v ) const; template <typename VectorLike> VectorLike extractColumn(Index_t colIdx) const; Scalar det() const; Derived inverse() const; Derived inverse_LLt() const; matrix_dim_t rank(Scalar threshold = 0) const; bool chol(Derived& U) const; bool eig(Derived& eVecs, std::vector<Scalar>& eVals, bool sorted = true) const; bool eig_symmetric(Derived& eVecs, std::vector<Scalar>& eVals, bool sorted = true) const; Scalar maximumDiagonal() const; Scalar minimumDiagonal() const; Scalar trace() const; void unsafeRemoveColumns(const std::vector<std::size_t>& idxs); void removeColumns(const std::vector<std::size_t>& idxsToRemove); void unsafeRemoveRows(const std::vector<std::size_t>& idxs); void removeRows(const std::vector<std::size_t>& idxsToRemove); template <typename OtherMatrixOrVector> void insertMatrix( const Index_t row_start, const Index_t col_start, const OtherMatrixOrVector& submat ); template <typename OtherMatrixOrVector> void insertMatrixTransposed( const Index_t row_start, const Index_t col_start, const OtherMatrixOrVector& submat ); template <size_type_t BLOCK_ROWS, size_type_t BLOCK_COLS> CMatrixFixed<Scalar, BLOCK_ROWS, BLOCK_COLS> blockCopy( Index_t start_row = 0, Index_t start_col = 0 ) const; CMatrixDynamic<Scalar> blockCopy(Index_t start_row, Index_t start_col, size_type_t BLOCK_ROWS, size_type_t BLOCK_COLS) const; template <size_type_t BLOCK_ROWS, size_type_t BLOCK_COLS> CMatrixFixed<Scalar, BLOCK_ROWS, BLOCK_COLS> extractMatrix( const Index_t start_row = 0, const Index_t start_col = 0 ) const; CMatrixDynamic<Scalar> extractMatrix( const size_type_t BLOCK_ROWS, const size_type_t BLOCK_COLS, const Index_t start_row, const Index_t start_col ) const; template <typename MAT_A> void matProductOf_AAt(const MAT_A& A); template <typename MAT_A> void matProductOf_AtA(const MAT_A& A); Derived& mbDerived(); const Derived& mbDerived() const; void setDiagonal(const size_type_t N, const Scalar value); void setDiagonal(const Scalar value); void setDiagonal(const std::vector<Scalar>& diags); void setIdentity(); void setIdentity(const size_type_t N); void matProductOf_AB(const Derived& A, const Derived& B); static Derived Identity(); static Derived Identity(const size_type_t N); }; // direct descendants template <class T> class CMatrixDynamic; template <typename T, matrix_dim_t ROWS, matrix_dim_t COLS> class CMatrixFixed;
Inherited Members
public: // methods void fill(const Scalar& val); void setConstant(const Scalar value); void setConstant(matrix_dim_t nrows, matrix_dim_t ncols, const Scalar value); void setConstant(matrix_dim_t nrows, const Scalar value); void assign(const matrix_dim_t N, const Scalar value); void setZero(); void setZero(matrix_dim_t nrows, matrix_dim_t ncols); void setZero(matrix_dim_t nrows); static Derived Constant(const Scalar value); static Derived Constant(matrix_dim_t nrows, matrix_dim_t ncols, const Scalar value); static Derived Zero(); static Derived Zero(matrix_dim_t nrows, matrix_dim_t ncols); template <matrix_dim_t BLOCK_ROWS, matrix_dim_t BLOCK_COLS> auto block( matrix_index_t start_row, matrix_index_t start_col ); auto block(matrix_index_t start_row, matrix_index_t start_col, matrix_dim_t BLOCK_ROWS, matrix_dim_t BLOCK_COLS); auto block(matrix_index_t start_row, matrix_index_t start_col, matrix_dim_t BLOCK_ROWS, matrix_dim_t BLOCK_COLS) const; auto transpose(); auto transpose() const; auto array(); auto array() const; auto operator - () const; template <typename S2, class D2> auto operator + (const MatrixVectorBase<S2, D2>& m2) const; template <typename S2, class D2> void operator += (const MatrixVectorBase<S2, D2>& m2); template <typename S2, class D2> auto operator - (const MatrixVectorBase<S2, D2>& m2) const; template <typename S2, class D2> void operator -= (const MatrixVectorBase<S2, D2>& m2); template <typename S2, class D2> auto operator * (const MatrixVectorBase<S2, D2>& m2) const; auto operator * (const Scalar s) const; template <matrix_dim_t N> CMatrixFixed<Scalar, N, 1> tail() const; template <matrix_dim_t N> CMatrixFixed<Scalar, N, 1> head() const; Scalar& coeffRef(matrix_index_t r, matrix_index_t c); const Scalar& coeff(matrix_index_t r, matrix_index_t c) const; Scalar minCoeff() const; Scalar minCoeff(matrix_index_t& outIndexOfMin) const; Scalar minCoeff(matrix_index_t& rowIdx, matrix_index_t& colIdx) const; Scalar maxCoeff() const; Scalar maxCoeff(matrix_index_t& outIndexOfMax) const; Scalar maxCoeff(matrix_index_t& rowIdx, matrix_index_t& colIdx) const; bool isSquare() const; bool empty() const; Scalar norm_inf() const; Scalar norm() const; void operator += (Scalar s); void operator -= (Scalar s); void operator *= (Scalar s); CMatrixDynamic<Scalar> operator * (const CMatrixDynamic<Scalar>& v); Derived operator + (const Derived& m2) const; void operator += (const Derived& m2); Derived operator - (const Derived& m2) const; void operator -= (const Derived& m2); Derived operator * (const Derived& m2) const; Scalar dot(const CVectorDynamic<Scalar>& v) const; Scalar dot(const MatrixVectorBase<Scalar, Derived>& v) const; void matProductOf_Ab(const CMatrixDynamic<Scalar>& A, const CVectorDynamic<Scalar>& b); void matProductOf_Atb(const CMatrixDynamic<Scalar>& A, const CVectorDynamic<Scalar>& b); Scalar sum() const; Scalar sum_abs() const; std::string asString() const; bool fromMatlabStringFormat(const std::string& s, mrpt::optional_ref<std::ostream> dump_errors_here = std::nullopt); std::string inMatlabFormat(const std::size_t decimal_digits = 6) const; void saveToTextFile( const std::string& file, mrpt::math::TMatrixTextFileFormat fileFormat = mrpt::math::MATRIX_FORMAT_ENG, bool appendMRPTHeader = false, const std::string& userHeader = std::string() ) const; void loadFromTextFile(std::istream& f); void loadFromTextFile(const std::string& file); template <typename OTHERMATVEC> bool operator == (const OTHERMATVEC& o) const; template <typename OTHERMATVEC> bool operator != (const OTHERMATVEC& o) const; Derived& mvbDerived(); const Derived& mvbDerived() const;
Methods
Scalar det() const
Determinant of matrix.
Derived inverse() const
Returns the inverse of a general matrix using LU.
Derived inverse_LLt() const
Returns the inverse of a symmetric matrix using LLt.
matrix_dim_t rank(Scalar threshold = 0) const
Finds the rank of the matrix via LU decomposition.
Uses Eigen’s default threshold unless threshold>0.
bool chol(Derived& U) const
Cholesky M=U T * U decomposition for symmetric matrix (upper-half of the matrix is actually ignored.
Returns:
false if Cholesky fails
bool eig(Derived& eVecs, std::vector<Scalar>& eVals, bool sorted = true) const
Computes the eigenvectors and eigenvalues for a square, general matrix.
Use eig_symmetric() for symmetric matrices for better accuracy and performance. Eigenvectors are the columns of the returned matrix, and their order matches that of returned eigenvalues.
Parameters:
sorted |
If true, eigenvalues (and eigenvectors) will be sorted in ascending order. |
eVecs |
The container where eigenvectors will be stored. |
eVals |
The container where eigenvalues will be stored. |
Returns:
false if eigenvalues could not be determined.
bool eig_symmetric( Derived& eVecs, std::vector<Scalar>& eVals, bool sorted = true ) const
Read: eig()
This only uses the lower-triangular part of the matrix
(Since MRPT 2.5.2) If sorted==true, the smallest eigenvalue is checked to be non-negative, and resetted to zero if found to be negative due to rounding errors in the internal Eigen3 routines.
Scalar maximumDiagonal() const
Returns the maximum value in the diagonal.
Scalar minimumDiagonal() const
Returns the minimum value in the diagonal.
Scalar trace() const
Returns the trace of the matrix (not necessarily square).
void unsafeRemoveColumns(const std::vector<std::size_t>& idxs)
Removes columns of the matrix.
This “unsafe” version assumes indices sorted in ascending order.
void removeColumns(const std::vector<std::size_t>& idxsToRemove)
Removes columns of the matrix.
Indices may be unsorted and duplicated
void unsafeRemoveRows(const std::vector<std::size_t>& idxs)
Removes rows of the matrix.
This “unsafe” version assumes indices sorted in ascending order.
void removeRows(const std::vector<std::size_t>& idxsToRemove)
Removes rows of the matrix.
Indices may be unsorted and duplicated
template <typename OtherMatrixOrVector> void insertMatrix( const Index_t row_start, const Index_t col_start, const OtherMatrixOrVector& submat )
Copies the given input submatrix/vector into this matrix/vector, starting at the given top-left coordinates.
template <typename OtherMatrixOrVector> void insertMatrixTransposed( const Index_t row_start, const Index_t col_start, const OtherMatrixOrVector& submat )
Like insertMatrix(), but inserts submat ` (transposed)
template <size_type_t BLOCK_ROWS, size_type_t BLOCK_COLS> CMatrixFixed<Scalar, BLOCK_ROWS, BLOCK_COLS> blockCopy( Index_t start_row = 0, Index_t start_col = 0 ) const
const blockCopy() : Returns a copy of the given block
CMatrixDynamic<Scalar> blockCopy(Index_t start_row, Index_t start_col, size_type_t BLOCK_ROWS, size_type_t BLOCK_COLS) const
const blockCopy() : Returns a copy of the given block (non templated version, dynamic sizes)
template <typename MAT_A> void matProductOf_AAt(const MAT_A& A)
this = A * A T
template <typename MAT_A> void matProductOf_AtA(const MAT_A& A)
this = A T * A
void setDiagonal(const size_type_t N, const Scalar value)
Resize to NxN, set all entries to zero, except the main diagonal which is set to value
void setDiagonal(const Scalar value)
Set all entries to zero, except the main diagonal which is set to value
void setDiagonal(const std::vector<Scalar>& diags)
Resizes to NxN, with N the length of the input vector, set all entries to zero, except the main diagonal which is set to values in the vector.
void matProductOf_AB(const Derived& A, const Derived& B)
this = A*B, with A & B of the same type of this.
For products of different matrix types, use the regular * operator (which requires the <Eigen/Dense> header)