template<std::size_t size, class T>
Magnum::Math::Matrix class

Square matrix.

Template parameters
size Matrix size
T Data type

See Operations with matrices and vectors for brief introduction.

Base classes

template<std::size_t cols, std::size_t rows, class T>
class RectangularMatrix<size, size, T>
Rectangular matrix.

Derived classes

template<class T>
class Matrix3
2D transformation matrix
template<class T>
class Matrix4
3D transformation matrix
template<class T>
class Matrix3
2D transformation matrix
template<class T>
class Matrix4
3D transformation matrix

Public types

enum (anonymous): std::size_t { Size = size }

Constructors, destructors, conversion operators

Matrix() constexpr noexcept
Default constructor.
Matrix(IdentityInitT, T value = T(1)) explicit constexpr noexcept
Construct an identity matrix.
Matrix(ZeroInitT) explicit constexpr noexcept
Construct a zero-filled matrix.
Matrix(Magnum::NoInitT) explicit constexpr noexcept
Construct without initializing the contents.
template<class ... U>
Matrix(const Vector<size, T>& first, const U&... next) constexpr noexcept
Construct from column vectors.
Matrix(T value) explicit constexpr noexcept
Construct with one value for all elements.
template<class U>
Matrix(const RectangularMatrix<size, size, U>& other) explicit constexpr noexcept
Construct from a matrix of adifferent type.
template<class U, class V = decltype(Implementation::RectangularMatrixConverter<size, size, T, U>::from(std::declval<U>()))>
Matrix(const U& other) explicit constexpr
Construct matrix from external representation.
template<std::size_t otherSize>
Matrix(const RectangularMatrix<otherSize, otherSize, T>& other) explicit constexpr noexcept
Construct by slicing or expanding a matrix of different size.
Matrix(const RectangularMatrix<size, size, T>& other) constexpr noexcept
Copy constructor.

Public functions

auto isOrthogonal() const -> bool
Whether the matrix is orthogonal.
auto trace() const -> T
Trace of the matrix.
auto ij(std::size_t skipCol, std::size_t skipRow) const -> Matrix<size-1, T>
Matrix without given column and row.
auto cofactor(std::size_t col, std::size_t row) const -> T new in 2019.10
Cofactor.
auto comatrix() const -> Matrix<size, T> new in 2019.10
Matrix of cofactors.
auto adjugate() const -> Matrix<size, T> new in 2019.10
Adjugate matrix.
auto determinant() const -> T
Determinant.
auto inverted() const -> Matrix<size, T>
Inverted matrix.
auto invertedOrthogonal() const -> Matrix<size, T>
Inverted orthogonal matrix.

Enum documentation

template<std::size_t size, class T>
enum Magnum::Math::Matrix<size, T>::(anonymous): std::size_t

Enumerators
Size

Matrix size

Function documentation

template<std::size_t size, class T>
Magnum::Math::Matrix<size, T>::Matrix() constexpr noexcept

Default constructor.

Equivalent to Matrix(IdentityInitT, T).

template<std::size_t size, class T>
Magnum::Math::Matrix<size, T>::Matrix(IdentityInitT, T value = T(1)) explicit constexpr noexcept

Construct an identity matrix.

The value allows you to specify a value on diagonal.

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

Construct from a matrix of adifferent type.

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

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

template<std::size_t size, class T> template<std::size_t otherSize>
Magnum::Math::Matrix<size, T>::Matrix(const RectangularMatrix<otherSize, otherSize, T>& other) explicit constexpr noexcept

Construct by slicing or expanding a matrix of different size.

If the other matrix is larger, takes only the first size columns and rows from it; if the other matrix is smaller, it's expanded to an identity (ones on diagonal, zeros elsewhere).

template<std::size_t size, class T>
bool Magnum::Math::Matrix<size, T>::isOrthogonal() const

Whether the matrix is orthogonal.

Returns true if all basis vectors have unit length and are orthogonal to each other. In other words, when its transpose is equal to its inverse:

\[ Q^T = Q^{-1} \]

template<std::size_t size, class T>
T Magnum::Math::Matrix<size, T>::trace() const

Trace of the matrix.

\[ tr(A) = \sum_{i=1}^n a_{i,i} \]

template<std::size_t size, class T>
Matrix<size-1, T> Magnum::Math::Matrix<size, T>::ij(std::size_t skipCol, std::size_t skipRow) const

Matrix without given column and row.

For the following matrix $ \boldsymbol{M} $ , $ \boldsymbol{M}_{3,2} $ is defined as:

