consteig
Compile-time eigenvalue and eigenvector computation for C++17
Loading...
Searching...
No Matches
Classes | Functions
Matrix

Fixed-size matrix type and arithmetic operations. More...

Classes

class  consteig::Matrix< T, R, C >
 Fixed-size matrix with compile-time dimensions. More...
 

Functions

template<typename T , Size R, Size C>
constexpr Matrix< T, C, Rconsteig::transpose (const Matrix< T, R, C > &mat)
 Matrix transpose.
 
template<typename T , Size R, Size C>
constexpr T consteig::trace (const Matrix< T, R, C > &mat)
 Trace: sum of diagonal elements.
 
template<typename T , Size R, Size C>
constexpr T consteig::determinant (const Matrix< T, R, C > &mat)
 Determinant via Laplace (cofactor) expansion.
 
template<typename T , Size R, Size C>
constexpr T consteig::norm (const Matrix< T, R, C > &mat)
 Frobenius (Euclidean) norm: sqrt(sum of squared elements).
 
template<typename T , Size R, Size C>
constexpr T consteig::dot (const Matrix< T, R, C > &lhs, const Matrix< T, R, C > &rhs)
 Dot product of two 1×N row vectors.
 
template<typename T , Size R, Size C, typename... Args>
constexpr Matrix< T, R, Cconsteig::make_matrix (Args... args)
 Construct a Matrix from a flat list of scalar arguments in row-major order.
 
template<typename To , typename From , Size R, Size C>
constexpr Matrix< To, R, Cconsteig::matrix_cast (const Matrix< From, R, C > &src)
 Convert a matrix from one element type to another.
 
template<typename T , Size R, Size C>
constexpr Matrix< T, R, Cconsteig::operator+ (const Matrix< T, R, C > &lhs, const Matrix< T, R, C > &rhs)
 Element-wise matrix addition.
 
template<typename T , Size R, Size C>
constexpr Matrix< T, R, Cconsteig::operator- (const Matrix< T, R, C > &lhs, const Matrix< T, R, C > &rhs)
 Element-wise matrix subtraction.
 
template<typename T , Size R1, Size C1, Size R2, Size C2>
constexpr Matrix< T, R1, C2consteig::operator* (const Matrix< T, R1, C1 > &lhs, const Matrix< T, R2, C2 > &rhs)
 Matrix multiplication: (R1×C1) * (R2×C2) → (R1×C2).
 
template<typename T , Size R, Size C>
constexpr Matrix< T, R, Cconsteig::operator* (const T &lhs, const Matrix< T, R, C > &rhs)
 Scalar-matrix multiplication.
 
template<typename T , Size S>
constexpr Matrix< T, S, Sconsteig::diagonal (const T val)
 Create an S×S matrix with val on the main diagonal and zeros elsewhere.
 
template<typename T , Size S>
constexpr Matrix< T, S, Sconsteig::eye ()
 Create an S×S identity matrix.
 
template<typename T , Size R, Size C>
constexpr T consteig::norm1 (const Matrix< T, R, C > &mat)
 1-norm (maximum absolute column sum).
 
template<typename T , Size R, Size C>
constexpr T consteig::normInf (const Matrix< T, R, C > &mat)
 Infinity-norm (maximum absolute row sum).
 
template<typename T , Size R, Size C>
constexpr Matrix< T, R, Cconsteig::sqrt (const Matrix< T, R, C > &mat)
 Element-wise square root.
 
template<typename T , Size N>
constexpr Matrix< T, N+1u, 1uconsteig::char_poly (const Matrix< T, N, N > &A)
 Monic characteristic polynomial via the Faddeev-LeVerrier algorithm.
 
template<typename T , Size R, Size C>
constexpr bool consteig::equalWithinMat (const Matrix< T, R, C > &a, const Matrix< T, R, C > &b, const T thresh)
 Element-wise approximate equality within an absolute tolerance.
 

Detailed Description

Fixed-size matrix type and arithmetic operations.

Function Documentation

◆ transpose()

template<typename T , Size R, Size C>
constexpr Matrix< T, C, R > consteig::transpose ( const Matrix< T, R, C > & mat)
constexpr

Matrix transpose.

Template Parameters
TScalar element type.
RNumber of rows in input (= columns in output).
CNumber of columns in input (= rows in output).
Parameters
matInput matrix.
Returns
New C×R matrix with rows and columns swapped.

◆ trace()

