Tensorium
Loading...
Searching...
No Matches
tensorium::Matrix< K, RowMajor > Class Template Reference

High-performance aligned matrix class with SIMD support. More...

#include <Matrix.hpp>

Collaboration diagram for tensorium::Matrix< K, RowMajor >:

Public Types

using Simd = simd::SimdTraits<K, DefaultISA>
using reg = typename Simd::reg

Public Member Functions

 Matrix (size_t r, size_t c)
 Construct a matrix of size r × c, initialized with zeros.
size_t index (size_t i, size_t j) const
size_t size () const
 Return the total number of elements.
K & operator() (size_t i, size_t j)
 Element access (mutable).
const K & operator() (size_t i, size_t j) const
void print () const
 Print the matrix to stdout.
void swap_rows (size_t i, size_t j)
 Swap two rows of the matrix.
template<typename T>
Vector< T > operator* (const Vector< T > &v) const
 Multiply matrix by a vector (naïve fallback).
void add (const Matrix &m)
 In-place matrix addition: this += m.
void sub (const Matrix &m)
 In-place matrix subtraction: this -= m.
void scl (K a)
 In-place scalar multiplication: this *= a.
void lerp (const Matrix< K > &A, const Matrix< K > &B, K alpha)
 Linearly interpolate between two matrices: this = (1 - α) * A + α * B.
Matrix _mul_mat (const Matrix< K > &mat) const
 Multiply matrix by another matrix using optimized SIMD path.
template<typename T>
Vector< T > mul_vec (const Vector< T > &x) const
 Multiply matrix by a vector using SIMD.
Matrix< K > transpose () const
 Returns the transpose \( A^T \) of the matrix (column-major layout).
Matrix< K > trace () const
 Returns the trace of a square matrix as a 1×1 matrix.
Matrix< K > inverse () const
 Compute the inverse of the matrix using Gauss–Jordan elimination.
det () const
 Compute the determinant using Gaussian elimination.
size_t rank (K eps=K(1e-6)) const
 Compute the numerical rank of the matrix.
Matrixoperator+= (const Matrix &m)
Matrixoperator-= (const Matrix &m)
Matrixoperator*= (K alpha)

Public Attributes

size_t rows
size_t cols
aligned_vector< K > data
size_t block_size
bool iscolumn
size_t simd_width = Simd::width

Detailed Description

template<typename K, bool RowMajor = false>
class tensorium::Matrix< K, RowMajor >

High-performance aligned matrix class with SIMD support.

This class provides a dense matrix container with:

  • Aligned memory allocation for SIMD operations
  • Fast matrix arithmetic (add, sub, scale)
  • Optimized matrix-vector and matrix-matrix multiplication
  • Support for inversion, determinant, transpose, and rank
Template Parameters
KScalar type (float, double, etc.)

Member Typedef Documentation

◆ reg

template<typename K, bool RowMajor = false>
using tensorium::Matrix< K, RowMajor >::reg = typename Simd::reg

◆ Simd

template<typename K, bool RowMajor = false>
using tensorium::Matrix< K, RowMajor >::Simd = simd::SimdTraits<K, DefaultISA>

Constructor & Destructor Documentation

◆ Matrix()

template<typename K, bool RowMajor = false>
tensorium::Matrix< K, RowMajor >::Matrix ( size_t r,
size_t c )
inline

Construct a matrix of size r × c, initialized with zeros.

References block_size, cols, data, detect_optimal_block_size(), and rows.

Referenced by _mul_mat(), add(), det(), inverse(), lerp(), operator*=(), operator+=(), operator-=(), rank(), sub(), trace(), and transpose().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ _mul_mat()

template<typename K, bool RowMajor = false>
Matrix tensorium::Matrix< K, RowMajor >::_mul_mat ( const Matrix< K > & mat) const
inline

Multiply matrix by another matrix using optimized SIMD path.

Uses blocking and micro-kernels to avoid cache bottleneck with FMA/AVX units repartition. Fast-paths exist for 4×4, 8×8, and 16×16.

References cols, data, Matrix(), and rows.

Referenced by tensorium::mul_mat(), and tensorium::operator*().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add()

template<typename K, bool RowMajor = false>
void tensorium::Matrix< K, RowMajor >::add ( const Matrix< K, RowMajor > & m)
inline

In-place matrix addition: this += m.

References _MM_HINT_T0, _mm_prefetch, cols, data, Matrix(), rows, simd_width, and size().

Referenced by tensorium::add_mat(), tensorium::operator+(), and operator+=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ det()

template<typename K, bool RowMajor = false>
K tensorium::Matrix< K, RowMajor >::det ( ) const
inline

Compute the determinant using Gaussian elimination.

\[\det A = \prod_{i=1}^n U_{ii} \quad \text{with } A = LU \]

References MathsUtils::_abs(), cols, det(), Matrix(), operator()(), rows, simd_width, and swap_rows().

Referenced by det(), and tensorium::det_mat().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ index()

template<typename K, bool RowMajor = false>
size_t tensorium::Matrix< K, RowMajor >::index ( size_t i,
size_t j ) const
inline

References cols, and rows.

Referenced by operator()(), and operator()().

Here is the caller graph for this function:

◆ inverse()

template<typename K, bool RowMajor = false>
Matrix< K > tensorium::Matrix< K, RowMajor >::inverse ( ) const
inline

Compute the inverse of the matrix using Gauss–Jordan elimination.

Throws if the matrix is singular or not square. need to add a fallback for unsquare matrix if possible

References MathsUtils::_abs(), cols, Matrix(), operator()(), rows, and swap_rows().

Referenced by tensorium_RG::__attribute__(), tensorium::inv_mat_tensor(), tensorium::inverse_mat(), tensorium_RG::Metric< T >::invert_metric_tensor(), and tensorium_RG::Metric< T >::invert_spatial_metric().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lerp()

template<typename K, bool RowMajor = false>
void tensorium::Matrix< K, RowMajor >::lerp ( const Matrix< K > & A,
const Matrix< K > & B,
K alpha )
inline

Linearly interpolate between two matrices: this = (1 - α) * A + α * B.

References cols, data, Matrix(), rows, simd_width, and size().

Referenced by tensorium::lerp_mat().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mul_vec()

template<typename K, bool RowMajor = false>
template<typename T>
Vector< T > tensorium::Matrix< K, RowMajor >::mul_vec ( const Vector< T > & x) const
inline

Multiply matrix by a vector using SIMD.

This is a faster alternative to operator* when available.

References cols, rows, and tensorium::Vector< K >::size().

Referenced by tensorium::mul_vec().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator()() [1/2]

template<typename K, bool RowMajor = false>
K & tensorium::Matrix< K, RowMajor >::operator() ( size_t i,
size_t j )
inline

Element access (mutable).

References data, and index().

Referenced by det(), inverse(), and trace().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator()() [2/2]

template<typename K, bool RowMajor = false>
const K & tensorium::Matrix< K, RowMajor >::operator() ( size_t i,
size_t j ) const
inline

References data, and index().

Here is the call graph for this function:

◆ operator*()

template<typename K, bool RowMajor = false>
template<typename T>
Vector< T > tensorium::Matrix< K, RowMajor >::operator* ( const Vector< T > & v) const
inline

Multiply matrix by a vector (naïve fallback).

\[(Ax)_i = \sum_j A_{ij} x_j \]

References cols, rows, and tensorium::Vector< K >::size().

Here is the call graph for this function:

◆ operator*=()

template<typename K, bool RowMajor = false>
Matrix & tensorium::Matrix< K, RowMajor >::operator*= ( K alpha)
inline

References Matrix(), and scl().

Here is the call graph for this function:

◆ operator+=()

template<typename K, bool RowMajor = false>
Matrix & tensorium::Matrix< K, RowMajor >::operator+= ( const Matrix< K, RowMajor > & m)
inline

References add(), and Matrix().

Here is the call graph for this function:

◆ operator-=()

template<typename K, bool RowMajor = false>
Matrix & tensorium::Matrix< K, RowMajor >::operator-= ( const Matrix< K, RowMajor > & m)
inline

References Matrix(), and sub().

Here is the call graph for this function:

◆ print()

template<typename K, bool RowMajor = false>
void tensorium::Matrix< K, RowMajor >::print ( ) const
inline

Print the matrix to stdout.

References cols, and rows.

◆ rank()

template<typename K, bool RowMajor = false>
size_t tensorium::Matrix< K, RowMajor >::rank ( K eps = K(1e-6)) const
inline

Compute the numerical rank of the matrix.

Performs row echelon reduction and counts non-zero pivots.

References MathsUtils::_abs(), cols, Matrix(), rows, and swap_rows().

Referenced by tensorium::rank_mat().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scl()

template<typename K, bool RowMajor = false>
void tensorium::Matrix< K, RowMajor >::scl ( K a)
inline

In-place scalar multiplication: this *= a.

References _MM_HINT_T0, _mm_prefetch, data, simd_width, and size().

Referenced by tensorium::operator*(), operator*=(), and tensorium::scl_mat().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

template<typename K, bool RowMajor = false>
size_t tensorium::Matrix< K, RowMajor >::size ( ) const
inline

Return the total number of elements.

References cols, and rows.

Referenced by add(), lerp(), scl(), and sub().

Here is the caller graph for this function:

◆ sub()

template<typename K, bool RowMajor = false>
void tensorium::Matrix< K, RowMajor >::sub ( const Matrix< K, RowMajor > & m)
inline

In-place matrix subtraction: this -= m.

References _MM_HINT_T0, _mm_prefetch, cols, data, Matrix(), rows, simd_width, and size().

Referenced by tensorium::operator-(), operator-=(), and tensorium::sub_mat().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ swap_rows()

template<typename K, bool RowMajor = false>
void tensorium::Matrix< K, RowMajor >::swap_rows ( size_t i,
size_t j )
inline

Swap two rows of the matrix.

References MathsUtils::_swap(), cols, and rows.

Referenced by det(), inverse(), rank(), and tensorium::solver::Gauss< K >::raw_row_echelon().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ trace()

template<typename K, bool RowMajor = false>
Matrix< K > tensorium::Matrix< K, RowMajor >::trace ( ) const
inline

Returns the trace of a square matrix as a 1×1 matrix.

References cols, Matrix(), operator()(), and rows.

Referenced by tensorium::trace_mat().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ transpose()

template<typename K, bool RowMajor = false>
Matrix< K > tensorium::Matrix< K, RowMajor >::transpose ( ) const
inline

Returns the transpose \( A^T \) of the matrix (column-major layout).

References cols, Matrix(), and rows.

Referenced by tensorium::transpose_mat().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ block_size

template<typename K, bool RowMajor = false>
size_t tensorium::Matrix< K, RowMajor >::block_size

Referenced by Matrix().

◆ cols

◆ data

◆ iscolumn

template<typename K, bool RowMajor = false>
bool tensorium::Matrix< K, RowMajor >::iscolumn

◆ rows

◆ simd_width

template<typename K, bool RowMajor = false>
size_t tensorium::Matrix< K, RowMajor >::simd_width = Simd::width

Referenced by add(), det(), lerp(), scl(), and sub().


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