Skip to content

raisim::Mat ​

class · #include <raisim/math/Matrix.hpp>

Inherits: raisim::MatExpr< Mat< n, m > >

cpp
template <size_t n, size_t m>
class Mat

Fixed-size n x m matrix of doubles, stored column-major: element (i, j) is v[i + j * n]. Vec<n> is the column-vector alias Mat<n, 1>.

The default constructor leaves the elements uninitialized. Arithmetic on Mat objects and other raisim matrix expressions (+, -, *, /, also with scalars) builds expression templates that hold their operands by reference and are evaluated when assigned, so do not store an expression that refers to temporaries (e.g. with auto). Assignment evaluates the expression element by element into the destination: element-wise expressions that read the destination (a = a + b) are fine, but a product or transpose that reads it (a = a * b, a = a.transpose()) gives wrong results; the 4x4 by 4x4 product is evaluated eagerly and is the exception. e() returns an Eigen map of the same memory.

In addition to the members listed here, Mat provides (through a shared macro that is not shown in this reference) norm(), sum(), squaredNorm(), dot(), setZero(), setConstant(), scalar *=, /=, += and -=, assignment from any matrix-like object of the same dimensions, cross() and skew() for 3D vectors, and an implicit conversion to double that returns element (0, 0).

Public types ​

EigenMat ​

cpp
typedef Eigen::Map<Eigen::Matrix<ScalarType, n, m>> EigenMat

Mutable Eigen map type returned by e().

ConstEigenMat ​

cpp
typedef Eigen::Map<const Eigen::Matrix<ScalarType, n, m>> ConstEigenMat

Read-only Eigen map type returned by e() const.

SelfType ​

cpp
typedef Mat<n, m> SelfType

This type (used by the shared expression machinery).

Constructors & destructor ​

Mat ​

cpp
Mat() = default

Leaves the elements uninitialized.

cpp
Mat(std::initializer_list<ScalarType> init)

Constructs from values listed in column-major order. The list may not exceed n*m elements (fatal otherwise); missing trailing elements are zero-filled.

Parameters

ParameterDescription
init (in)element values, column-major
cpp
template <class scalarType>
Mat(const Eigen::Matrix<scalarType, n, m> &eigen)

Copies an Eigen matrix with the same dimensions.

Parameters

ParameterDescription
eigen (in)source matrix
cpp
template <class T2>
Mat(const T2 &rhs)

Constructs from any matrix-like object with rows(), cols() and operator()(i, j), such as a raisim matrix expression or an Eigen object. Mismatched dimensions are a compile-time error for raisim types and fatal at run time otherwise.

Parameters

ParameterDescription
rhs (in)source

Public member functions ​

operator= ​

cpp
template <class T2>
Mat<n, m> &operator=(const MatExpr<T2> &expr)

Evaluates an expression with identical dimensions into this matrix (see the class description for aliasing).

Parameters

ParameterDescription
expr (in)expression to evaluate

Returns *this

cpp
template <size_t n2, size_t m2>
Mat<n, m> &operator=(const Mat<n2, m2> &expr)

Copies a Mat with identical dimensions (checked at compile time).

Parameters

ParameterDescription
expr (in)matrix to copy

Returns *this

ptr ​

cpp
ScalarType *ptr()

Returns pointer to the first element.

cpp
const ScalarType *ptr() const

Returns pointer to the first element.

data ​

cpp
ScalarType *data()

Returns pointer to the first element (Eigen-compatible name).

cpp
const ScalarType *data() const

Returns pointer to the first element (Eigen-compatible name).

operator[] ​

cpp
double operator[](size_t i) const

Parameters

ParameterDescription
i (in)column-major linear index

Returns element i (no bounds check)

cpp
double &operator[](size_t i)

Parameters

ParameterDescription
i (in)column-major linear index

Returns reference to element i (no bounds check)

operator() ​

cpp
double operator()(size_t i) const

Parameters

ParameterDescription
i (in)column-major linear index

Returns element i (no bounds check)

cpp
double &operator()(size_t i)

Parameters

ParameterDescription
i (in)column-major linear index