template<typename T , Size R, Size C>
constexpr T consteig::trace ( const Matrix< T, R, C > & mat)
constexpr

Trace: sum of diagonal elements.

Template Parameters
TScalar type.
RNumber of rows (must equal C).
CNumber of columns.
Parameters
matSquare input matrix.
Returns
Sum of mat(i,i) for all i.
Precondition
R == C (enforced by static_assert).

◆ determinant()

template<typename T , Size R, Size C>
constexpr T consteig::determinant ( const Matrix< T, R, C > & mat)
constexpr

Determinant via Laplace (cofactor) expansion.

Computes the determinant recursively. Time complexity is O(n!), so this is only practical for small matrices (n ≤ 4 or 5). Used internally by checkEigenValues only when R <= 4.

Template Parameters
TScalar type.
RNumber of rows (must equal C).
CNumber of columns.
Parameters
matSquare input matrix.
Returns
Determinant of mat.
Precondition
R == C (enforced by static_assert).

◆ norm()

template<typename T , Size R, Size C>
constexpr T consteig::norm ( const Matrix< T, R, C > & mat)
constexpr

Frobenius (Euclidean) norm: sqrt(sum of squared elements).

Template Parameters
TFloating-point scalar type.
RNumber of rows.
CNumber of columns.
Parameters
matInput matrix.
Returns
Non-negative Frobenius norm.

◆ dot()

template<typename T , Size R, Size C>
constexpr T consteig::dot ( const Matrix< T, R, C > & lhs,
const Matrix< T, R, C > & rhs )
constexpr

Dot product of two 1×N row vectors.

Computes lhs * rhs^T and returns the scalar result.

Template Parameters
TScalar element type.
RMust be 1 (enforced by static_assert).
CNumber of elements.
Parameters
lhsFirst row vector (1×C).
rhsSecond row vector (1×C).
Returns
Scalar dot product.

◆ make_matrix()

template<typename T , Size R, Size C, typename... Args>
constexpr Matrix< T, R, C > consteig::make_matrix ( Args... args)
constexpr

Construct a Matrix from a flat list of scalar arguments in row-major order.

Alternative to aggregate initialization when nested braces are inconvenient. Arguments are filled row by row, left to right, identical to how values appear when written out as a matrix on paper.

Template Parameters
TScalar element type.
RNumber of rows (compile-time).
CNumber of columns (compile-time).
ArgsDeduced scalar argument types; must all be convertible to T.

The number of arguments must equal R * C exactly (enforced by static_assert).

// 2x3 matrix, equivalent to aggregate init
static constexpr auto m = make_matrix<double, 2, 3>(1.0, 2.0, 3.0,
4.0, 5.0, 6.0);
constexpr T epsilon()
Machine epsilon for type T.
Definition utilities.hpp:82

◆ matrix_cast()

template<typename To , typename From , Size R, Size C>
constexpr Matrix< To, R, C > consteig::matrix_cast ( const Matrix< From, R, C > & src)
constexpr

Convert a matrix from one element type to another.

Performs an element-wise static_cast<To> on each element. This is the preferred way to change element type (e.g. double to float) since Matrix has no converting constructor in order to preserve aggregate initialization.

Template Parameters
ToTarget element type.
FromSource element type.
RNumber of rows (compile-time).
CNumber of columns (compile-time).
Parameters
srcInput matrix with element type From.
Returns
New matrix with element type To and the same dimensions.
static constexpr auto fmat = consteig::matrix_cast<float>(dmat);

◆ operator+()

template<typename T , Size R, Size C>
constexpr Matrix< T, R, C > consteig::operator+ ( const Matrix< T, R, C > & lhs,
const Matrix< T, R, C > & rhs )
constexpr

Element-wise matrix addition.

Template Parameters
TScalar element type.
RNumber of rows.
CNumber of columns.
Parameters
lhsLeft-hand operand.
rhsRight-hand operand.
Returns
New matrix where each element equals lhs(i,j) + rhs(i,j).

◆ operator-()

template<typename T , Size R, Size C>
constexpr Matrix< T, R, C > consteig::operator- ( const Matrix< T, R, C > & lhs,
const Matrix< T, R, C > & rhs )
constexpr

Element-wise matrix subtraction.

Template Parameters
TScalar element type.
RNumber of rows.
CNumber of columns.
Parameters
lhsLeft-hand operand.
rhsRight-hand operand.
Returns
New matrix where each element equals lhs(i,j) - rhs(i,j).

◆ operator*() [1/2]