\[ \begin{array}{rcl} \boldsymbol{M} & = & \begin{pmatrix} \,\,\,1 & 4 & 7 \\ \,\,\,3 & 0 & 5 \\ -1 & 9 & \!11 \\ \end{pmatrix} \\[2em] \boldsymbol{M}_{2,3} & = & \begin{pmatrix} \,\,1 & 4 & \Box\, \\ \,\Box & \Box & \Box\, \\ -1 & 9 & \Box\, \\ \end{pmatrix} = \begin{pmatrix} \,\,\,1 & 4\, \\ -1 & 9\, \\ \end{pmatrix} \end{array} \]

template<std::size_t size, class T>
T Magnum::Math::Matrix<size, T>::cofactor(std::size_t col, std::size_t row) const new in 2019.10

Cofactor.

Cofactor $ C_{i,j} $ of a matrix $ \boldsymbol{M} $ is defined as $ C_{i,j} = (-1)^{i + j} \det \boldsymbol{M}_{i,j} $ , with $ \boldsymbol{M}_{i,j} $ being $ \boldsymbol{M} $ without the i-th column and j-th row. For example, calculating $ C_{3,2} $ of $ \boldsymbol{M} $ defined as

\[ \boldsymbol{M} = \begin{pmatrix} \,\,\,1 & 4 & 7 \\ \,\,\,3 & 0 & 5 \\ -1 & 9 & \!11 \\ \end{pmatrix} \]

would be

\[ C_{3,2} = (-1)^{2 + 3} \det \begin{pmatrix} \,\,1 & 4 & \Box\, \\ \,\Box & \Box & \Box\, \\ -1 & 9 & \Box\, \\ \end{pmatrix} = -\det \begin{pmatrix} \,\,\,1 & 4\, \\ -1 & 9\, \\ \end{pmatrix} = -(9-(-4)) = -13 \]

template<std::size_t size, class T>
Matrix<size, T> Magnum::Math::Matrix<size, T>::comatrix() const new in 2019.10

Matrix of cofactors.

A cofactor matrix $ \boldsymbol{C} $ of a matrix $ \boldsymbol{M} $ is defined as the following, with each $ C_{i,j} $ calculated as in cofactor().

\[ \boldsymbol C = \begin{pmatrix} C_{1,1} & C_{2,1} & \cdots & C_{n,1} \\ C_{1,2} & C_{2,2} & \cdots & C_{n,2} \\ \vdots & \vdots & \ddots & \vdots \\ C_{1,n} & C_{2,n} & \cdots & C_{n,n} \end{pmatrix} \]

template<std::size_t size, class T>
Matrix<size, T> Magnum::Math::Matrix<size, T>::adjugate() const new in 2019.10

Adjugate matrix.

$ adj(A) $ . Transpose of a comatrix(), used for example to calculate an inverted() matrix.

template<std::size_t size, class T>
T Magnum::Math::Matrix<size, T>::determinant() const

Determinant.

Returns 0 if the matrix is noninvertible and 1 if the matrix is orthogonal. Computed recursively using Laplace's formula:

\[ \det \boldsymbol{A} = \sum_{j=1}^n (-1)^{i+j} a_{i,j} \det \boldsymbol{A}_{i,j} \]

$ \boldsymbol{A}_{i,j} $ is $ \boldsymbol{A} $ without the i-th column and j-th row. The formula is recursed down to a 2x2 matrix, where the determinant is calculated directly:

\[ \det \boldsymbol{A} = a_{0, 0} a_{1, 1} - a_{1, 0} a_{0, 1} \]

template<std::size_t size, class T>
Matrix<size, T> Magnum::Math::Matrix<size, T>::inverted() const

Inverted matrix.

Calculated using Cramer's rule and adjugate(), or equivalently using a comatrix():

\[ \boldsymbol{A}^{-1} = \frac{1}{\det \boldsymbol{A}} adj(\boldsymbol{A}) = \frac{1}{\det \boldsymbol{A}} \boldsymbol{C}^T \]

See invertedOrthogonal(), Matrix3::invertedRigid() and Matrix4::invertedRigid() which are faster alternatives for particular matrix types.

template<std::size_t size, class T>
Matrix<size, T> Magnum::Math::Matrix<size, T>::invertedOrthogonal() const

Inverted orthogonal matrix.

Equivalent to transposed(), expects that the matrix is orthogonal.

\[ \boldsymbol{A}^{-1} = \boldsymbol{A}^T \]