template class mrpt::math::MatrixVectorBase

Overview

Base CRTP class for all MRPT vectors and matrices.

Template methods whose implementation is not in this header file are explicitly instantiated and exported for these derived types:

See also:

CMatrixFixed

#include <mrpt/math/MatrixVectorBase.h>

template <typename Scalar, class Derived>
class MatrixVectorBase
{
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;
};

// direct descendants

template <class T>
class CVectorDynamic;

template <typename Scalar, class Derived>
class MatrixBase;

Methods

void fill(const Scalar& val)

Fill all the elements with a given value (Note: named “fillAll” since “fill” will be used by child classes)

template <matrix_dim_t BLOCK_ROWS, matrix_dim_t BLOCK_COLS>
auto block(
    matrix_index_t start_row,
    matrix_index_t start_col
    )

non-const block() : Returns an Eigen::Block reference to the block

Scalar minCoeff() const

Minimum value in the matrix/vector.

Scalar maxCoeff() const

Maximum value in the matrix/vector.

bool isSquare() const

returns true if matrix is NxN

bool empty() const

returns true if matrix/vector has size=0

Scalar norm_inf() const

Compute the norm-infinite of a vector (.

\[||\mathbf{v}||_\infnty\]

), ie the maximum absolute value of the elements.

Scalar norm() const

Compute the L2 norm of a vector/array/matrix (the Euclidean distance to the origin, taking all the elements as a single vector).

See also:

norm

Scalar dot(const CVectorDynamic<Scalar>& v) const

dot product of this \cdot v

void matProductOf_Ab(const CMatrixDynamic<Scalar>& A, const CVectorDynamic<Scalar>& b)

this = A * b , with A and b a dynamic matrix & vector

void matProductOf_Atb(const CMatrixDynamic<Scalar>& A, const CVectorDynamic<Scalar>& b)

this = A T * b , with A and b a dynamic matrix & vector

Scalar sum() const

Sum of all elements in matrix/vector.

Scalar sum_abs() const

Sum of the absolute value of all elements in matrix/vector.

std::string asString() const

Returns a string representation of the vector/matrix, using Eigen’s default settings.

bool fromMatlabStringFormat(
    const std::string& s,
    mrpt::optional_ref<std::ostream> dump_errors_here = std::nullopt
    )

Reads a matrix from a string in Matlab-like format, for example: “[1 0 2; 0 4 -1]” The string must start with ‘[’ and end with ‘]’.

Rows are separated by semicolons ‘;’ and columns in each row by one or more whitespaces ‘ ‘ or tabs ‘t’. Commas ‘,’ between elements are NOT allowed.

This format is also used for CConfigFile::read_matrix.

Returns:

true on success. false if the string is malformed, and then the matrix will be resized to 0x0.

See also:

inMatlabFormat, CConfigFile::read_matrix

std::string inMatlabFormat(const std::size_t decimal_digits = 6) const

Exports the matrix as a string compatible with Matlab/Octave.

See also:

fromMatlabStringFormat()

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

Saves the vector/matrix to a file compatible with MATLAB/Octave text format.

Parameters:

file

The target filename.

fileFormat

See TMatrixTextFileFormat. The format of the numbers in the text file.

appendMRPTHeader

Insert a two-line “% File generated with mrpt-math at <date>” comment header before the data.

userHeader

Additional text to be written at the head of the file. Typically MATLAB comments “% This file blah blah”. A final end-of-line is not needed: one is added if missing.

See also:

loadFromTextFile, CMatrixDynamic::inMatlabFormat, SAVE_MATRIX

void loadFromTextFile(std::istream& f)

Loads a vector/matrix from a text file, compatible with MATLAB text format.

Lines starting with ‘’ or ‘#’ are interpreted as comments and ignored.

Parameters:

std::runtime_error

On format error.

See also:

saveToTextFile, fromMatlabStringFormat

void loadFromTextFile(const std::string& file)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.