template<typename T , Size R1, Size C1, Size R2, Size C2>
constexpr Matrix< T, R1, C2 > consteig::operator* ( const Matrix< T, R1, C1 > & lhs,
const Matrix< T, R2, C2 > & rhs )
constexpr

Matrix multiplication: (R1×C1) * (R2×C2) → (R1×C2).

Template Parameters
TScalar element type.
R1Rows of lhs.
C1Columns of lhs (must equal R2).
R2Rows of rhs (must equal C1).
C2Columns of rhs.
Parameters
lhsLeft matrix.
rhsRight matrix.
Returns
Product matrix of dimension R1×C2.
Precondition
C1 == R2 (enforced by static_assert).

◆ operator*() [2/2]

template<typename T , Size R, Size C>
constexpr Matrix< T, R, C > consteig::operator* ( const T & lhs,
const Matrix< T, R, C > & rhs )
constexpr

Scalar-matrix multiplication.

Template Parameters
TScalar and element type.
RNumber of rows.
CNumber of columns.
Parameters
lhsScalar multiplier.
rhsMatrix to scale.
Returns
New matrix where each element equals lhs * rhs(i,j).

◆ diagonal()

template<typename T , Size S>
constexpr Matrix< T, S, S > consteig::diagonal ( const T val)
constexpr

Create an S×S matrix with val on the main diagonal and zeros elsewhere.

Template Parameters
TScalar type.
SMatrix dimension.
Parameters
valValue to place on the diagonal.
Returns
S×S diagonal matrix.

◆ eye()

template<typename T , Size S>
constexpr Matrix< T, S, S > consteig::eye ( )
constexpr

Create an S×S identity matrix.

Template Parameters
TScalar type.
SMatrix dimension.
Returns
S×S identity matrix (1 on diagonal, 0 elsewhere).
static constexpr auto I = consteig::eye<double, 3>();

◆ norm1()

template<typename T , Size R, Size C>
constexpr T consteig::norm1 ( const Matrix< T, R, C > & mat)
constexpr

1-norm (maximum absolute column sum).

Template Parameters
TScalar type.
RNumber of rows.
CNumber of columns.
Parameters
matInput matrix.
Returns
Maximum sum of absolute values over all columns.

◆ normInf()

template<typename T , Size R, Size C>
constexpr T consteig::normInf ( const Matrix< T, R, C > & mat)
constexpr

Infinity-norm (maximum absolute row sum).

Template Parameters
TScalar type.
RNumber of rows.
CNumber of columns.
Parameters
matInput matrix.
Returns
Maximum sum of absolute values over all rows.

◆ sqrt()

template<typename T , Size R, Size C>
constexpr Matrix< T, R, C > consteig::sqrt ( const Matrix< T, R, C > & mat)
constexpr

Element-wise square root.

Applies sqrt to every element. Each element must be non-negative; a negative element triggers a compile-time error.

Template Parameters
TFloating-point scalar type.
RNumber of rows.
CNumber of columns.
Parameters
matInput matrix.
Returns
New matrix with result(i,j) = sqrt(mat(i,j)).

◆ char_poly()

template<typename T , Size N>
constexpr Matrix< T, N+1u, 1u > consteig::char_poly ( const Matrix< T, N, N > & A)
constexpr

Monic characteristic polynomial via the Faddeev-LeVerrier algorithm.

Computes det(lam*I - A) = lam^N + c_1*lam^(N-1) + ... + c_N using only matrix multiplications and traces; no eigenvalues, no complex arithmetic. Susceptible to catastrophic cancellation for near-repeated eigenvalues.

Template Parameters
TScalar type.
NMatrix dimension.
Parameters
ASquare NxN matrix.
Returns
Column vector of N+1 coefficients in descending power order, with result(0,0) = 1 (monic leading term).

◆ equalWithinMat()

template<typename T , Size R, Size C>
constexpr bool consteig::equalWithinMat ( const Matrix< T, R, C > & a,
const Matrix< T, R, C > & b,
const T thresh )
constexpr

Element-wise approximate equality within an absolute tolerance.

Returns true if every element satisfies |a(i,j) - b(i,j)| < thresh. Prefer this over operator== for floating-point matrices. Can also be called as a member function via Matrix::equalWithin.

Template Parameters
TScalar element type.
RNumber of rows.
CNumber of columns.
Parameters
aFirst matrix.
bSecond matrix.
threshAbsolute per-element tolerance.
Returns
true if all elements are within thresh of each other.