template<std::size_t cols, std::size_t rows, class T>
Magnum::Math::RectangularMatrix class

Rectangular matrix.

Template parameters
cols Column count
rows Row count
T Underlying data type

See Operations with matrices and vectors for brief introduction. See also Matrix (square), Matrix3, Matrix4 and Vector.

The data are stored in column-major order, to reflect that, all indices in math formulas are in reverse order (i.e. $ \boldsymbol A_{ji} $ instead of $ \boldsymbol A_{ij} $ ).

Public types

enum (anonymous): std::size_t { Cols = cols, Rows = rows, DiagonalSize = (cols < rows ? cols : rows) }
using Type = T
Underlying data type.

Public static functions

static auto from(T* data) -> RectangularMatrix<cols, rows, T>&
Matrix from an array.
static auto from(const T* data) -> const RectangularMatrix<cols, rows, T>&
static auto fromVector(const Vector<cols*rows, T>& vector) -> RectangularMatrix<cols, rows, T>
Construct a matrix from a vector.
static auto fromDiagonal(const Vector<DiagonalSize, T>& diagonal) -> RectangularMatrix<cols, rows, T> constexpr noexcept
Construct a diagonal matrix.

Constructors, destructors, conversion operators

RectangularMatrix() constexpr noexcept
Default constructor.
RectangularMatrix(ZeroInitT) explicit constexpr noexcept
Construct a zero-filled matrix.
RectangularMatrix(IdentityInitT, T value = T(1)) explicit constexpr noexcept new in Git master
Construct an identity matrix.
RectangularMatrix(Magnum::NoInitT) explicit noexcept
Construct without initializing the contents.
template<class ... U>
RectangularMatrix(const Vector<rows, T>& first, const U&... next) constexpr noexcept
Construct from column vectors.
RectangularMatrix(T value) explicit constexpr noexcept
Construct with one value for all components.
template<std::size_t cols_, std::size_t rows_>
RectangularMatrix(const T(&data)[cols_][rows_]) explicit constexpr noexcept new in Git master
Construct from a fixed-size array.
template<class U>
RectangularMatrix(const RectangularMatrix<cols, rows, U>& other) explicit constexpr noexcept
Construct from a matrix of a different type.
template<std::size_t otherCols, std::size_t otherRows>
RectangularMatrix(ZeroInitT, const RectangularMatrix<otherCols, otherRows, T>& other) explicit constexpr noexcept new in Git master
Construct by slicing or expanding a matrix of different size, leaving the rest at zero.
template<std::size_t otherCols, std::size_t otherRows>
RectangularMatrix(IdentityInitT, const RectangularMatrix<otherCols, otherRows, T>& other, T value = T(1)) explicit constexpr noexcept new in Git master
Construct by slicing or expanding a matrix of different size, leaving the rest at identity.
template<std::size_t otherCols, std::size_t otherRows>
RectangularMatrix(const RectangularMatrix<otherCols, otherRows, T>& other) explicit constexpr noexcept new in Git master
Construct by slicing or expanding a matrix of different size.
template<class U, class = decltype(Implementation::RectangularMatrixConverter<cols, rows, T, U>::from(std::declval<U>()))>
RectangularMatrix(const U& other) explicit constexpr
Construct a matrix from external representation.
template<class U, class = decltype(Implementation::RectangularMatrixConverter<cols, rows, T, U>::to(std::declval<RectangularMatrix<cols, rows, T>>()))>
operator U() const explicit constexpr
Convert the matrix to external representation.

Public functions

