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.
 
Koperator() (size_t i, size_t j)
 Element access (mutable)
 
const Koperator() (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< Toperator* (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< Tmul_vec (const Vector< T > &x) const
 Multiply matrix by a vector using SIMD.
 
Matrix< Ktranspose () const
 Returns the transpose \( A^T \) of the matrix (column-major layout)
 
Matrix< Ktrace () const
 Returns the trace of a square matrix as a 1×1 matrix.
 
Matrix< Kinverse () const
 Compute the inverse of the matrix using Gauss–Jordan elimination.
 
K det () const
 Compute the determinant using Gaussian elimination.
 
size_t rank (K eps=K(1e-6)) const
 Compute the numerical rank of the matrix.
 

Public Attributes

size_t rows
 
size_t cols
 
aligned_vector< Kdata
 
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.

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 tensorium::Matrix< K, RowMajor >::cols, tensorium::Matrix< K, RowMajor >::data, tensorium::GemmKernelBigger< T >::matmul(), and tensorium::Matrix< K, RowMajor >::rows.

Here is the call 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 tensorium::Matrix< K, RowMajor >::cols, tensorium::Matrix< K, RowMajor >::data, tensorium::Matrix< K, RowMajor >::rows, tensorium::Matrix< K, RowMajor >::simd_width, and tensorium::Matrix< K, RowMajor >::size().

Here is the call 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(), tensorium::Matrix< K, RowMajor >::cols, tensorium::Matrix< K, RowMajor >::det(), f(), tensorium::Matrix< K, RowMajor >::operator()(), tensorium::Matrix< K, RowMajor >::rows, tensorium::Matrix< K, RowMajor >::simd_width, and tensorium::Matrix< K, RowMajor >::swap_rows().

Referenced by tensorium::Matrix< K, RowMajor >::det().

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 tensorium::Matrix< K, RowMajor >::cols, and tensorium::Matrix< K, RowMajor >::rows.

Referenced by tensorium::Matrix< K, RowMajor >::operator()(), and tensorium::Matrix< K, RowMajor >::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(), tensorium::Matrix< K, RowMajor >::cols, f(), tensorium::Matrix< K, RowMajor >::operator()(), tensorium::Matrix< K, RowMajor >::rows, and tensorium::Matrix< K, RowMajor >::swap_rows().

Here is the call 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 alpha, tensorium::Matrix< K, RowMajor >::cols, tensorium::Matrix< K, RowMajor >::data, tensorium::Matrix< K, RowMajor >::rows, tensorium::Matrix< K, RowMajor >::simd_width, and tensorium::Matrix< K, RowMajor >::size().

Here is the call 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 tensorium::Matrix< K, RowMajor >::cols, tensorium::Matrix< K, RowMajor >::rows, and tensorium::Vector< K >::size().

Here is the call 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 tensorium::Matrix< K, RowMajor >::data, and tensorium::Matrix< K, RowMajor >::index().

Referenced by tensorium::Matrix< K, RowMajor >::det(), tensorium::Matrix< K, RowMajor >::inverse(), and tensorium::Matrix< K, RowMajor >::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 tensorium::Matrix< K, RowMajor >::data, and tensorium::Matrix< K, RowMajor >::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 tensorium::Matrix< K, RowMajor >::cols, tensorium::Matrix< K, RowMajor >::rows, and tensorium::Vector< K >::size().

Here is the call graph for this function:

◆ print()

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

◆ 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(), tensorium::Matrix< K, RowMajor >::cols, f(), tensorium::Matrix< K, RowMajor >::rows, and tensorium::Matrix< K, RowMajor >::swap_rows().

Here is the call 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 tensorium::Matrix< K, RowMajor >::data, tensorium::Matrix< K, RowMajor >::simd_width, and tensorium::Matrix< K, RowMajor >::size().

Here is the call 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 tensorium::Matrix< K, RowMajor >::cols, and tensorium::Matrix< K, RowMajor >::rows.

Referenced by tensorium::Matrix< K, RowMajor >::add(), tensorium::Matrix< K, RowMajor >::lerp(), tensorium::Matrix< K, RowMajor >::scl(), and tensorium::Matrix< K, RowMajor >::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 tensorium::Matrix< K, RowMajor >::cols, tensorium::Matrix< K, RowMajor >::data, tensorium::Matrix< K, RowMajor >::rows, tensorium::Matrix< K, RowMajor >::simd_width, and tensorium::Matrix< K, RowMajor >::size().

Here is the call 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(), tensorium::Matrix< K, RowMajor >::cols, and tensorium::Matrix< K, RowMajor >::rows.

Referenced by tensorium::Matrix< K, RowMajor >::det(), tensorium::Matrix< K, RowMajor >::inverse(), tensorium::Matrix< K, RowMajor >::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 tensorium::Matrix< K, RowMajor >::cols, tensorium::Matrix< K, RowMajor >::operator()(), and tensorium::Matrix< K, RowMajor >::rows.

Here is the call 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 tensorium::Matrix< K, RowMajor >::cols, and tensorium::Matrix< K, RowMajor >::rows.

Member Data Documentation

◆ block_size

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

◆ cols

◆ data

◆ iscolumn

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

◆ rows

◆ simd_width


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