Template Class qmatrix#

Class Documentation#

template<class T = double>
class qmatrix#

A template matrix class for quantum (q) operators and general linear algebra.

qmatrix is a row-major matrix container for NRG quantum calculations, supporting:

  • Generic template types (double, std::complex<double>)

  • Dense matrix operations and eigenvalue decomposition

  • Kronecker products for tensor operations

  • Intel MKL optimized BLAS/LAPACK routines

The matrix is stored in flat STL vector in row-major order (LAPACK_ROW_MAJOR), with element (i,j) at index i*column+j.

Note

All indexing is 0-based. OpenMP parallelization used for large operations.

Template Parameters:

T – Matrix element type (default: double). Supports std::complex<double>.

Public Functions

inline qmatrix(const std::initializer_list<T> inputVec, size_t _row = 0, size_t _column = 0)#

Construct a matrix from an initializer list.

If no explicit shape is provided, the list is interpreted as a square matrix.

Parameters:
  • inputVec – Row-major values to initialize the matrix with.

  • _row – Number of rows; when zero the size is inferred as square.

  • _column – Number of columns; when zero the size is inferred as square.

Throws:

std::runtime_error – If a square matrix is implied but the flat size is not square.

inline qmatrix(const std::vector<T> &inputVec, size_t _row = 0, size_t _column = 0)#

Construct a matrix from an STL vector.

Parameters:
  • inputVec – Flat row-major values.

  • _row – Number of rows; zero implies a square matrix.

  • _column – Number of columns; zero implies a square matrix.

Throws:

std::runtime_error – If the supplied vector is not compatible with a square matrix.

inline qmatrix(size_t _row, size_t _column, T populate)#

Allocate a matrix with the given shape and fill value.

Parameters:
  • _row – Number of rows.

  • _column – Number of columns.

  • populate – Fill value assigned to every entry.

inline void resize(size_t _row = 0, size_t _column = 0, T populate = 0)#

Resize the matrix and repopulate all entries.

Parameters:
  • _row – Number of rows in the new matrix.

  • _column – Number of columns in the new matrix.

  • populate – Initial value for each new element.

inline void clear()#

Reset the matrix to an empty state.

inline qmatrix(size_t N, T populate)#

Construct a square matrix with a constant fill value.

Parameters:
  • N – Matrix dimension.

  • populate – Value assigned to each element.

inline qmatrix()#

Default constructor; creates an empty matrix.

inline T &operator()(size_t i)#

Access a single flattened element by linear index.

Parameters:

i – Linear storage index.

Returns:

Reference to the element at that index.

inline T operator()(size_t i) const#

Access a single flattened element by linear index in const context.

Parameters:

i – Linear storage index.

Returns:

Copy of the value at that index.

inline T &at(size_t i)#

Access a single flattened element by linear index.

Parameters:

i – Linear storage index.

Returns:

Reference to the element at that index.

inline T at(size_t i) const#

Access a single flattened element by linear index in const context.

Parameters:

i – Linear storage index.

Returns:

Copy of the value at that index.

inline size_t size() const#

Return the number of stored elements.

Returns:

The matrix capacity in flat storage, equal to row * column.

inline size_t getrow() const#

Return the number of rows.

Returns:

Row count.

inline size_t getcolumn() const#

Return the number of columns.

Returns:

Column count.

inline T &operator()(size_t i, size_t j)#

Access an element using row and column indices.

Parameters:
  • i – Row index.

  • j – Column index.

Returns:

Reference to the selected element.

inline T operator()(size_t i, size_t j) const#

Access an element using row and column indices in const context.

Parameters:
  • i – Row index.

  • j – Column index.

Returns:

Copy of the selected element.

inline T &at(size_t i, size_t j)#

Access an element using row and column indices with bounds-safe semantics.

Parameters:
  • i – Row index.

  • j – Column index.

Returns:

Reference to the selected element.

inline T at(size_t i, size_t j) const#

Access an element using row and column indices with const semantics.

Parameters:
  • i – Row index.

  • j – Column index.

Returns:

Copy of the selected element.

inline T sum() const#

Sum all matrix elements.

Returns:

Sum of every entry in the matrix.

inline T absSum() const#

Sum the absolute values of all matrix entries.

Returns:

Absolute-value sum.

inline T trace() const#

Compute the trace of the matrix.

Throws:

std::runtime_error – If the matrix is not square.

Returns:

Sum of the diagonal elements.

inline auto getdiagonal()#

Extract the diagonal entries as a vector.

Returns:

Vector containing the main diagonal.

inline qmatrix<T> id(size_t _row = 0) const#

Create an identity matrix of the requested size.

Parameters:

_row – Matrix dimension. When zero, the current square dimension is used.

Throws:

std::runtime_error – If the current matrix is not square.

Returns:

Identity matrix with dimension _row x _row.

inline qmatrix<double> real() const#

Extract the real part of a complex-valued matrix.

Returns:

Matrix containing real parts of each entry.

inline qmatrix<double> imag() const#

Extract the imaginary part of a complex-valued matrix.

Returns:

Matrix containing imaginary parts of each entry.

inline qmatrix<T> cTranspose() const#

Compute the conjugate transpose of the matrix.

Returns:

Transposed matrix with complex conjugation applied.

inline qmatrix operator*(const T &x) const#

Multiply the matrix by a scalar.

Parameters:

x – Scalar multiplier.

Returns:

Result of element-wise scalar multiplication.

inline qmatrix operator/(const T &x) const#

Divide the matrix by a scalar.

Parameters:

x – Scalar divisor.

Returns:

Result of element-wise scalar division.

inline T *data()#

Return a pointer to the underlying flat storage.

Returns:

Pointer to the internal data array.

inline const T *data() const#

Return a const pointer to the underlying flat storage.

Returns:

Const pointer to the internal data array.

inline auto begin() const#

Return a const iterator to the beginning of the underlying storage.

Returns:

Begin iterator.

inline auto end() const#

Return a const iterator to the end of the underlying storage.

Returns:

End iterator.

inline void display()#

Print the matrix contents to standard output.

inline qmatrix operator+(const qmatrix<T> &rhs) const#

Add two matrices of identical shape.

Parameters:

rhs – Matrix on the right-hand side.

Throws:

std::runtime_error – If the matrix dimensions differ.

Returns:

Element-wise sum.

inline qmatrix operator-(const qmatrix<T> &rhs) const#

Subtract two matrices of identical shape.

Parameters:

rhs – Matrix on the right-hand side.

Throws:

std::runtime_error – If the matrix dimensions differ.

Returns:

Element-wise difference.

inline qmatrix<T> dot(const qmatrix<T> &rhs, double talpha = 1.0)#

Multiply this matrix by another matrix using BLAS-backed matrix multiplication.

Parameters:
  • rhs – Right-hand matrix with compatible dimensions.

  • talpha – Optional scalar prefactor.

Throws:

std::runtime_error – If the inner dimensions do not match.

Returns:

Product matrix of dimension row x rhs.column.

inline std::vector<double> diag()#

Diagonalize a symmetric or Hermitian matrix and return eigenvalues.

Throws:

std::runtime_error – If the matrix is not square.

Returns:

Vector of eigenvalues in ascending order as returned by LAPACK.

inline std::tuple<qmatrix<T>, qmatrix<T>, cm_vec> nonsys_diag_complex()#

Solve the nonsymmetric eigenproblem for a complex matrix.

Throws:
  • std::runtime_error – If the matrix is not square.

  • std::invalid_argument – If the matrix is not complex-valued.

Returns:

Tuple containing left eigenvectors, right eigenvectors, and eigenvalues.

inline std::tuple<qmatrix<std::complex<T>>, qmatrix<std::complex<T>>, std::vector<std::complex<T>>> nonsys_diag_real()#

Solve the nonsymmetric real eigenproblem for a real-valued matrix.

Throws:

std::invalid_argument – If the matrix is not square or is not a qmatrix<double>.

Returns:

Tuple containing left vectors, right vectors, and complex eigenvalues.

inline qmatrix<T> krDot(const qmatrix<T> &rhs, double alpha = 1)#

Compute the Kronecker product between this matrix and another.

Parameters:
  • rhs – Right-hand matrix.

  • alpha – Optional scalar prefactor.

Returns:

Kronecker product matrix of dimension (row * rhs.row) by (column * rhs.column).

inline void unitary_transform(const qmatrix<T> &eigen_vector)#

Apply a unitary similarity transform using a supplied eigenvector basis.

Parameters:

eigen_vector – Matrix containing the eigenvectors to apply.

Friends

inline friend std::ostream &operator<<(std::ostream &out, const qmatrix<T> &val)#

Stream a matrix to an output stream for debugging and logging.

Parameters:
  • out – Output stream.

  • val – Matrix to print.

Returns:

Reference to the same output stream.