template class mrpt::math::CMatrixFixed

Overview

A compile-time fixed-size numeric matrix container.

It uses a RowMajor element memory layout.

For a complete introduction to Matrices and vectors in MRPT, see: https://www.mrpt.org/Matrices_vectors_arrays_and_Linear_Algebra_MRPT_and_Eigen_classes

See also:

CMatrixDynamic (for dynamic-size matrices)

#include <mrpt/math/CMatrixFixed.h>

template <typename T, matrix_dim_t ROWS, matrix_dim_t COLS>
class CMatrixFixed: 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, ROWS, COLS, StorageOrder, ROWS, COLS> eigen_t;
    typedef typename vec_t::iterator iterator;
    typedef typename vec_t::const_iterator const_iterator;

    // fields

    static constexpr static int RowsAtCompileTime = ROWS;
    static constexpr static int ColsAtCompileTime = COLS;
    static constexpr static int SizeAtCompileTime = ROWS* COLS;
    static constexpr static int is_mrpt_type = 1;
    static constexpr static int StorageOrder =(ROWS != 1&& COLS == 1) ? 0  : 1;

    // construction

    CMatrixFixed();
    CMatrixFixed(TConstructorFlags_Matrices);

    template <size_t N>
    CMatrixFixed(const T(&) vals [N]);

    CMatrixFixed(const T* data);

    template <class Derived>
    CMatrixFixed(const Eigen::MatrixBase<Derived>& m);

    template <typename LHS, typename RHS, int Option>
    CMatrixFixed(const Eigen::Product<LHS, RHS, Option>& p);

    template <typename Op, typename Lhs, typename Rhs>
    CMatrixFixed(const Eigen::CwiseBinaryOp<Op, Lhs, Rhs>& p);

    template <typename VectorType, int Size>
    CMatrixFixed(const Eigen::VectorBlock<VectorType, Size>& m);

    CMatrixFixed(const size_type rows, const size_type cols);

    // methods

    iterator begin();
    iterator end();
    const_iterator begin() const;
    const_iterator end() const;
    const_iterator cbegin() const;
    const_iterator cend() const;

    template <class MAT>
    void setFromMatrixLike(const MAT& m);

    template <class Derived>
    CMatrixFixed& operator = (const Eigen::MatrixBase<Derived>& m);

    template <typename VectorType, int Size>
    CMatrixFixed& operator = (const Eigen::VectorBlock<VectorType, Size>& m);

    template <typename U>
    CMatrixFixed& operator = (const CMatrixDynamic<U>& m);

    template <typename VECTOR>
    void loadFromArray(const VECTOR& vals);

    void loadFromRawPointer(const T* data);
    void setSize(size_type row, size_type col, ] bool zeroNewElements = false);
    void swap(CMatrixFixed& o);
    CMatrixFixed& derived();
    const CMatrixFixed& derived() const;

    void conservativeResize(
        size_type row,
        size_type col
        );

    void resize(size_type n);
    void resize(const matrix_size_t& siz, ] bool zeroNewElements = false);

    void resize(
        size_type row,
        size_type col
        );

    constexpr size_type rows() const;
    constexpr size_type cols() const;
    constexpr matrix_size_t size() const;

    template <
        typename EIGEN_MATRIX = eigen_t,
        typename EIGEN_MAP = Eigen::Map<EIGEN_MATRIX, EIGEN_MAP_ALIGN_BYTES, Eigen::InnerStride<1>>
        >
    EIGEN_MAP asEigen();

    template <
        typename EIGEN_MATRIX = eigen_t,
        typename EIGEN_MAP = Eigen::Map<const EIGEN_MATRIX, EIGEN_MAP_ALIGN_BYTES, Eigen::InnerStride<1>>
        >
    EIGEN_MAP asEigen() const;

    const T* data() const;
    T* data();
    T& operator () (Index row, Index col);

    const T& operator () (
        Index row,
        Index col
        ) const;

    T& operator () (Index i);
    const T& operator () (Index i) const;
    T& operator [] (Index i);
    const T& operator [] (Index i) const;
    CMatrixFixed<float, ROWS, COLS> cast_float() const;
    CMatrixFixed<double, ROWS, COLS> cast_double() const;
    CMatrixFixed<T, ROWS, 1> llt_solve(const CMatrixFixed<T, ROWS, 1>& b) const;
    CMatrixFixed<T, ROWS, 1> lu_solve(const CMatrixFixed<T, ROWS, 1>& b) const;
    void sum_At(const CMatrixFixed<Scalar, ROWS, COLS>& A);
};

// direct descendants

template <class T>
class CQuaternion;

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

CMatrixFixed()

Default constructor, initializes all elements to zero.

CMatrixFixed(TConstructorFlags_Matrices)

Constructor which leaves the matrix uninitialized.

Example of usage: CMatrixFixed<double,3,2> M(mrpt::math::UNINITIALIZED_MATRIX);

template <size_t N>
CMatrixFixed(const T(&) vals [N])

Initializes from a C array with RowMajor values.

CMatrixFixed(const T* data)

Initializes from a plain buffer with RowMajor values.

template <class Derived>
CMatrixFixed(const Eigen::MatrixBase<Derived>& m)

Convert from Eigen matrix.

template <typename LHS, typename RHS, int Option>
CMatrixFixed(const Eigen::Product<LHS, RHS, Option>& p)

Convert from Eigen product.

template <typename Op, typename Lhs, typename Rhs>
CMatrixFixed(const Eigen::CwiseBinaryOp<Op, Lhs, Rhs>& p)

Convert from Eigen binary op.

template <typename VectorType, int Size>
CMatrixFixed(const Eigen::VectorBlock<VectorType, Size>& m)

Convert from Eigen block.

CMatrixFixed(const size_type rows, const size_type cols)

Convenient ctor from size: in this class, it throws if size does not match compile-time size.

It is provided for the sake of offering a uniform API with CMatrixDynamic.

Methods

template <class Derived>
CMatrixFixed& operator = (const Eigen::MatrixBase<Derived>& m)

Assignment from an Eigen matrix.

template <typename VectorType, int Size>
CMatrixFixed& operator = (const Eigen::VectorBlock<VectorType, Size>& m)

Assignment from an Eigen vector block.

template <typename U>
CMatrixFixed& operator = (const CMatrixDynamic<U>& m)

Assignment from a Dynamic matrix.

void loadFromRawPointer(const T* data)

Initializes from a plain buffer with RowMajor values.

Unsafe, prefer loadFromArray() wherever possible, to ensure buffer length checks.

void setSize(size_type row, size_type col, ] bool zeroNewElements = false)

Throws if size does not match with the fixed matrix size.

void resize(const matrix_size_t& siz, ] bool zeroNewElements = false)

Throws if size does not match with the fixed matrix size.

constexpr size_type rows() const

Number of rows in the matrix.

See also:

rows()

constexpr size_type cols() const

Number of columns in the matrix.

See also:

rows()

constexpr matrix_size_t size() const

Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))

template <
    typename EIGEN_MATRIX = eigen_t,
    typename EIGEN_MAP = Eigen::Map<EIGEN_MATRIX, EIGEN_MAP_ALIGN_BYTES, Eigen::InnerStride<1>>
    >
EIGEN_MAP asEigen()

Get as an Eigen-compatible Eigen::Map object

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)

Access (row,col), without out-of-bounds check (except in Debug builds)

T& operator () (Index i)

Access the i-th element, Row-Major order, without out-of-bounds check (except in Debug builds)

T& operator [] (Index i)

Access the [i-th] element (for 1xN or Nx1 matrices)

CMatrixFixed<T, ROWS, 1> llt_solve(const CMatrixFixed<T, ROWS, 1>& b) const

Solves the linear system Ax=b, returns x, with A this symmetric matrix.

See also:

lu_solve()

CMatrixFixed<T, ROWS, 1> lu_solve(const CMatrixFixed<T, ROWS, 1>& b) const

Solves the linear system Ax=b, returns x, with A this asymmetric matrix.

See also:

llt_solve()

void sum_At(const CMatrixFixed<Scalar, ROWS, COLS>& A)

this += A T