auto data() -> T*
Raw data.
auto data() const -> const T*
auto operator[](std::size_t col) -> Vector<rows, T>&
Column at given position.
auto operator[](std::size_t col) const -> const Vector<rows, T>& constexpr
auto row(std::size_t row) const -> Vector<cols, T>
Row at given position.
void setRow(std::size_t row, const Vector<cols, T>& data)
Set matrix row.
auto operator==(const RectangularMatrix<cols, rows, T>& other) const -> bool
Equality comparison.
auto operator!=(const RectangularMatrix<cols, rows, T>& other) const -> bool
Non-equality comparison.
auto operator<(const RectangularMatrix<cols, rows, T>& other) const -> BitVector<cols*rows>
Component-wise less than.
auto operator<=(const RectangularMatrix<cols, rows, T>& other) const -> BitVector<cols*rows>
Component-wise less than or equal.
auto operator>=(const RectangularMatrix<cols, rows, T>& other) const -> BitVector<cols*rows>
Component-wise greater than or equal.
auto operator>(const RectangularMatrix<cols, rows, T>& other) const -> BitVector<cols*rows>
Component-wise greater than.
auto operator+() const -> RectangularMatrix<cols, rows, T> new in Git master
Promotion.
auto operator-() const -> RectangularMatrix<cols, rows, T>
Negated matrix.
auto operator+=(const RectangularMatrix<cols, rows, T>& other) -> RectangularMatrix<cols, rows, T>&
Add and assign a matrix.
auto operator+(const RectangularMatrix<cols, rows, T>& other) const -> RectangularMatrix<cols, rows, T>
Add a matrix.
auto operator-=(const RectangularMatrix<cols, rows, T>& other) -> RectangularMatrix<cols, rows, T>&
Subtract and assign a matrix.
auto operator-(const RectangularMatrix<cols, rows, T>& other) const -> RectangularMatrix<cols, rows, T>
Subtract a matrix.
auto operator*=(T scalar) -> RectangularMatrix<cols, rows, T>&
Multiply with a scalar and assign.
auto operator*(T scalar) const -> RectangularMatrix<cols, rows, T>
Multiply with a scalar.
auto operator/=(T scalar) -> RectangularMatrix<cols, rows, T>&
Divide with a scalar and assign.
auto operator/(T scalar) const -> RectangularMatrix<cols, rows, T>
Divide with a scalar.
template<std::size_t size>
auto operator*(const RectangularMatrix<size, cols, T>& other) const -> RectangularMatrix<size, rows, T>
Multiply a matrix.
auto operator*(const Vector<cols, T>& other) const -> Vector<rows, T>
Multiply a vector.
auto transposed() const -> RectangularMatrix<rows, cols, T>
Transposed matrix.
auto flippedCols() const -> RectangularMatrix<cols, rows, T> constexpr
Matrix with flipped cols.
auto flippedRows() const -> RectangularMatrix<cols, rows, T> constexpr
Matrix with flipped rows.
auto diagonal() const -> Vector<DiagonalSize, T> constexpr
Values on diagonal.
auto toVector() const -> Vector<rows*cols, T>
Convert matrix to vector.

Friends

auto operator*(T scalar, const RectangularMatrix<cols, rows, T>& matrix) -> RectangularMatrix<cols, rows, T>
Multiply a scalar with a matrix.
auto operator/(T scalar, const RectangularMatrix<cols, rows, T>& matrix) -> RectangularMatrix<cols, rows, T>
Divide a matrix with a scalar and invert.

Enum documentation

template<std::size_t cols, std::size_t rows, class T>
enum Magnum::Math::RectangularMatrix<cols, rows, T>::(anonymous): std::size_t

Enumerators
Cols

Matrix column count

Rows

Matrix row count

DiagonalSize

Size of matrix diagonal

Function documentation

template<std::size_t cols, std::size_t rows, class T>
static RectangularMatrix<cols, rows, T>& Magnum::Math::RectangularMatrix<cols, rows, T>::from(T* data)

Matrix from an array.

Returns Reference to the data as if it was matrix, thus doesn't perform any copying.

Use with caution, the function doesn't check whether the array is long enough. If possible, prefer to use the RectangularMatrix(const T(&)[cols_][rows_]) constructor.

template<std::size_t cols, std::size_t rows, class T>
static const RectangularMatrix<cols, rows, T>& Magnum::Math::RectangularMatrix<cols, rows, T>::from(const T* data)

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

