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 a different type.
template<class U, class = decltype(Implementation::RectangularMatrixConverter<size, size, T, U>::from(std::declval<U>()))>
Matrix(const U& other) explicit constexpr
Construct a matrix from external representation.
template<std::size_t otherCols, std::size_t otherRows>
Matrix(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>
Matrix(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>
Matrix(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.
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>
Magnum::Math::Matrix<size, T>::Matrix(ZeroInitT) explicit constexpr noexcept

Construct a zero-filled matrix.

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 a different 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 otherCols, std::size_t otherRows>
Magnum::Math::Matrix<size, T>::Matrix(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 size, class T> template<std::size_t otherCols, std::size_t otherRows>
Magnum::Math::Matrix<size, T>::Matrix(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 size, class T> template<std::size_t otherCols, std::size_t otherRows>
Magnum::Math::Matrix<size, T>::Matrix(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.

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

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

Whether the matrix is orthogonal.

Also called an orthonormal matrix. 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, ±1 if the matrix is orthogonal, 1 if it's a pure rotation and -1 if it contains a reflection. 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 \]