Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
RealMatrix Class Reference

Dense real-valued matrix with STL storage and GSL-backed linear algebra. More...

#include <Matrix.h>

Public Member Functions

 RealMatrix ()
 Constructs an empty 0×0 matrix.
 
 RealMatrix (std::vector< double > data_, std::size_t rows, std::size_t cols)
 Constructs a matrix from flat row-major data.
 
 RealMatrix (std::vector< std::vector< double > > data_)
 Constructs a matrix from nested row vectors.
 
 RealMatrix (std::size_t rows, std::size_t cols)
 Constructs a zero-initialized matrix of given shape.
 
double & at (size_t i, size_t j)
 Returns a mutable reference to element (i,j) with bounds checking.
 
double & unchecked_at (size_t i, size_t j)
 Returns a mutable reference to element (i,j) without bounds checking.
 
const double & at (size_t i, size_t j) const
 Returns a const reference to element (i,j) with bounds checking.
 
const double & unchecked_at (size_t i, size_t j) const
 Returns a const reference to element (i,j) without bounds checking.
 
std::size_t rows () const
 Returns the number of rows.
 
std::size_t cols () const
 Returns the number of columns.
 
void remove_row (std::size_t row_idx)
 Removes one row from the matrix.
 
void remove_column (std::size_t col_idx)
 Removes one column from the matrix.
 
void remove_row_and_column (std::size_t dim_idx)
 Removes one row and one column with the same index.
 
gsl_matrix_sptr to_gsl_matrix () const
 Converts this matrix to a newly allocated GSL matrix.
 
bool is_symmetric () const
 Checks whether the matrix is symmetric.
 
RealMatrix transpose () const
 Returns the transpose of the matrix.
 
EigenSystem eig () const
 Computes the eigensystem of a symmetric matrix.
 
SignedLogDet slogdet () const
 Computes the signed logarithmic determinant via LU decomposition.
 
RealMatrix inv () const
 Computes the inverse of the matrix via LU decomposition.
 
RealMatrix operator- () const
 Unary minus.
 
RealMatrixoperator+= (const RealMatrix &rhs)
 In-place matrix addition.
 
RealMatrixoperator-= (const RealMatrix &rhs)
 In-place matrix subtraction.
 
RealMatrixoperator*= (const RealMatrix &rhs)
 In-place matrix multiplication.
 
RealMatrixoperator*= (double scalar)
 In-place scalar multiplication.
 
RealMatrixoperator/= (double scalar)
 In-place scalar division.
 

Static Public Member Functions

static RealMatrix from_gsl_copy (const gsl_matrix *A)
 Creates a RealMatrix by copying data from a GSL matrix.
 

Public Attributes

 Number = Union[int, float]
 
 NestedList = Sequence[Sequence[Number]]
 

Friends

RealMatrix operator+ (RealMatrix lhs, const RealMatrix &rhs)
 Matrix addition.
 
RealMatrix operator- (RealMatrix lhs, const RealMatrix &rhs)
 Matrix subtraction.
 
RealMatrix operator* (RealMatrix lhs, const RealMatrix &rhs)
 Matrix product.
 
RealMatrix operator* (RealMatrix lhs, double scalar)
 Right scalar multiplication.
 
RealMatrix operator* (double scalar, RealMatrix rhs)
 Left scalar multiplication.
 
RealMatrix operator/ (RealMatrix lhs, double scalar)
 Scalar division.
 
std::ostream & operator<< (std::ostream &os, RealMatrix A)
 Stream output helper.
 

Detailed Description

Dense real-valued matrix with STL storage and GSL-backed linear algebra.

RealMatrix is a small utility class representing a dense matrix of double-precision real numbers. It stores data in a single contiguous std::vector<double> in row-major order.

Features include:

  • checked and unchecked element access,
  • shape queries,
  • row / column removal,
  • conversion to/from GSL matrices,
  • symmetry checks,
  • transpose,
  • eigendecomposition for symmetric matrices,
  • LU-based inverse and signed log-determinant,
  • basic arithmetic operations.

The class is intentionally lightweight and does not attempt to be a full linear algebra framework.

Definition at line 144 of file Matrix.h.

Constructor & Destructor Documentation

◆ RealMatrix() [1/4]

RealMatrix::RealMatrix ( )
inline

Constructs an empty 0×0 matrix.

Definition at line 149 of file Matrix.h.

◆ RealMatrix() [2/4]

RealMatrix::RealMatrix ( std::vector< double >  data_,
std::size_t  rows,
std::size_t  cols 
)

Constructs a matrix from flat row-major data.

Parameters
data_Flat row-major data buffer.
rowsNumber of rows.
colsNumber of columns.
Exceptions
std::invalid_argumentif data_.size() != rows * cols.