template<std::size_t cols, std::size_t rows, class T>
static RectangularMatrix<cols, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::fromVector(const Vector<cols*rows, T>& vector)

Construct a matrix from a vector.

Rolls the vector into matrix, i.e. first rows elements of the vector will make first column of resulting matrix.

template<std::size_t cols, std::size_t rows, class T>
static RectangularMatrix<cols, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::fromDiagonal(const Vector<DiagonalSize, T>& diagonal) constexpr noexcept

Construct a diagonal matrix.

template<std::size_t cols, std::size_t rows, class T>
Magnum::Math::RectangularMatrix<cols, rows, T>::RectangularMatrix() constexpr noexcept

Default constructor.

Equivalent to RectangularMatrix(ZeroInitT).

template<std::size_t cols, std::size_t rows, class T>
Magnum::Math::RectangularMatrix<cols, rows, T>::RectangularMatrix(ZeroInitT) explicit constexpr noexcept

Construct a zero-filled matrix.

template<std::size_t cols, std::size_t rows, class T>
Magnum::Math::RectangularMatrix<cols, rows, T>::RectangularMatrix(IdentityInitT, T value = T(1)) explicit constexpr noexcept new in Git master

Construct an identity matrix.

For non-square matrices, the diagonal has DiagonalSize elements. The value allows you to specify a value on diagonal.

template<std::size_t cols, std::size_t rows, class T> template<std::size_t cols_, std::size_t rows_>
Magnum::Math::RectangularMatrix<cols, rows, T>::RectangularMatrix(const T(&data)[cols_][rows_]) explicit constexpr noexcept new in Git master

Construct from a fixed-size array.

Use from(const T*) to reinterpret an arbitrary pointer to a matrix.

template<std::size_t cols, std::size_t rows, class T> template<class U>
Magnum::Math::RectangularMatrix<cols, rows, T>::RectangularMatrix(const RectangularMatrix<cols, rows, U>& other) explicit constexpr noexcept

Construct from a matrix of a different type.

Performs only default casting on the values, no rounding or anything else. Example usage:

Math::RectangularMatrix<4, 1, Float> floatingPoint{1.3f, 2.7f, -15.0f, 7.0f};
Math::RectangularMatrix<4, 1, Byte> integral{floatingPoint}; // {1, 2, -15, 7}

template<std::size_t cols, std::size_t rows, class T> template<std::size_t otherCols, std::size_t otherRows>
Magnum::Math::RectangularMatrix<cols, rows, T>::RectangularMatrix(ZeroInitT, const RectangularMatrix<otherCols, otherRows, T>& other) explicit constexpr noexcept new in Git master

Construct by slicing or expanding a matrix of different size, leaving the rest at zero.

If the other matrix has less columns or rows, the corresponding vectors and components are set to zeros.

template<std::size_t cols, std::size_t rows, class T> template<std::size_t otherCols, std::size_t otherRows>
Magnum::Math::RectangularMatrix<cols, rows, T>::RectangularMatrix(IdentityInitT, const RectangularMatrix<otherCols, otherRows, T>& other, T value = T(1)) explicit constexpr noexcept new in Git master

Construct by slicing or expanding a matrix of different size, leaving the rest at identity.

If the other matrix has less columns or rows, the corresponding vectors and components are set to either zeros or value on the diagonal.

template<std::size_t cols, std::size_t rows, class T> template<std::size_t otherCols, std::size_t otherRows>
Magnum::Math::RectangularMatrix<cols, rows, T>::RectangularMatrix(const RectangularMatrix<otherCols, otherRows, T>& other) explicit constexpr noexcept new in Git master

Construct by slicing or expanding a matrix of different size.

Equivalent to RectangularMatrix(ZeroInitT, const RectangularMatrix<otherCols, otherRows, T>&). Note that this default is different from Matrix, where it's equivalent to the IdentityInit variant instead.