Returns reference to element i (no bounds check)

cpp
double operator()(size_t i, size_t j) const

Parameters

ParameterDescription
i (in)row index
j (in)column index

Returns element (i, j) (no bounds check)

cpp
double &operator()(size_t i, size_t j)

Parameters

ParameterDescription
i (in)row index
j (in)column index

Returns reference to element (i, j) (no bounds check)

operator- ​

cpp
Mat<n, m> operator-()

Returns the negated matrix.

e ​

cpp
EigenMat e()

Returns Eigen map aliasing this matrix's memory (writes go to this matrix).

cpp
ConstEigenMat e() const

Returns read-only Eigen map aliasing this matrix's memory.

operator<< ​

cpp
ElementRef<Mat<n, m>> operator<<(ScalarType a)

Comma-initializer entry point (Eigen-style): v << 1.0, 2.0, 3.0;. Elements are written in column-major order; writing past the last element is fatal.

Parameters

ParameterDescription
a (in)value of the first element

Returns proxy that receives the following elements

normalize ​

cpp
void normalize()

Eigen-like vector normalization (vectors only). A zero vector is left unchanged.

operator-= ​

cpp
void operator-=(const Mat<n, m> &val)

Element-wise subtraction.

Parameters

ParameterDescription
val (in)matrix to subtract

operator+= ​

cpp
void operator+=(const Mat<n, m> &val)

Element-wise addition.

Parameters

ParameterDescription
val (in)matrix to add

operator/= ​

cpp
void operator/=(const Mat<n, m> &val)

Element-wise division.

Parameters

ParameterDescription
val (in)element-wise divisors

setIdentity ​

cpp
void setIdentity()

Sets this square matrix to the identity.

determinant ​

cpp
ScalarType determinant() const

Returns the determinant, computed by Eigen (square matrices only).

inverse ​

cpp
Mat<n, n> inverse() const

Returns the inverse, computed by Eigen (square matrices only). Singularity is not checked.

transpose ​

cpp
Transpose<Mat<n, m>> transpose()

Returns lazy transpose expression referencing this matrix.

cpp
const Transpose<Mat<n, m>> transpose() const

Returns lazy transpose expression referencing this matrix.

row ​

cpp
RowRef<Mat<n, m>, m> row(size_t r)

Parameters

ParameterDescription
r (in)row index

Returns proxy referencing row r; writes go to this matrix.

cpp
RowRef<const Mat<n, m>, m> row(size_t r) const

Parameters

ParameterDescription
r (in)row index

Returns read-only proxy referencing row r.

col ​

cpp
ColRef<Mat<n, m>, n> col(size_t r)

Parameters

ParameterDescription
r (in)column index

Returns proxy referencing column r; writes go to this matrix.

cpp
ColRef<const Mat<n, m>, n> col(size_t r) const

Parameters

ParameterDescription
r (in)column index

Returns read-only proxy referencing column r.

segment ​

cpp
template <size_t sizeRow, size_t sizeCol>
BlockRef<Mat<n, m>, sizeRow, sizeCol> segment(
    size_t startRow,
    size_t startCol)

Block of size sizeRow x sizeCol (template arguments) starting at a run-time position. No bounds check.

Parameters

ParameterDescription
startRow (in)row of the block's top-left element
startCol (in)column of the block's top-left element

Returns proxy referencing the block; writes go to this matrix.

cpp
template <size_t sizeRow, size_t sizeCol>
BlockRef<const Mat<n, m>, sizeRow, sizeCol> segment(
    size_t startRow,
    size_t startCol) const

Read-only block of size sizeRow x sizeCol starting at a run-time position. No bounds check.

Parameters

ParameterDescription
startRow (in)row of the block's top-left element
startCol (in)column of the block's top-left element

Returns read-only proxy referencing the block.

cpp
template <size_t sizeRow>
BlockRef<Mat<n, m>, sizeRow, 1> head()

Returns proxy referencing the first sizeRow elements (vectors only).

cpp
template <size_t sizeRow>
BlockRef<const Mat<n, m>, sizeRow, 1> head() const

