libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
Matrix< T, W, H > Class Template Reference

Generic fixed-size matrix with compile-time dimensions. More...

#include <matrix.h>

Public Types

using RowDataType = Array< T, W >
 Type alias for a single row of the matrix.
 
using DataType = Array< RowDataType, H >
 Type alias for the entire matrix storage (array of rows).
 

Public Member Functions

 Matrix ()
 Constructs a zero-initialized matrix.
 
 Matrix (const DataType &data)
 Constructs a matrix from existing data.
 
RowDataTypeoperator[] (size_t index)
 Accesses a row by index.
 
const RowDataTypeoperator[] (size_t index) const
 Accesses a row by index (const).
 
DataTypedata ()
 Returns a mutable reference to the underlying data.
 
const DataTypedata () const
 Returns a const reference to the underlying data.
 
size_t width () const
 Returns the number of columns.
 
size_t height () const
 Returns the number of rows.
 
bool isSquare () const
 Returns true if the matrix is square (W == H).
 
Matrix< T, H, Wtranspose () const
 Returns the transpose of this matrix.
 
Matrix< T, W, Hoperator+ (const Matrix< T, W, H > &other) const
 Element-wise matrix addition.
 
Matrix< T, W, Hoperator- (const Matrix< T, W, H > &other) const
 Element-wise matrix subtraction.
 
Matrix< T, W, Hoperator* (const T &scalar) const
 Scalar multiplication.
 
template<size_t K>
Matrix< T, W, Koperator* (const Matrix< T, H, K > &other) const
 Matrix multiplication.
 
Matrix< T, W, Hoperator/ (const T &scalar) const
 Scalar division.
 
void LUdecomposition (Matrix< T, W, H > &L, Matrix< T, W, H > &U) const
 Computes the LU decomposition of this matrix.
 
T determinant () const
 Computes the determinant of a square matrix.
 
Matrix< T, W, Hinverse (Error *err=nullptr) const
 Computes the inverse of a square matrix.
 
template<size_t L>
T dot (const Matrix< T, L, 1 > &other) const
 Computes the dot product between two matrices treated as vectors.
 
template<typename Func >
Matrix< T, W, Happly (Func &&func) const
 Applies a function to each element of the matrix.
 
T frobeniusNorm () const
 Computes the Frobenius norm of the matrix.
 
T trace () const
 Computes the trace (sum of diagonal elements) of a square matrix.
 
T sum () const
 Computes the sum of all elements in the matrix.
 
Matrix< T, W, HhadamardProduct (const Matrix< T, W, H > &other) const
 Computes the Hadamard (element-wise) product of two matrices.
 
template<size_t L>
Matrix< T, H, Louter_product (const Matrix< T, L, 1 > &other) const
 Computes the outer product of two column vectors.
 
Matrix< T, 1, HrowSum () const
 Computes the sum of each row.
 
Matrix< T, W, 1 > colSum () const
 Computes the sum of each column.
 
Matrix< T, W, 1 > diagonal () const
 Extracts the diagonal elements of a square matrix.
 
Matrix< double, 1, HrowMean () const
 Computes the mean of each row.
 
Matrix< double, W, 1 > colMean () const
 Computes the mean of each column.
 
Matrix< T, H, WrotateCW () const
 Rotates the matrix 90 degrees clockwise.
 
Matrix< T, H, WrotateCCW () const
 Rotates the matrix 90 degrees counter-clockwise.
 

Static Public Member Functions

static Matrix< T, W, Hidentity ()
 Creates an identity matrix.
 
static Matrix< T, W, HrotationMatrix (T radians, size_t dim, Error *err=nullptr)
 Creates a rotation matrix for the given angle and dimension pair.
 

Detailed Description

template<typename T, size_t W, size_t H>
class Matrix< T, W, H >

Generic fixed-size matrix with compile-time dimensions.

Example
m.translate(1.0f, 2.0f, 3.0f);
auto inv = m.inverted();
Dynamic array container wrapping std::vector.
Definition list.h:40
static Matrix< T, W, H > identity()
Creates an identity matrix.
Definition matrix.h:48
Provides standard matrix operations including arithmetic, transposition, determinant, inverse, LU decomposition, and element-wise operations.
Template Parameters
TElement type (e.g. float, double).
WNumber of columns (width).
HNumber of rows (height).

Constructor & Destructor Documentation

◆ Matrix()

template<typename T , size_t W, size_t H>
Matrix< T, W, H >::Matrix ( const DataType data)
inline

Constructs a matrix from existing data.

Parameters
dataThe data to initialize from.

Member Function Documentation

◆ apply()

template<typename T , size_t W, size_t H>
template<typename Func >
Matrix< T, W, H > Matrix< T, W, H >::apply ( Func &&  func) const
inline

Applies a function to each element of the matrix.

Template Parameters
FuncA callable that takes a T and returns a T.
Parameters
funcThe function to apply.
Returns
A new matrix with the function applied to each element.

◆ colMean()

template<typename T , size_t W, size_t H>
Matrix< double, W, 1 > Matrix< T, W, H >::colMean ( ) const
inline

Computes the mean of each column.

Returns
A row vector containing the mean of each column.

◆ colSum()

template<typename T , size_t W, size_t H>
Matrix< T, W, 1 > Matrix< T, W, H >::colSum ( ) const
inline

Computes the sum of each column.

Returns
A row vector containing the sum of each column.

◆ determinant()

template<typename T , size_t W, size_t H>
T Matrix< T, W, H >::determinant ( ) const
inline

Computes the determinant of a square matrix.

Uses direct formulas for 2x2 and 3x3 matrices, and LU decomposition for larger sizes. Only valid for square matrices.

Returns
The determinant value.

◆ diagonal()

template<typename T , size_t W, size_t H>
Matrix< T, W, 1 > Matrix< T, W, H >::diagonal ( ) const
inline

Extracts the diagonal elements of a square matrix.

Returns
A column vector containing the diagonal elements.

◆ dot()

template<typename T , size_t W, size_t H>
template<size_t L>
T Matrix< T, W, H >::dot ( const Matrix< T, L, 1 > &  other) const
inline

Computes the dot product between two matrices treated as vectors.

Both matrices must be either row vectors or column vectors.

Template Parameters
LSize of the other vector.
Parameters
otherThe other vector matrix.
Returns
The scalar dot product.

◆ frobeniusNorm()

template<typename T , size_t W, size_t H>
T Matrix< T, W, H >::frobeniusNorm ( ) const
inline

Computes the Frobenius norm of the matrix.

Returns
The square root of the sum of squared elements.

◆ hadamardProduct()

template<typename T , size_t W, size_t H>
Matrix< T, W, H > Matrix< T, W, H >::hadamardProduct ( const Matrix< T, W, H > &  other) const
inline

Computes the Hadamard (element-wise) product of two matrices.

Parameters
otherThe matrix to multiply element-wise.
Returns
A new matrix containing the element-wise products.

◆ identity()

template<typename T , size_t W, size_t H>
static Matrix< T, W, H > Matrix< T, W, H >::identity ( )
inlinestatic

Creates an identity matrix.

Returns
An identity matrix with ones on the diagonal and zeros elsewhere.

◆ inverse()

template<typename T , size_t W, size_t H>
Matrix< T, W, H > Matrix< T, W, H >::inverse ( Error err = nullptr) const
inline

Computes the inverse of a square matrix.

Uses direct formulas for 2x2 and 3x3, Gaussian elimination for larger. Only valid for square matrices.

Parameters
errOptional error output; set to Error::SingularMatrix if the matrix is singular and cannot be inverted.
Returns
The inverse matrix, or a zero matrix on failure.

◆ LUdecomposition()

template<typename T , size_t W, size_t H>
void Matrix< T, W, H >::LUdecomposition ( Matrix< T, W, H > &  L,
Matrix< T, W, H > &  U 
) const
inline

Computes the LU decomposition of this matrix.

Parameters
LOutput lower triangular matrix.
UOutput upper triangular matrix.

◆ operator*() [1/2]

template<typename T , size_t W, size_t H>
template<size_t K>
Matrix< T, W, K > Matrix< T, W, H >::operator* ( const Matrix< T, H, K > &  other) const
inline

Matrix multiplication.

Template Parameters
KNumber of columns in the other matrix.
Parameters
otherThe right-hand-side matrix.
Returns
The product matrix of dimensions W x K.

◆ operator*() [2/2]

template<typename T , size_t W, size_t H>
Matrix< T, W, H > Matrix< T, W, H >::operator* ( const T scalar) const
inline