template<std::size_t cols, std::size_t rows, class T>
T* Magnum::Math::RectangularMatrix<cols, rows, T>::data()

Raw data.

Contrary to what Doxygen shows, returns reference to an one-dimensional fixed-size array of cols*rows elements, i.e. T(&)[cols*rows].

template<std::size_t cols, std::size_t rows, class T>
const T* Magnum::Math::RectangularMatrix<cols, rows, T>::data() const

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

template<std::size_t cols, std::size_t rows, class T>
Vector<rows, T>& Magnum::Math::RectangularMatrix<cols, rows, T>::operator[](std::size_t col)

Column at given position.

Particular elements can be accessed using Vector::operator[](), e.g.:

Matrix4x3 m;
Float a = m[2][1];

template<std::size_t cols, std::size_t rows, class T>
const Vector<rows, T>& Magnum::Math::RectangularMatrix<cols, rows, T>::operator[](std::size_t col) const constexpr

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

template<std::size_t cols, std::size_t rows, class T>
Vector<cols, T> Magnum::Math::RectangularMatrix<cols, rows, T>::row(std::size_t row) const

Row at given position.

Consider using transposed() when accessing rows frequently, as this is slower than accessing columns due to the way the matrix is stored.

template<std::size_t cols, std::size_t rows, class T>
void Magnum::Math::RectangularMatrix<cols, rows, T>::setRow(std::size_t row, const Vector<cols, T>& data)

Set matrix row.

Consider using transposed() when accessing rows frequently, as this is slower than accessing columns due to the way the matrix is stored.

template<std::size_t cols, std::size_t rows, class T>
bool Magnum::Math::RectangularMatrix<cols, rows, T>::operator!=(const RectangularMatrix<cols, rows, T>& other) const

Non-equality comparison.

template<std::size_t cols, std::size_t rows, class T>
BitVector<cols*rows> Magnum::Math::RectangularMatrix<cols, rows, T>::operator<(const RectangularMatrix<cols, rows, T>& other) const

Component-wise less than.

Calls Vector::operator<() on toVector().

template<std::size_t cols, std::size_t rows, class T>
BitVector<cols*rows> Magnum::Math::RectangularMatrix<cols, rows, T>::operator<=(const RectangularMatrix<cols, rows, T>& other) const

Component-wise less than or equal.

Calls Vector::operator<=() on toVector().

template<std::size_t cols, std::size_t rows, class T>
BitVector<cols*rows> Magnum::Math::RectangularMatrix<cols, rows, T>::operator>=(const RectangularMatrix<cols, rows, T>& other) const

Component-wise greater than or equal.

Calls Vector::operator>=() on toVector().

template<std::size_t cols, std::size_t rows, class T>
BitVector<cols*rows> Magnum::Math::RectangularMatrix<cols, rows, T>::operator>(const RectangularMatrix<cols, rows, T>& other) const

Component-wise greater than.

Calls Vector::operator>() on toVector().

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::operator+() const new in Git master

Promotion.

Returns the value as-is.

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::operator-() const

Negated matrix.

The computation is done column-wise.

\[ \boldsymbol B_j = -\boldsymbol A_j \]

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T>& Magnum::Math::RectangularMatrix<cols, rows, T>::operator+=(const RectangularMatrix<cols, rows, T>& other)

Add and assign a matrix.

The computation is done column-wise in-place.

\[ \boldsymbol A_j = \boldsymbol A_j + \boldsymbol B_j \]

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::operator+(const RectangularMatrix<cols, rows, T>& other) const

Add a matrix.

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T>& Magnum::Math::RectangularMatrix<cols, rows, T>::operator-=(const RectangularMatrix<cols, rows, T>& other)

Subtract and assign a matrix.

The computation is done column-wise in-place.

\[ \boldsymbol A_j = \boldsymbol A_j - \boldsymbol B_j \]

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::operator-(const RectangularMatrix<cols, rows, T>& other) const

Subtract a matrix.

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T>& Magnum::Math::RectangularMatrix<cols, rows, T>::operator*=(T scalar)

Multiply with a scalar and assign.

The computation is done column-wise in-place.

\[ \boldsymbol A_j = a \boldsymbol A_j \]

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::operator*(T scalar) const

Multiply with a scalar.

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T>& Magnum::Math::RectangularMatrix<cols, rows, T>::operator/=(T scalar)

Divide with a scalar and assign.

The computation is done column-wise in-place.

\[ \boldsymbol A_j = \frac{\boldsymbol A_j} a \]

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::operator/(T scalar) const

Divide with a scalar.

template<std::size_t cols, std::size_t rows, class T> template<std::size_t size>
RectangularMatrix<size, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::operator*(const RectangularMatrix<size, cols, T>& other) const

Multiply a matrix.

\[ (\boldsymbol {AB})_{ji} = \sum_{k=0}^{m-1} \boldsymbol A_{ki} \boldsymbol B_{jk} \]

template<std::size_t cols, std::size_t rows, class T>
Vector<rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::operator*(const Vector<cols, T>& other) const

Multiply a vector.

Internally the same as multiplying with a one-column matrix, but returns a vector instead of a one-column matrix.

\[ (\boldsymbol {Aa})_i = \sum_{k=0}^{m-1} \boldsymbol A_{ki} \boldsymbol a_k \]

Vectors are treated as columns. An equivalent operation is multiplying a transposed matrix with an one-row matrix from the left side instead:

Matrix3x4 m = ;
Vector3 v = ;

/* a1 and a2 contain the same data, just transposed */
Vector4 a1 = m*v;
Matrix4x1 a2 = Matrix3x1::fromVector(v)*m.transposed();

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<rows, cols, T> Magnum::Math::RectangularMatrix<cols, rows, T>::transposed() const

Transposed matrix.

\[ \boldsymbol{A}^T_ij = \boldsymbol{A}_ji \]

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::flippedCols() const constexpr

Matrix with flipped cols.

The order of columns is reversed.

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T> Magnum::Math::RectangularMatrix<cols, rows, T>::flippedRows() const constexpr

Matrix with flipped rows.

The order of rows is reversed.

template<std::size_t cols, std::size_t rows, class T>
Vector<DiagonalSize, T> Magnum::Math::RectangularMatrix<cols, rows, T>::diagonal() const constexpr

Values on diagonal.

template<std::size_t cols, std::size_t rows, class T>
Vector<rows*cols, T> Magnum::Math::RectangularMatrix<cols, rows, T>::toVector() const

Convert matrix to vector.

Returns the matrix unrolled into one large vector, i.e. first column of the matrix will make first rows elements of resulting vector. Useful for performing vector operations with the matrix (e.g. summing the elements etc.).

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T> operator*(T scalar, const RectangularMatrix<cols, rows, T>& matrix)

Multiply a scalar with a matrix.

Same as operator*(T) const.

template<std::size_t cols, std::size_t rows, class T>
RectangularMatrix<cols, rows, T> operator/(T scalar, const RectangularMatrix<cols, rows, T>& matrix)

Divide a matrix with a scalar and invert.

The computation is done column-wise.

\[ \boldsymbol B_j = \frac a {\boldsymbol A_j} \]

template<std::size_t cols, std::size_t rows, class T> template<std::size_t size, std::size_t cols, class T>
RectangularMatrix<cols, size, T> operator*(const Vector<size, T>& vector, const RectangularMatrix<cols, 1, T>& matrix)

Multiply a vector with a rectangular matrix.

Internally the same as multiplying one-column matrix with one-row matrix.

\[ (\boldsymbol {aA})_{ji} = \boldsymbol a_i \boldsymbol A_j \]

template<std::size_t cols, std::size_t rows, class T> template<std::size_t cols, std::size_t rows, class T>
Debug& operator<<(Debug& debug, const Magnum::Math::RectangularMatrix<cols, rows, T>& value)

Debug output operator.