pbpt
Loading...
Searching...
No Matches
pbpt::math::Matrix< T, R, C > Class Template Reference

A template class for RxC dimensional mathematical matrices. More...

#include <matrix.hpp>

Public Types

template<int ViewR, int ViewC>
using MatView = MatrixView<T, R, C, ViewR, ViewC>
 Provides a view into a sub-region of the matrix.
using RowView = VectorView<T, C>
using ColView = VectorView<T, R>

Public Member Functions

constexpr Matrix () noexcept=default
 Default constructor. Initializes a zero matrix.
template<typename... Vecs>
requires (sizeof...(Vecs) == C && (std::is_same_v<Vector<T, R>, std::remove_cvref_t<Vecs>> && ...))
constexpr Matrix (Vecs &&... col_vecs) noexcept
 Constructs a matrix from a list of col vectors.
template<std::convertible_to< T >... Vals>
requires (sizeof...(Vals) == R * C && (std::is_arithmetic_v<std::remove_cvref_t<Vals>> && ...))
constexpr Matrix (Vals &&... vals) noexcept
 Constructs a matrix from a list of values.
constexpr const T & at (int r, int c) const
 Provides const access to matrix components by row and column.
constexpr RowView operator[] (int r)
 Returns a non-owning, mutable view of a row.
constexpr const RowView operator[] (int r) const
 Returns a non-owning, read-only view of a row.
constexpr RowView row (int r)
 Returns a non-owning, mutable view of a row.
constexpr const RowView row (int r) const
 Returns a non-owning, read-only view of a row.
constexpr ColView col (int c)
 Returns a non-owning, mutable view of a column.
constexpr const ColView col (int c) const
 Returns a non-owning, read-only view of a column.
constexpr int row_dims () const noexcept
 Returns the number of rows.
constexpr int col_dims () const noexcept
 Returns the number of columns.
const T * data () const noexcept
 Returns a pointer to the underlying contiguous data array.
T * data () noexcept
 Returns a pointer to the underlying contiguous data array.
constexpr bool operator== (const Matrix &rhs) const noexcept
 Compares two matrices for equality.
constexpr bool operator!= (const Matrix &rhs) const noexcept
 Compares two matrices for inequality.
constexpr Matrixoperator+= (const Matrix &rhs) noexcept
 Adds the elements of another matrix to this matrix.
constexpr Matrixoperator-= (const Matrix &rhs) noexcept
 Subtracts the elements of another matrix from this matrix.
constexpr Matrixoperator*= (T scalar) noexcept
 Multiplies this matrix by a scalar.
constexpr Matrix< T, C, R > transposed () const noexcept
 Computes the transpose of this matrix.
constexpr Matrix< T, C, R > & transpose () noexcept
 Computes the transpose of this matrix.
constexpr T determinant () const
 Computes the determinant of the matrix.
constexpr Matrix inversed () const
 Computes the inverse of the matrix.
constexpr Matrixinverse ()
 Computes the inverse of the matrix.
template<int ViewR, int ViewC>
constexpr Matrix::MatView< ViewR, ViewC > view (int row_start, int col_start)
 Creates a mutable, non-owning view into a sub-region of this matrix.
template<int ViewR, int ViewC>
constexpr const Matrix::MatView< ViewR, ViewC > view (int row_start, int col_start) const
 Creates a non-owning, read-only view into a sub-region of this const matrix.
constexpr auto submatrix (int row_to_remove, int col_to_remove) const
 Creates a submatrix by removing a specified row and column.
constexpr Matrix operator+ (const Matrix &rhs) const noexcept
 Matrix addition.
constexpr Matrix operator- (const Matrix &rhs) const noexcept
 Matrix subtraction.
template<std::convertible_to< T > U>
constexpr Matrix operator* (U scalar) const noexcept
 Matrix multiplication by a scalar.
constexpr Vector< T, R > operator* (const Vector< T, R > &rhs) const noexcept
 Matrix-Vector multiplication.
template<int M>
constexpr Matrix< T, R, M > operator* (const Matrix< T, C, M > &rhs) const noexcept
 Matrix-Matrix multiplication.
constexpr bool is_zero () const
 Checks if the matrix is zero.
constexpr bool is_identity () const
 Checks if the matrix is identity.
constexpr bool has_nan () const
 Checks if the matrix has NaN values.

Static Public Member Functions

static constexpr Matrix zeros () noexcept
 Creates a matrix with all components set to zero.
static constexpr Matrix identity () noexcept
 Creates an identity matrix.

Friends

template<std::convertible_to< T > U>
constexpr Matrix operator* (U scalar, const Matrix< T, R, C > &mat) noexcept
 Matrix multiplication by a scalar.
std::ostream & operator<< (std::ostream &os, const Matrix< T, R, C > &mat)
 Matrix stream output.

Detailed Description

template<typename T, int R, int C>
requires std::is_floating_point_v<T> && (R > 0) && (C > 0)
class pbpt::math::Matrix< T, R, C >

A template class for RxC dimensional mathematical matrices.

This class provides a generic, fixed-size matrix implementation using row-major memory layout. It's designed for high performance and flexibility, with extensive use of constexpr for compile-time computations.

Template Parameters
TThe underlying floating-point type.
RThe number of rows.
CThe number of columns.

Member Typedef Documentation

◆ MatView

template<typename T, int R, int C>
template<int ViewR, int ViewC>
using pbpt::math::Matrix< T, R, C >::MatView = MatrixView<T, R, C, ViewR, ViewC>

Provides a view into a sub-region of the matrix.

Template Parameters
R1The number of rows in the view.
C1The number of columns in the view.
Parameters
rThe starting row index of the view.
cThe starting column index of the view.
Returns
A MatrixView instance.

Constructor & Destructor Documentation

◆ Matrix() [1/2]

template<typename T, int R, int C>
template<typename... Vecs>
requires (sizeof...(Vecs) == C && (std::is_same_v<Vector<T, R>, std::remove_cvref_t<Vecs>> && ...))
pbpt::math::Matrix< T, R, C >::Matrix ( Vecs &&... col_vecs)
inlineexplicitconstexprnoexcept

Constructs a matrix from a list of col vectors.

The number of vectors must match the number of columns (C). Each vector's dimension must match the number of rows (R). The requires clause ensures this constructor is only enabled for Vector types.

Parameters
col_vecsA parameter pack of column vectors.

◆ Matrix() [2/2]

template<typename T, int R, int C>
template<std::convertible_to< T >... Vals>
requires (sizeof...(Vals) == R * C && (std::is_arithmetic_v<std::remove_cvref_t<Vals>> && ...))
pbpt::math::Matrix< T, R, C >::Matrix ( Vals &&... vals)
inlineconstexprnoexcept

Constructs a matrix from a list of values.

The number of values must match the number of elements (R * C).

Template Parameters
ValsThe parameter pack of values.

Member Function Documentation

◆ at()

template<typename T, int R, int C>
const T & pbpt::math::Matrix< T, R, C >::at ( int r,
int c ) const
inlineconstexpr

Provides const access to matrix components by row and column.

Parameters
rThe zero-based row index.
cThe zero-based column index.
Returns
A const reference to the component.
Exceptions
std::out_of_rangeIf index is out of bounds at runtime.

◆ data() [1/2]

template<typename T, int R, int C>
const T * pbpt::math::Matrix< T, R, C >::data ( ) const
inlinenoexcept

Returns a pointer to the underlying contiguous data array.

Useful for interoperability with graphics APIs like OpenGL. The data is in row-major order.

◆ data() [2/2]

template<typename T, int R, int C>
T * pbpt::math::Matrix< T, R, C >::data ( )
inlinenoexcept

Returns a pointer to the underlying contiguous data array.

Useful for interoperability with graphics APIs like OpenGL. The data is in row-major order.

◆ determinant()

template<typename T, int R, int C>
T pbpt::math::Matrix< T, R, C >::determinant ( ) const
inlineconstexpr

Computes the determinant of the matrix.

Note
Only available for square matrices.

◆ has_nan()

template<typename T, int R, int C>
bool pbpt::math::Matrix< T, R, C >::has_nan ( ) const
inlineconstexpr

Checks if the matrix has NaN values.

Returns
true If any element is NaN.
false Otherwise.

◆ identity()

template<typename T, int R, int C>
constexpr Matrix pbpt::math::Matrix< T, R, C >::identity ( )
inlinestaticconstexprnoexcept

Creates an identity matrix.

Note
Only available for square matrices (R == C).

◆ inverse()

template<typename T, int R, int C>
Matrix & pbpt::math::Matrix< T, R, C >::inverse ( )
inlineconstexpr

Computes the inverse of the matrix.

Note
Only available for square matrices.
Exceptions
std::runtime_errorif the matrix is singular (determinant is zero).

◆ inversed()

template<typename T, int R, int C>
Matrix pbpt::math::Matrix< T, R, C >::inversed ( ) const
inlineconstexpr

Computes the inverse of the matrix.

Note
Only available for square matrices.
Exceptions
std::runtime_errorif the matrix is singular (determinant is zero).

◆ is_identity()

template<typename T, int R, int C>
bool pbpt::math::Matrix< T, R, C >::is_identity ( ) const
inlineconstexpr

Checks if the matrix is identity.

Returns
true If the matrix is an identity matrix.
false Otherwise.

◆ is_zero()

template<typename T, int R, int C>
bool pbpt::math::Matrix< T, R, C >::is_zero ( ) const
inlineconstexpr

Checks if the matrix is zero.

Returns
true If all elements are zero.
false Otherwise.

◆ operator!=()

template<typename T, int R, int C>
bool pbpt::math::Matrix< T, R, C >::operator!= ( const Matrix< T, R, C > & rhs) const
inlineconstexprnoexcept

Compares two matrices for inequality.

Parameters
rhsThe matrix to compare with.
Returns
true if the matrices are not equal, false otherwise.

◆ operator*() [1/2]

template<typename T, int R, int C>
template<int M>
Matrix< T, R, M > pbpt::math::Matrix< T, R, C >::operator* ( const Matrix< T, C, M > & rhs) const
inlineconstexprnoexcept

Matrix-Matrix multiplication.

The number of columns in the left matrix must equal the number of rows in the right matrix.

Returns
A new Matrix<T, R, M>

◆ operator*() [2/2]

template<typename T, int R, int C>
template<std::convertible_to< T > U>
Matrix pbpt::math::Matrix< T, R, C >::operator* ( U scalar) const
inlineconstexprnoexcept

Matrix multiplication by a scalar.

Multiplies each element of the matrix by the scalar.

Returns
A new Matrix<T, R, C>

◆ operator*=()

template<typename T, int R, int C>
Matrix & pbpt::math::Matrix< T, R, C >::operator*= ( T scalar)
inlineconstexprnoexcept

Multiplies this matrix by a scalar.

Parameters
scalarThe scalar to multiply by.
Returns
A reference to this matrix.

◆ operator+()

template<typename T, int R, int C>
Matrix pbpt::math::Matrix< T, R, C >::operator+ ( const Matrix< T, R, C > & rhs) const
inlineconstexprnoexcept

Matrix addition.

Element-wise addition of two matrices.

Returns
A new Matrix<T, R, C>

◆ operator+=()

template<typename T, int R, int C>
Matrix & pbpt::math::Matrix< T, R, C >::operator+= ( const Matrix< T, R, C > & rhs)
inlineconstexprnoexcept

Adds the elements of another matrix to this matrix.

Parameters
rhsThe matrix to add.
Returns
A reference to this matrix.

◆ operator-()

template<typename T, int R, int C>
Matrix pbpt::math::Matrix< T, R, C >::operator- ( const Matrix< T, R, C > & rhs) const
inlineconstexprnoexcept

Matrix subtraction.

Element-wise subtraction of two matrices.

Returns
A new Matrix<T, R, C>

◆ operator-=()

template<typename T, int R, int C>
Matrix & pbpt::math::Matrix< T, R, C >::operator-= ( const Matrix< T, R, C > & rhs)
inlineconstexprnoexcept

Subtracts the elements of another matrix from this matrix.

Parameters
rhsThe matrix to subtract.
Returns
A reference to this matrix.

◆ operator==()

template<typename T, int R, int C>
bool pbpt::math::Matrix< T, R, C >::operator== ( const Matrix< T, R, C > & rhs) const
inlineconstexprnoexcept

Compares two matrices for equality.

Parameters
rhsThe matrix to compare with.
Returns
true if the matrices are equal, false otherwise.

◆ submatrix()

template<typename T, int R, int C>
auto pbpt::math::Matrix< T, R, C >::submatrix ( int row_to_remove,
int col_to_remove ) const
inlineconstexpr

Creates a submatrix by removing a specified row and column.

Note
Helper for determinant and inverse calculations.

◆ transpose()

template<typename T, int R, int C>
Matrix< T, C, R > & pbpt::math::Matrix< T, R, C >::transpose ( )
inlineconstexprnoexcept

Computes the transpose of this matrix.

Returns
A new Matrix<T, C, R> that is the transpose.

◆ transposed()

template<typename T, int R, int C>
Matrix< T, C, R > pbpt::math::Matrix< T, R, C >::transposed ( ) const
inlineconstexprnoexcept

Computes the transpose of this matrix.

Returns
A new Matrix<T, C, R> that is the transpose.

◆ view() [1/2]

template<typename T, int R, int C>
template<int ViewR, int ViewC>
Matrix::MatView< ViewR, ViewC > pbpt::math::Matrix< T, R, C >::view ( int row_start,
int col_start )
inlineconstexpr

Creates a mutable, non-owning view into a sub-region of this matrix.

Template Parameters
NewRThe number of rows in the view.
NewCThe number of columns in the view.
Parameters
row_startThe starting row index for the view.
col_startThe starting column index for the view.
Returns
A MatrixView object that refers to a part of this matrix.

◆ view() [2/2]

template<typename T, int R, int C>
template<int ViewR, int ViewC>
const Matrix::MatView< ViewR, ViewC > pbpt::math::Matrix< T, R, C >::view ( int row_start,
int col_start ) const
inlineconstexpr

Creates a non-owning, read-only view into a sub-region of this const matrix.

Template Parameters
ViewRThe number of rows in the view.
ViewCThe number of columns in the view.
Parameters
row_startThe starting row index for the view.
col_startThe starting column index for the view.
Returns
A ConstMatrixView object that refers to a part of this matrix.

◆ operator*

template<typename T, int R, int C>
template<std::convertible_to< T > U>
Matrix operator* ( U scalar,
const Matrix< T, R, C > & mat )
friend

Matrix multiplication by a scalar.

Multiplies each element of the matrix by the scalar.

Returns
A new Matrix<T, R, C>

◆ operator<<

template<typename T, int R, int C>
std::ostream & operator<< ( std::ostream & os,
const Matrix< T, R, C > & mat )
friend

Matrix stream output.

Template Parameters
TThe matrix element type.
RThe number of rows in the matrix.
CThe number of columns in the matrix.
Parameters
osThe output stream.
matThe matrix to output.
Returns
std::ostream& The output stream.

The documentation for this class was generated from the following file:
  • /Users/jinceyang/Desktop/codebase/pbpt/engine/math/matrix.hpp