Scalar multiplication.

Parameters
scalarThe scalar to multiply each element by.
Returns
A new matrix with each element multiplied by scalar.

◆ operator+()

template<typename T , size_t W, size_t H>
Matrix< T, W, H > Matrix< T, W, H >::operator+ ( const Matrix< T, W, H > &  other) const
inline

Element-wise matrix addition.

Parameters
otherThe matrix to add.
Returns
A new matrix containing the element-wise sum.

◆ operator-()

template<typename T , size_t W, size_t H>
Matrix< T, W, H > Matrix< T, W, H >::operator- ( const Matrix< T, W, H > &  other) const
inline

Element-wise matrix subtraction.

Parameters
otherThe matrix to subtract.
Returns
A new matrix containing the element-wise difference.

◆ operator/()

template<typename T , size_t W, size_t H>
Matrix< T, W, H > Matrix< T, W, H >::operator/ ( const T scalar) const
inline

Scalar division.

Parameters
scalarThe scalar to divide each element by.
Returns
A new matrix with each element divided by scalar.

◆ operator[]() [1/2]

template<typename T , size_t W, size_t H>
RowDataType & Matrix< T, W, H >::operator[] ( size_t  index)
inline

Accesses a row by index.

Parameters
indexThe row index.
Returns
A mutable reference to the row.

◆ operator[]() [2/2]

template<typename T , size_t W, size_t H>
const RowDataType & Matrix< T, W, H >::operator[] ( size_t  index) const
inline

Accesses a row by index (const).

Parameters
indexThe row index.
Returns
A const reference to the row.

◆ outer_product()

template<typename T , size_t W, size_t H>
template<size_t L>
Matrix< T, H, L > Matrix< T, W, H >::outer_product ( const Matrix< T, L, 1 > &  other) const
inline

Computes the outer product of two column vectors.

This matrix must be a column vector (W == 1).

Template Parameters
LLength of the other column vector.
Parameters
otherThe other column vector.
Returns
The outer product matrix of dimensions H x L.

◆ rotateCCW()

template<typename T , size_t W, size_t H>
Matrix< T, H, W > Matrix< T, W, H >::rotateCCW ( ) const
inline

Rotates the matrix 90 degrees counter-clockwise.

Returns
A new matrix with dimensions transposed and elements rotated.

◆ rotateCW()

template<typename T , size_t W, size_t H>
Matrix< T, H, W > Matrix< T, W, H >::rotateCW ( ) const
inline

Rotates the matrix 90 degrees clockwise.

Returns
A new matrix with dimensions transposed and elements rotated.

◆ rotationMatrix()

template<typename T , size_t W, size_t H>
static Matrix< T, W, H > Matrix< T, W, H >::rotationMatrix ( T  radians,
size_t  dim,
Error err = nullptr 
)
inlinestatic

Creates a rotation matrix for the given angle and dimension pair.

Parameters
radiansThe rotation angle in radians.
dimThe dimension index for the rotation plane (must be less than W-1).
errOptional error output; set to Error::InvalidDimension on invalid dim.
Returns
A rotation matrix, or a zero matrix on error.

◆ rowMean()

template<typename T , size_t W, size_t H>
Matrix< double, 1, H > Matrix< T, W, H >::rowMean ( ) const
inline

Computes the mean of each row.

Returns
A column vector containing the mean of each row.

◆ rowSum()

template<typename T , size_t W, size_t H>
Matrix< T, 1, H > Matrix< T, W, H >::rowSum ( ) const
inline

Computes the sum of each row.

Returns
A column vector containing the sum of each row.

◆ sum()

template<typename T , size_t W, size_t H>
T Matrix< T, W, H >::sum ( ) const
inline

Computes the sum of all elements in the matrix.

Returns
The total sum.

◆ trace()

template<typename T , size_t W, size_t H>
T Matrix< T, W, H >::trace ( ) const
inline

Computes the trace (sum of diagonal elements) of a square matrix.

Returns
The trace value.

◆ transpose()

template<typename T , size_t W, size_t H>
Matrix< T, H, W > Matrix< T, W, H >::transpose ( ) const
inline

Returns the transpose of this matrix.

Returns
A new matrix with rows and columns swapped.

The documentation for this class was generated from the following file: