template class mrpt::math::CMatrixDynamic
Overview
This template class provides the basic functionality for a general 2D any-size, resizable container of numerical or non-numerical elements.
Notes:
This class is not serializable since it is a template. For using serialization, see mrpt::math::CMatrixD.
First row or column index is “0”.
This class includes range checks with ASSERT_() if compiling with “_DEBUG” or “MRPT_ALWAYS_CHECKS_DEBUG_MATRICES=1”.
Use asEigen() to get an
Eigen::Map<>object and to access full Algebra functionality.
See also:
#include <mrpt/math/CMatrixDynamic.h> template <class T> class CMatrixDynamic: public mrpt::math::MatrixBase { public: // typedefs typedef T value_type; typedef T Scalar; typedef matrix_index_t Index; typedef T& reference; typedef const T& const_reference; typedef matrix_dim_t size_type; typedef std::ptrdiff_t difference_type; typedef Eigen::Matrix<T, RowsAtCompileTime, ColsAtCompileTime, StorageOrder, RowsAtCompileTime, ColsAtCompileTime> eigen_t; typedef typename vec_t::iterator iterator; typedef typename vec_t::const_iterator const_iterator; // fields static constexpr static int RowsAtCompileTime = -1; static constexpr static int ColsAtCompileTime = -1; static constexpr static int SizeAtCompileTime = -1; static constexpr static int is_mrpt_type = 1; static constexpr static int StorageOrder = 1; // construction CMatrixDynamic(const CMatrixDynamic& m); CMatrixDynamic( size_type row = 0, size_type col = 0 ); template <typename U> CMatrixDynamic(const CMatrixDynamic<U>& m); template <class Derived> CMatrixDynamic(const Eigen::MatrixBase<Derived>& m); template <typename LHS, typename RHS, int Option> CMatrixDynamic(const Eigen::Product<LHS, RHS, Option>& p); template <typename Op, typename Lhs, typename Rhs> CMatrixDynamic(const Eigen::CwiseBinaryOp<Op, Lhs, Rhs>& p); CMatrixDynamic(const CMatrixDynamic& m, size_type cropRowCount, size_type cropColCount); template <size_type ROWS, size_type COLS> CMatrixDynamic(const CMatrixFixed<T, ROWS, COLS>& o); template <typename V, std::size_t N> CMatrixDynamic(size_type row, size_type col, V(&) theArray [N]); template <typename V> CMatrixDynamic(size_type row, size_type col, const V& theVector); CMatrixDynamic(CMatrixDynamic&& m); // methods iterator begin(); iterator end(); const_iterator begin() const; const_iterator end() const; const_iterator cbegin() const; const_iterator cend() const; void swap(CMatrixDynamic<T>& o); template <class MAT> void setFromMatrixLike(const MAT& m); CMatrixDynamic& operator = (const CMatrixDynamic<T>& m); template <typename U> CMatrixDynamic& operator = (const CMatrixDynamic<U>& m); template <class Derived> CMatrixDynamic& operator = (const Eigen::MatrixBase<Derived>& m); template <size_type ROWS, size_type COLS> CMatrixDynamic& operator = (const CMatrixFixed<T, ROWS, COLS>& m); template <typename V, size_t N> CMatrixDynamic& operator = (V(&) theArray [N]); CMatrixDynamic& operator = (CMatrixDynamic&& m); size_type rows() const; size_type cols() const; matrix_size_t size() const; void setSize(size_type row, size_type col, bool zeroNewElements = false); void resize( size_type row, size_type col ); void resize(size_type vectorLen); void resize(const matrix_size_t& siz, bool zeroNewElements = false); CMatrixDynamic& derived(); const CMatrixDynamic& derived() const; void conservativeResize( size_type row, size_type col ); const T* data() const; T* data(); T& operator () (Index row, Index col); const T& operator () (Index row, Index col) const; T& operator [] (Index ith); const T& operator [] (Index ith) const; template <typename VECTOR> void appendRow(const VECTOR& in); template <typename VECTOR> void setRow( const Index row, const VECTOR& v ); template <typename VECTOR> void setCol( const Index col, const VECTOR& v ); template <typename VECTOR> void appendCol(const VECTOR& in); template <typename VECTOR> void asVector(VECTOR& out) const; template < typename EIGEN_MATRIX = eigen_t, typename EIGEN_MAP = Eigen::Map<EIGEN_MATRIX, MRPT_MAX_ALIGN_BYTES, Eigen::InnerStride<1>> > EIGEN_MAP asEigen(); template < typename EIGEN_MATRIX = eigen_t, typename EIGEN_MAP = Eigen::Map<const EIGEN_MATRIX, MRPT_MAX_ALIGN_BYTES, Eigen::InnerStride<1>> > EIGEN_MAP asEigen() const; CMatrixDynamic<float> cast_float() const; CMatrixDynamic<double> cast_double() const; CVectorDynamic<Scalar> llt_solve(const CVectorDynamic<Scalar>& b) const; CVectorDynamic<Scalar> lu_solve(const CVectorDynamic<Scalar>& b) const; }; // direct descendants class CMatrixB; class CMatrixF;
Inherited Members
public: // typedefs typedef matrix_index_t Index_t; typedef matrix_dim_t size_type_t; // 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; 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);
Typedefs
typedef T value_type
The type of the matrix elements.
Construction
CMatrixDynamic(const CMatrixDynamic& m)
Constructors.
template <typename U> CMatrixDynamic(const CMatrixDynamic<U>& m)
Copy (casting from if needed) from another matrix
template <class Derived> CMatrixDynamic(const Eigen::MatrixBase<Derived>& m)
Convert from Eigen matrix.
template <typename LHS, typename RHS, int Option> CMatrixDynamic(const Eigen::Product<LHS, RHS, Option>& p)
Convert from Eigen product.
template <typename Op, typename Lhs, typename Rhs> CMatrixDynamic(const Eigen::CwiseBinaryOp<Op, Lhs, Rhs>& p)
Convert from Eigen binary op.
CMatrixDynamic(const CMatrixDynamic& m, size_type cropRowCount, size_type cropColCount)
Copy constructor & crop from another matrix.
template <size_type ROWS, size_type COLS> CMatrixDynamic(const CMatrixFixed<T, ROWS, COLS>& o)
Constructor from fixed-size matrix:
template <typename V, std::size_t N> CMatrixDynamic( size_type row, size_type col, V(&) theArray [N] )
Constructor from a given size and a C array.
The array length must match cols x row.
const double numbers[] = { 1,2,3, 4,5,6 }; CMatrixDouble M(3,2, numbers);
template <typename V> CMatrixDynamic(size_type row, size_type col, const V& theVector)
Constructor from a given size and a STL container (std::vector, std::list,…) with the initial values.
The vector length must match cols x row.
CMatrixDynamic(CMatrixDynamic&& m)
Move ctor.
Methods
void swap(CMatrixDynamic<T>& o)
Swap with another matrix very efficiently (just swaps a pointer and two integer values).
template <typename U> CMatrixDynamic& operator = (const CMatrixDynamic<U>& m)
Assignment operator from another matrix (possibly of a different type)
template <class Derived> CMatrixDynamic& operator = (const Eigen::MatrixBase<Derived>& m)
Assignment from an Eigen matrix.
template <size_type ROWS, size_type COLS> CMatrixDynamic& operator = (const CMatrixFixed<T, ROWS, COLS>& m)
Assignment from a fixed matrix.
template <typename V, size_t N> CMatrixDynamic& operator = (V(&) theArray [N])
Assignment operator for initializing from a C array (The matrix must be set to the correct size before invoking this assignment)
CMatrixDouble M(3,2); const double numbers[] = { 1,2,3, 4,5,6 }; M = numbers;
Refer also to the constructor with initialization data CMatrixDynamic::CMatrixDynamic
CMatrixDynamic& operator = (CMatrixDynamic&& m)
Move operator.
size_type rows() const
Number of rows in the matrix.
See also:
size_type cols() const
Number of columns in the matrix.
See also:
matrix_size_t size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
void setSize(size_type row, size_type col, bool zeroNewElements = false)
Changes the size of matrix, maintaining the previous contents.
void resize(size_type vectorLen)
Resizes as a Nx1 vector.
void resize(const matrix_size_t& siz, bool zeroNewElements = false)
Resize the matrix.
const T* data() const
Return raw pointer to row-major data buffer.
All matrix cells can be assumed to be stored contiguously in memory, i.e. row stride = column count.
T* data()
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
T& operator () (Index row, Index col)
Subscript operator to get/set individual elements.
const T& operator () (Index row, Index col) const
Subscript operator to get individual elements.
T& operator [] (Index ith)
Subscript operator to get/set an individual element from a row or column matrix.
Parameters:
std::exception |
If the object is not a column or row matrix. |
const T& operator [] (Index ith) const
Subscript operator to get/set an individual element from a row or column matrix.
For non-vectors (NxM matrices), it returns the i-th matrix element, in RowMajor order.
Parameters:
std::exception |
If the object is not a column or row matrix. |
template <typename VECTOR> void appendRow(const VECTOR& in)
Appends a new row to the MxN matrix from a 1xN vector.
The length of the vector must match the width of the matrix, unless it’s empty: in that case the matrix is resized to 1xN.
CMatrixDouble M(0,0); CVectorDouble v(7),w(7); // ... M.appendRow(v); M.appendRow(w);
Parameters:
std::exception |
On incorrect vector length. |
See also:
template <typename VECTOR> void appendCol(const VECTOR& in)
Appends a new column to the matrix from a vector.
The length of the vector must match the number of rows of the matrix, unless it is (0,0).
Parameters:
std::exception |
On size mismatch. |
See also:
extractCol
template <typename VECTOR> void asVector(VECTOR& out) const
Returns a vector containing the matrix’s values.
template < typename EIGEN_MATRIX = eigen_t, typename EIGEN_MAP = Eigen::Map<EIGEN_MATRIX, MRPT_MAX_ALIGN_BYTES, Eigen::InnerStride<1>> > EIGEN_MAP asEigen()
Get as an Eigen-compatible Eigen::Map object
CVectorDynamic<Scalar> llt_solve(const CVectorDynamic<Scalar>& b) const
Solves the linear system Ax=b, returns x, with A this symmetric matrix.
See also:
CVectorDynamic<Scalar> lu_solve(const CVectorDynamic<Scalar>& b) const
Solves the linear system Ax=b, returns x, with A this asymmetric matrix.
See also: