High-precision calculations for one- and two-valence atomic systems
LinAlg::Matrix< T >

ok

template<typename T = double>
class LinAlg::Matrix< T >

Row-major dense matrix with arithmetic and linear algebra support.

Owns its data as a contiguous std::vector<T> stored in row-major order. Supports scalar types T = float, double, std::complex<float>, and std::complex<double>.

Provides:

  • Element access via operator[] (no bounds checking) and at()/operator() (bounds-checked).
  • Basic arithmetic: addition, subtraction, scalar multiply/divide, and matrix multiplication via BLAS.
  • Linear algebra via GSL: determinant(), inverse(), transpose().
  • Conversion utilities: conj(), real(), imag(), complex().
  • Non-owning views: row_view(), column_view(), matrix_view().
  • GSL interop: as_gsl_view().
Note
The scalar += and -= operators treat the scalar as a multiple of the identity: M += a adds a to all diagonal elements. Only valid for square matrices.

#include <Matrix.hpp>

+ Inheritance diagram for LinAlg::Matrix< T >:

Public Member Functions

 Matrix ()
 Default initialiser.
 
 Matrix (std::size_t rows, std::size_t cols)
 Initialise a blank matrix rows*cols, filled with 0.
 
 Matrix (std::size_t rows, std::size_t cols, const T &value)
 Initialise a matrix rows*cols, filled with 'value'.
 
 Matrix (std::size_t dimension)
 Initialise a blank square matrix dimension*dimension, filled with 0.
 
 Matrix (std::initializer_list< std::initializer_list< T > > ll)
 Initialise a matrix from initialiser list. {{},{},{}}. Each row must be same length.
 
 Matrix (std::size_t rows, std::size_t cols, std::initializer_list< T > l)
 Initialise a matrix from single initialiser list. {...}.
 
 Matrix (std::size_t rows, std::size_t cols, std::vector< T > &&v)
 Initialise a matrix from single initialiser list. {...}.
 
 Matrix (std::size_t rows, std::size_t cols, const std::vector< T > &v)
 Initialise a matrix from single initialiser list. {...}.
 
void resize (std::size_t rows, std::size_t cols)
 Resizes matrix to new dimension; all values reset to default.
 
void resize (std::size_t rows, std::size_t cols, const T &value)
 Resizes matrix to new dimension; all values reset to 'value'.
 
std::size_t rows () const
 Return rows [major index size].
 
std::size_t cols () const
 Return columns [minor index size].
 
std::size_t size () const
 Return rows*columns [total array size].
 
bool empty () const
 Bool - is empty? (same as size==0)
 
T * data ()
 Pointer to first element; for std::complex<T> this is complex<T>*, not T*.
 
const T * data () const
 Const pointer to first element; for std::complex<T> this is const complex<T>*, not const T*.
 
T * operator[] (std::size_t i)
 [] index access (no range checking). [i] returns pointer to ith row, mutable
 
const T * operator[] (std::size_t i) const
 [] index access (no range checking). [i] returns const pointer to ith row
 
T & at (std::size_t row_i, std::size_t col_j)
 at(i,j): element access with range checking, mutable
 
T at (std::size_t row_i, std::size_t col_j) const
 at(i,j): element access with range checking, const
 
const T & atc (std::size_t row_i, std::size_t col_j) const
 at(i,j): element access with range checking, const ref
 
T & operator() (std::size_t i, std::size_t j)
 () index access with range checking, mutable
 
T operator() (std::size_t i, std::size_t j) const
 () index access with range checking, const
 
auto begin ()
 iterators for underlying std::vector (entire data)
 
auto cbegin () const
 
auto end ()
 
auto cend () const
 
const T * row (std::size_t row) const
 Returns raw c pointer to start of a row.
 
View< T > row_view (std::size_t row)
 Returns a mutable 'View' of a row.
 
View< const T > row_view (std::size_t row) const
 Returns an immutable 'View' of a row.
 
View< T > column_view (std::size_t col)
 Returns a mutable 'View' of a column.
 
View< const T > column_view (std::size_t col) const
 Returns an immutable 'View' of a column.
 
Matrix_view< T > matrix_view ()
 Returns a mutable view of the entire matrix (no ownership, no resize)
 
Matrix_view< const T > matrix_view () const
 Returns an immutable view of the entire matrix (no ownership, no resize)
 
auto as_gsl_view ()
 Returns a GSL matrix view for use with GSL functions (no copy).
 
auto as_gsl_view () const
 Const GSL matrix view; see as_gsl_view()
 
T determinant () const
 Returns the determinant.
 
Matrix< T > & invert_in_place ()
 Inverts the matrix in place.
 
Matrix< T > inverse () const
 Returns inverse of the matrix; original is unchanged.
 
Matrix< T > transpose () const
 Returns the transpose of the matrix.
 
Matrix< T > & make_identity ()
 Constructs a diagonal unit matrix (identity), in place; only for square.
 
Matrix< T > & zero ()
 Sets all elements to zero, in place.
 
Matrix< T > conj () const
 Returns conjugate of matrix.
 
auto real () const
 Returns real part of complex matrix (changes type; returns a real matrix)
 
auto imag () const
 Returns imag part of complex matrix (changes type; returns a real matrix)
 
auto complex () const
 Converts a real to complex matrix (changes type; returns a complex matrix)
 
Matrix< T > & conj_in_place ()
 Conjugates matrix, in place.
 
Matrix< T > & mult_elements_by (const Matrix< T > &a)
 Elementwise multiply in place: M_ij *= a_ij.
 
Matrix< T > & operator+= (const Matrix< T > &rhs)
 In-place elementwise addition; dimensions must match.
 
Matrix< T > & operator-= (const Matrix< T > &rhs)
 In-place elementwise subtraction; dimensions must match.
 
Matrix< T > & operator*= (const T x)
 In-place scalar multiply: M_ij *= x.
 
Matrix< T > & operator/= (const T x)
 In-place scalar divide: M_ij /= x.
 
Matrix< T > & operator+= (T aI)
 M += aI: adds a to diagonal elements (scalar treated as a*Identity)
 
Matrix< T > & operator-= (T aI)
 M -= aI: subtracts a from diagonal elements (scalar treated as a*Identity)
 

Protected Attributes

std::size_t m_rows
 
std::size_t m_cols
 
std::vector< T > m_data {}
 

Friends

Matrix< T > mult_elements (Matrix< T > a, const Matrix< T > &b)
 Returns new matrix C with C_ij = A_ij * B_ij (elementwise product).
 
Matrix< T > operator+ (Matrix< T > lhs, const Matrix< T > &rhs)
 Elementwise addition: returns M + N.
 
Matrix< T > operator- (Matrix< T > lhs, const Matrix< T > &rhs)
 Elementwise subtraction: returns M - N.
 
Matrix< T > operator* (const T x, Matrix< T > rhs)
 Scalar multiply: returns x * M.
 
Matrix< T > operator* (Matrix< T > lhs, const T x)
 Scalar multiply: returns M * x.
 
Matrix< T > operator/ (Matrix< T > lhs, const T x)
 Scalar divide: returns M / x.
 
Matrix< T > operator+ (Matrix< T > M, T aI)
 Returns M + aI (adds a to diagonal)
 
Matrix< T > operator- (Matrix< T > M, T aI)
 Returns M - aI (subtracts a from diagonal)
 
template<typename U >
Matrix< U > operator* (const Matrix< U > &a, const Matrix< U > &b)
 Matrix multiplication: C_ij = sum_k A_ik*B_kj.
 
template<typename U >
std::ostream & operator<< (std::ostream &os, const Matrix< U > &a)
 Prints matrix elements row by row to output stream.
 

Constructor & Destructor Documentation

◆ Matrix() [1/8]

template<typename T = double>
LinAlg::Matrix< T >::Matrix ( )
inline

Default initialiser.

◆ Matrix() [2/8]

template<typename T = double>
LinAlg::Matrix< T >::Matrix ( std::size_t  rows,
std::size_t  cols 
)
inline

Initialise a blank matrix rows*cols, filled with 0.

◆ Matrix() [3/8]

template<typename T = double>
LinAlg::Matrix< T >::Matrix ( std::size_t  rows,
std::size_t  cols,
const T &  value 
)
inline

Initialise a matrix rows*cols, filled with 'value'.

◆ Matrix() [4/8]

template<typename T = double>
LinAlg::Matrix< T >::Matrix ( std::size_t  dimension)
inlineexplicit

Initialise a blank square matrix dimension*dimension, filled with 0.

◆ Matrix() [5/8]

template<typename T = double>
LinAlg::Matrix< T >::Matrix ( std::initializer_list< std::initializer_list< T > >  ll)
inline

Initialise a matrix from initialiser list. {{},{},{}}. Each row must be same length.

◆ Matrix() [6/8]

template<typename T = double>
LinAlg::Matrix< T >::Matrix ( std::size_t  rows,
std::size_t  cols,
std::initializer_list< T >  l 
)
inline

Initialise a matrix from single initialiser list. {...}.

◆ Matrix() [7/8]

template<typename T = double>
LinAlg::Matrix< T >::Matrix ( std::size_t  rows,
std::size_t  cols,
std::vector< T > &&  v 
)
inline

Initialise a matrix from single initialiser list. {...}.

◆ Matrix() [8/8]

template<typename T = double>
LinAlg::Matrix< T >::Matrix ( std::size_t  rows,
std::size_t  cols,
const std::vector< T > &  v 
)
inline

Initialise a matrix from single initialiser list. {...}.

Member Function Documentation

◆ resize() [1/2]

template<typename T = double>
void LinAlg::Matrix< T >::resize ( std::size_t  rows,
std::size_t  cols 
)
inline

Resizes matrix to new dimension; all values reset to default.

◆ resize() [2/2]

template<typename T = double>
void LinAlg::Matrix< T >::resize ( std::size_t  rows,
std::size_t  cols,
const T &  value 
)
inline

Resizes matrix to new dimension; all values reset to 'value'.

◆ rows()

template<typename T = double>
std::size_t LinAlg::Matrix< T >::rows ( ) const
inline

Return rows [major index size].

◆ cols()

template<typename T = double>
std::size_t LinAlg::Matrix< T >::cols ( ) const
inline

Return columns [minor index size].

◆ size()

template<typename T = double>
std::size_t LinAlg::Matrix< T >::size ( ) const
inline

Return rows*columns [total array size].

◆ empty()

template<typename T = double>
bool LinAlg::Matrix< T >::empty ( ) const
inline

Bool - is empty? (same as size==0)

◆ data() [1/2]

template<typename T = double>
T * LinAlg::Matrix< T >::data ( )
inline

Pointer to first element; for std::complex<T> this is complex<T>*, not T*.

◆ data() [2/2]

template<typename T = double>
const T * LinAlg::Matrix< T >::data ( ) const
inline

Const pointer to first element; for std::complex<T> this is const complex<T>*, not const T*.

◆ operator[]() [1/2]

template<typename T = double>
T * LinAlg::Matrix< T >::operator[] ( std::size_t  i)
inline

[] index access (no range checking). [i] returns pointer to ith row, mutable

◆ operator[]() [2/2]

template<typename T = double>
const T * LinAlg::Matrix< T >::operator[] ( std::size_t  i) const
inline

[] index access (no range checking). [i] returns const pointer to ith row

◆ at() [1/2]

template<typename T = double>
T & LinAlg::Matrix< T >::at ( std::size_t  row_i,
std::size_t  col_j 
)
inline

at(i,j): element access with range checking, mutable

◆ at() [2/2]

template<typename T = double>
T LinAlg::Matrix< T >::at ( std::size_t  row_i,
std::size_t  col_j 
) const
inline

at(i,j): element access with range checking, const

◆ atc()

template<typename T = double>
const T & LinAlg::Matrix< T >::atc ( std::size_t  row_i,
std::size_t  col_j 
) const
inline

at(i,j): element access with range checking, const ref

◆ operator()() [1/2]

template<typename T = double>
T & LinAlg::Matrix< T >::operator() ( std::size_t  i,
std::size_t  j 
)
inline

() index access with range checking, mutable

◆ operator()() [2/2]

template<typename T = double>
T LinAlg::Matrix< T >::operator() ( std::size_t  i,
std::size_t  j 
) const
inline

() index access with range checking, const

◆ begin()

template<typename T = double>
auto LinAlg::Matrix< T >::begin ( )
inline

iterators for underlying std::vector (entire data)

◆ row()

template<typename T = double>
const T * LinAlg::Matrix< T >::row ( std::size_t  row) const
inline

Returns raw c pointer to start of a row.

◆ row_view() [1/2]

template<typename T = double>
View< T > LinAlg::Matrix< T >::row_view ( std::size_t  row)
inline

Returns a mutable 'View' of a row.

◆ row_view() [2/2]

template<typename T = double>
View< const T > LinAlg::Matrix< T >::row_view ( std::size_t  row) const
inline

Returns an immutable 'View' of a row.

◆ column_view() [1/2]

template<typename T = double>
View< T > LinAlg::Matrix< T >::column_view ( std::size_t  col)
inline

Returns a mutable 'View' of a column.

◆ column_view() [2/2]

template<typename T = double>
View< const T > LinAlg::Matrix< T >::column_view ( std::size_t  col) const
inline

Returns an immutable 'View' of a column.

◆ matrix_view() [1/2]

template<typename T = double>
Matrix_view< T > LinAlg::Matrix< T >::matrix_view ( )
inline

Returns a mutable view of the entire matrix (no ownership, no resize)

◆ matrix_view() [2/2]

template<typename T = double>
Matrix_view< const T > LinAlg::Matrix< T >::matrix_view ( ) const
inline

Returns an immutable view of the entire matrix (no ownership, no resize)

◆ as_gsl_view() [1/2]

template<typename T >
auto LinAlg::Matrix< T >::as_gsl_view ( )

Returns a GSL matrix view for use with GSL functions (no copy).

Returns a gsl_matrix_view (or _float_view, _complex_view, _complex_float_view) wrapping the matrix data in place. Access the underlying GSL matrix via .matrix on the returned object. Allows use of all GSL/BLAS built-in functions without any data copy.

Supported types: double, float, std::complex<double>, std::complex<float>.

Note
Both the Matrix and the returned view must remain in scope during use; the view is non-owning.
Warning
Fails at runtime (assert) for unsupported scalar types.

◆ as_gsl_view() [2/2]

template<typename T >
auto LinAlg::Matrix< T >::as_gsl_view ( ) const

Const GSL matrix view; see as_gsl_view()

◆ determinant()

template<typename T >
T LinAlg::Matrix< T >::determinant ( ) const

Returns the determinant.

The original matrix is not modified.

Returns
Determinant of the matrix.
Note
Only available for T = double or T = std::complex<double>.
Warning
Only defined for square matrices; asserts otherwise.

◆ invert_in_place()

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::invert_in_place ( )

Inverts the matrix in place.

Returns
Reference to *this (the inverted matrix).
Note
Only available for T = double or T = std::complex<double>.
Warning
Only defined for square matrices; asserts otherwise.

◆ inverse()

template<typename T = double>
Matrix< T > LinAlg::Matrix< T >::inverse ( ) const
inline

Returns inverse of the matrix; original is unchanged.

Copies *this and calls invert_in_place() on the copy.

Note
Only available for T = double or T = std::complex<double>.

◆ transpose()

template<typename T >
Matrix< T > LinAlg::Matrix< T >::transpose ( ) const

Returns the transpose of the matrix.

Allocates and returns a new Matrix<T> of size cols() x rows() with elements transposed. Uses GSL gsl_matrix_transpose_memcpy for double, float, and their complex variants; falls back to a naive loop for other types.

Returns
New matrix of dimensions cols() x rows().

◆ make_identity()

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::make_identity ( )

Constructs a diagonal unit matrix (identity), in place; only for square.

◆ zero()

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::zero ( )

Sets all elements to zero, in place.

◆ conj()

template<typename T >
Matrix< T > LinAlg::Matrix< T >::conj ( ) const

Returns conjugate of matrix.

◆ real()

template<typename T >
auto LinAlg::Matrix< T >::real ( ) const

Returns real part of complex matrix (changes type; returns a real matrix)

◆ imag()

template<typename T >
auto LinAlg::Matrix< T >::imag ( ) const

Returns imag part of complex matrix (changes type; returns a real matrix)

◆ complex()

template<typename T >
auto LinAlg::Matrix< T >::complex ( ) const

Converts a real to complex matrix (changes type; returns a complex matrix)

◆ conj_in_place()

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::conj_in_place ( )

Conjugates matrix, in place.

◆ mult_elements_by()

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::mult_elements_by ( const Matrix< T > &  a)

Elementwise multiply in place: M_ij *= a_ij.

Each element of *this is multiplied by the corresponding element of a. Matrices must have the same dimensions.

Parameters
aMatrix whose elements multiply *this.
Returns
Reference to *this.
Warning
Dimensions must match; asserts otherwise.

◆ operator+=() [1/2]

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::operator+= ( const Matrix< T > &  rhs)

In-place elementwise addition; dimensions must match.

◆ operator-=() [1/2]

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::operator-= ( const Matrix< T > &  rhs)

In-place elementwise subtraction; dimensions must match.

◆ operator*=()

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::operator*= ( const T  x)

In-place scalar multiply: M_ij *= x.

◆ operator/=()

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::operator/= ( const T  x)

In-place scalar divide: M_ij /= x.

◆ operator+=() [2/2]

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::operator+= ( T  aI)

M += aI: adds a to diagonal elements (scalar treated as a*Identity)

◆ operator-=() [2/2]

template<typename T >
Matrix< T > & LinAlg::Matrix< T >::operator-= ( T  aI)

M -= aI: subtracts a from diagonal elements (scalar treated as a*Identity)

Friends And Related Symbol Documentation

◆ mult_elements

template<typename T = double>
Matrix< T > mult_elements ( Matrix< T >  a,
const Matrix< T > &  b 
)
friend

Returns new matrix C with C_ij = A_ij * B_ij (elementwise product).

Calls a.mult_elements_by(b) on a copy of a.

◆ operator+ [1/2]

template<typename T = double>
Matrix< T > operator+ ( Matrix< T >  lhs,
const Matrix< T > &  rhs 
)
friend

Elementwise addition: returns M + N.

◆ operator- [1/2]

template<typename T = double>
Matrix< T > operator- ( Matrix< T >  lhs,
const Matrix< T > &  rhs 
)
friend

Elementwise subtraction: returns M - N.

◆ operator* [1/3]

template<typename T = double>
Matrix< T > operator* ( const T  x,
Matrix< T >  rhs 
)
friend

Scalar multiply: returns x * M.

◆ operator* [2/3]

template<typename T = double>
Matrix< T > operator* ( Matrix< T >  lhs,
const T  x 
)
friend

Scalar multiply: returns M * x.

◆ operator/

template<typename T = double>
Matrix< T > operator/ ( Matrix< T >  lhs,
const T  x 
)
friend

Scalar divide: returns M / x.

◆ operator+ [2/2]

template<typename T = double>
Matrix< T > operator+ ( Matrix< T >  M,
T  aI 
)
friend

Returns M + aI (adds a to diagonal)

◆ operator- [2/2]

template<typename T = double>
Matrix< T > operator- ( Matrix< T >  M,
T  aI 
)
friend

Returns M - aI (subtracts a from diagonal)

◆ operator* [3/3]

template<typename T = double>
template<typename U >
Matrix< U > operator* ( const Matrix< U > &  a,
const Matrix< U > &  b 
)
friend

Matrix multiplication: C_ij = sum_k A_ik*B_kj.

◆ operator<<

template<typename T = double>
template<typename U >
std::ostream & operator<< ( std::ostream &  os,
const Matrix< U > &  a 
)
friend

Prints matrix elements row by row to output stream.


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