Definition at line 555 of file Matrix.cpp.

◆ RealMatrix() [3/4]

RealMatrix::RealMatrix ( std::vector< std::vector< double > >  data_)

Constructs a matrix from nested row vectors.

Parameters
data_Matrix data as data_[row][col].
Exceptions
std::invalid_argumentif the outer vector is empty or if rows do not all have the same length.

Definition at line 560 of file Matrix.cpp.

◆ RealMatrix() [4/4]

RealMatrix::RealMatrix ( std::size_t  rows,
std::size_t  cols 
)

Constructs a zero-initialized matrix of given shape.

Parameters
rowsNumber of rows.
colsNumber of columns.

Definition at line 584 of file Matrix.cpp.

Member Function Documentation

◆ at() [1/2]

double & RealMatrix::at ( size_t  i,
size_t  j 
)

Returns a mutable reference to element (i,j) with bounds checking.

Parameters
iRow index.
jColumn index.
Returns
Mutable reference to the requested entry.
Exceptions
std::out_of_rangeif (i,j) lies outside the matrix shape.

Definition at line 587 of file Matrix.cpp.

◆ at() [2/2]

const double & RealMatrix::at ( size_t  i,
size_t  j 
) const

Returns a const reference to element (i,j) with bounds checking.

Parameters
iRow index.
jColumn index.
Returns
Const reference to the requested entry.
Exceptions
std::out_of_rangeif (i,j) lies outside the matrix shape.

Definition at line 594 of file Matrix.cpp.

◆ cols()

std::size_t RealMatrix::cols ( ) const

Returns the number of columns.

Definition at line 605 of file Matrix.cpp.

◆ eig()

EigenSystem RealMatrix::eig ( ) const

Computes the eigensystem of a symmetric matrix.

Uses GSL symmetric eigendecomposition and sorts eigenvalues in descending order.

Returns
A struct containing:
  • D: diagonal matrix of eigenvalues,
  • P: matrix of eigenvectors.
Exceptions
std::invalid_argumentif the matrix is not square.
std::runtime_errorif the matrix is not symmetric.

Definition at line 710 of file Matrix.cpp.

◆ from_gsl_copy()

RealMatrix RealMatrix::from_gsl_copy ( const gsl_matrix *  A)
static

Creates a RealMatrix by copying data from a GSL matrix.

Parameters
ASource GSL matrix.
Returns
Copied matrix in row-major STL storage.

Definition at line 658 of file Matrix.cpp.

◆ inv()

RealMatrix RealMatrix::inv ( ) const

Computes the inverse of the matrix via LU decomposition.

Returns
Inverse matrix.
Exceptions
std::invalid_argumentif the matrix is not square.
std::runtime_errorif the matrix is singular.

Definition at line 750 of file Matrix.cpp.

◆ is_symmetric()

bool RealMatrix::is_symmetric ( ) const

Checks whether the matrix is symmetric.

The check is exact up to the project helper fpeq(...).

Returns
True if the matrix is square and symmetric, false otherwise.

Definition at line 684 of file Matrix.cpp.

◆ operator*=() [1/2]

RealMatrix & RealMatrix::operator*= ( const RealMatrix rhs)

In-place matrix multiplication.

Performs dense matrix multiplication using a blocked algorithm.

Parameters
rhsRight-hand side matrix.
Returns
Reference to *this.
Exceptions
std::invalid_argumentif inner dimensions do not match.

Definition at line 815 of file Matrix.cpp.

◆ operator*=() [2/2]

RealMatrix & RealMatrix::operator*= ( double  scalar)

In-place scalar multiplication.

Parameters
scalarMultiplicative factor.
Returns
Reference to *this.

Definition at line 856 of file Matrix.cpp.

◆ operator+=()

RealMatrix & RealMatrix::operator+= ( const RealMatrix rhs)

In-place matrix addition.

Parameters
rhsRight-hand side matrix.
Returns
Reference to *this.
Exceptions
std::invalid_argumentif shapes differ.

Definition at line 789 of file Matrix.cpp.

◆ operator-()

RealMatrix RealMatrix::operator- ( ) const

Unary minus.

Returns
Matrix with all coefficients negated.

Definition at line 777 of file Matrix.cpp.

◆ operator-=()

RealMatrix & RealMatrix::operator-= ( const RealMatrix rhs)

In-place matrix subtraction.

Parameters
rhsRight-hand side matrix.
Returns
Reference to *this.
Exceptions
std::invalid_argumentif shapes differ.

Definition at line 802 of file Matrix.cpp.

◆ operator/=()

RealMatrix & RealMatrix::operator/= ( double  scalar)

In-place scalar division.

Parameters
scalarDivisor.
Returns
Reference to *this.
Exceptions
std::invalid_argumentif scalar is numerically zero.

Definition at line 863 of file Matrix.cpp.

◆ remove_column()

void RealMatrix::remove_column ( std::size_t  col_idx)

Removes one column from the matrix.

All columns to the right of col_idx are shifted left by one.

Parameters
col_idxIndex of the column to remove.
Warning
The implementation assumes col_idx < cols(). It does not currently throw on invalid indices.

Definition at line 624 of file Matrix.cpp.

◆ remove_row()

void RealMatrix::remove_row ( std::size_t  row_idx)

Removes one row from the matrix.

All rows below row_idx are shifted up by one.

Parameters
row_idxIndex of the row to remove.
Warning
The implementation assumes row_idx < rows(). It does not currently throw on invalid indices.

Definition at line 609 of file Matrix.cpp.

◆ remove_row_and_column()

void RealMatrix::remove_row_and_column ( std::size_t  dim_idx)

Removes one row and one column with the same index.

This is useful for principal-submatrix extraction, e.g. when removing one nuisance direction from a covariance / correlation matrix.

Parameters
dim_idxIndex of the row and column to remove.
Warning
The implementation assumes a valid index.
Note
Implementation detail: the rewritten buffer must use the new column count (cols()-1) and the shifted column index.

Definition at line 639 of file Matrix.cpp.

◆ rows()

std::size_t RealMatrix::rows ( ) const

Returns the number of rows.

Definition at line 601 of file Matrix.cpp.

◆ slogdet()

SignedLogDet RealMatrix::slogdet ( ) const

Computes the signed logarithmic determinant via LU decomposition.

Returns
Signed-logdet representation of the determinant.
Exceptions
std::invalid_argumentif the matrix is not square.

Definition at line 731 of file Matrix.cpp.

◆ to_gsl_matrix()

gsl_matrix_sptr RealMatrix::to_gsl_matrix ( ) const

Converts this matrix to a newly allocated GSL matrix.

Returns
RAII-owned GSL matrix containing a full copy of the data.

Definition at line 672 of file Matrix.cpp.

◆ transpose()

RealMatrix RealMatrix::transpose ( ) const

Returns the transpose of the matrix.

Returns
Transposed matrix.

Definition at line 698 of file Matrix.cpp.

◆ unchecked_at() [1/2]

double & RealMatrix::unchecked_at ( size_t  i,
size_t  j 
)

Returns a mutable reference to element (i,j) without bounds checking.

Parameters
iRow index.
jColumn index.
Returns
Mutable reference to the requested entry.
Warning
No bounds check is performed.

Definition at line 873 of file Matrix.cpp.

◆ unchecked_at() [2/2]

const double & RealMatrix::unchecked_at ( size_t  i,
size_t  j 
) const

Returns a const reference to element (i,j) without bounds checking.

Parameters
iRow index.
jColumn index.
Returns
Const reference to the requested entry.
Warning
No bounds check is performed.

Definition at line 877 of file Matrix.cpp.

Friends And Related Symbol Documentation

◆ operator* [1/3]

RealMatrix operator* ( double  scalar,
RealMatrix  rhs 
)
friend

Left scalar multiplication.

Definition at line 447 of file Matrix.h.

◆ operator* [2/3]

RealMatrix operator* ( RealMatrix  lhs,
const RealMatrix rhs 
)
friend

Matrix product.

Definition at line 431 of file Matrix.h.

◆ operator* [3/3]

RealMatrix operator* ( RealMatrix  lhs,
double  scalar 
)
friend

Right scalar multiplication.

Definition at line 439 of file Matrix.h.

◆ operator+

RealMatrix operator+ ( RealMatrix  lhs,
const RealMatrix rhs 
)
friend

Matrix addition.

Definition at line 415 of file Matrix.h.

◆ operator-

RealMatrix operator- ( RealMatrix  lhs,
const RealMatrix rhs 
)
friend

Matrix subtraction.

Definition at line 423 of file Matrix.h.

◆ operator/

RealMatrix operator/ ( RealMatrix  lhs,
double  scalar 
)
friend

Scalar division.

Definition at line 455 of file Matrix.h.

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
RealMatrix  A 
)
friend

Stream output helper.

Prints the matrix in a nested bracket form.

Parameters
osOutput stream.
AMatrix to print.
Returns
Output stream.

Definition at line 439 of file Matrix.cpp.

Member Data Documentation

◆ NestedList

RealMatrix.NestedList = Sequence[Sequence[Number]]

Definition at line 18 of file RealMatrix.py.

◆ Number

RealMatrix.Number = Union[int, float]

Definition at line 17 of file RealMatrix.py.


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