Returns read-only proxy referencing the first sizeRow elements (vectors only).

tail ​

cpp
template <size_t sizeRow>
BlockRef<Mat<n, m>, sizeRow, 1> tail()

Returns proxy referencing the last sizeRow elements (vectors only).

cpp
template <size_t sizeRow>
BlockRef<const Mat<n, m>, sizeRow, 1> tail() const

Returns read-only proxy referencing the last sizeRow elements (vectors only).

topLeftCorner ​

cpp
template <size_t sizeRow, size_t sizeCol>
BlockRef<const Mat<n, m>, sizeRow, sizeCol> topLeftCorner() const

Returns read-only proxy referencing the top-left sizeRow x sizeCol block.

cpp
template <size_t sizeRow, size_t sizeCol>
BlockRef<Mat<n, m>, sizeRow, sizeCol> topLeftCorner()

Returns proxy referencing the top-left sizeRow x sizeCol block.

topRightCorner ​

cpp
template <size_t sizeRow, size_t sizeCol>
BlockRef<const Mat<n, m>, sizeRow, sizeCol> topRightCorner() const

Returns read-only proxy referencing the top-right sizeRow x sizeCol block.

cpp
template <size_t sizeRow, size_t sizeCol>
BlockRef<Mat<n, m>, sizeRow, sizeCol> topRightCorner()

Returns proxy referencing the top-right sizeRow x sizeCol block.

bottomRightCorner ​

cpp
template <size_t sizeRow, size_t sizeCol>
BlockRef<const Mat<n, m>, sizeRow, sizeCol> bottomRightCorner() const

Returns read-only proxy referencing the bottom-right sizeRow x sizeCol block.

cpp
template <size_t sizeRow, size_t sizeCol>
BlockRef<Mat<n, m>, sizeRow, sizeCol> bottomRightCorner()

Returns proxy referencing the bottom-right sizeRow x sizeCol block.

bottomLeftCorner ​

cpp
template <size_t sizeRow, size_t sizeCol>
BlockRef<const Mat<n, m>, sizeRow, sizeCol> bottomLeftCorner() const

Returns read-only proxy referencing the bottom-left sizeRow x sizeCol block.

cpp
template <size_t sizeRow, size_t sizeCol>
BlockRef<Mat<n, m>, sizeRow, sizeCol> bottomLeftCorner()

Returns proxy referencing the bottom-left sizeRow x sizeCol block.

block ​

cpp
template <size_t sizeRow, size_t sizeCol, size_t startRow, size_t startCol>
BlockRef<const Mat<n, m>, sizeRow, sizeCol> block() const

Returns read-only proxy referencing the sizeRow x sizeCol block at (startRow, startCol), all given as template arguments.

cpp
template <size_t sizeRow, size_t sizeCol, size_t startRow, size_t startCol>
BlockRef<Mat<n, m>, sizeRow, sizeCol> block()

Returns proxy referencing the sizeRow x sizeCol block at (startRow, startCol), all given as template arguments.

Static member functions ​

size static ​

cpp
static constexpr size_t size()

Returns number of elements, n * m.

rows static ​

cpp
static constexpr size_t rows()

Returns number of rows, n.

cols static ​

cpp
static constexpr size_t cols()

Returns number of columns, m.

getIdentity static ​

cpp
static Mat<n, n> getIdentity()

Returns the n x n identity matrix (square matrices only).

Identity static ​

cpp
static Mat<n, n> Identity()

Returns the n x n identity matrix (Eigen-style name for getIdentity()).

getZeros static ​

cpp
static Mat<n, m> getZeros()

Returns a matrix of zeros.

Zero static ​

cpp
static Mat<n, m> Zero()

Returns a matrix of zeros (Eigen-style name for getZeros()).

Public attributes ​

alignment_only ​

cpp
std::aligned_storage<n *m *sizeof(ScalarType), 32> alignment_only

v ​

cpp
double v[n *m]

(anonymous union) ​

An anonymous union: its members are listed with the other attributes and share storage.

RaiSim — physics for robotics and AI.