HILA
Loading...
Searching...
No Matches
Matrix_t< n, m, T, Mtype > Class Template Reference

The main \( n \times m \) matrix type template Matrix_t. This is a base class type for "useful" types which are derived from this. More...

#include <matrix.h>

Public Member Functions

auto abs () const
 Returns absolute value of Matrix.
 
template<typename Rtype = typename std::conditional<n == m, Mtype, Matrix<m, n, T>>::type>
Rtype adjoint () const
 Adjoint of matrix.
 
const Array< n, m, T > & asArray () const
 Cast Matrix to Array.
 
template<int mm = m, std::enable_if_t< mm==1, int > = 0>
const DiagonalMatrix< n, T > & asDiagonalMatrix () const
 Cast Vector to DiagonalMatrix.
 
Vector< n, T > column (int c) const
 Returns column vector as value at index c.
 
Mtype conj () const
 Returns complex conjugate of Matrix.
 
template<typename Rtype = typename std::conditional<n == m, Mtype, Matrix<m, n, T>>::type>
Rtype dagger () const
 Hermitian conjugate of matrix.
 
det () const
 determinant function - if matrix size is < 5, use Laplace, otherwise LU
 
det_laplace () const
 Determinant of matrix, using Laplace method.
 
det_lu () const
 following calculate the determinant of a square matrix det() is the generic interface, using laplace for small matrices and LU for large
 
DiagonalMatrix< n, T > diagonal ()
 Return diagonal of square matrix.
 
template<int p, int q, typename S , typename R = hila::type_mul<T, S>>
dot (const Matrix< p, q, S > &rhs) const
 Dot product.
 
template<int q = n, int p = m, std::enable_if_t<(q==1||p==1), int > = 0>
e (const int i) const
 Standard array indexing operation for vectors.
 
e (const int i, const int j) const
 Standard array indexing operation for matrices.
 
template<typename Et , typename Mt , typename MT >
int eigen_hermitean (DiagonalMatrix< n, Et > &eigenvalues, Matrix_t< n, n, Mt, MT > &eigenvectors, enum hila::sort sorted=hila::sort::unsorted) const
 Calculate eigenvalues and -vectors of an hermitean (or symmetric) matrix.
 
eigen_result< Mtypeeigen_hermitean (enum hila::sort sorted=hila::sort::unsorted) const
 eigenvalues and -vectors of hermitean/symmetric matrix, alternative interface
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
const Mtypefill (const S rhs)
 Matrix fill.
 
Mtypegaussian_random (double width=1.0)
 Fills Matrix with gaussian random elements from gaussian distribution.
 
Matrix< n, m, hila::arithmetic_type< T > > imag () const
 Returns imaginary part of Matrix or Vector.
 
 Matrix_t ()=default
 Define default constructors to ensure std::is_trivial.
 
template<typename S = T, std::enable_if_t< hila::is_arithmetic< S >::value, int > = 0>
max () const
 Find max of Matrix only for arithmetic types.
 
template<typename S = T, std::enable_if_t< hila::is_arithmetic< S >::value, int > = 0>
min () const
 Find min of Matrix only for arithmetic types.
 
template<int p, int q, typename S , typename MT >
hila::type_mul< T, S > mul_trace (const Matrix_t< p, q, S, MT > &rm) const
 Multiply with given matrix and compute trace of result.
 
template<typename R , typename Mt >
void mult_by_2x2_left (int p, int q, const Matrix_t< 2, 2, R, Mt > &M)
 Multiply \( n \times m \)-matrix from the left by \( n \times m \) matrix defined by \( 2 \times 2 \) sub matrix.
 
template<typename R , typename Mt >
void mult_by_2x2_right (int p, int q, const Matrix_t< 2, 2, R, Mt > &M)
 Multiply \( n \times m \)-matrix from the right by \( n \times m \) matrix defined by \( 2 \times 2 \) sub matrix.
 
template<typename S = T, std::enable_if_t< hila::is_floating_point< hila::arithmetic_type< S > >::value, int > = 0>
hila::arithmetic_type< T > norm () const
 Calculate vector norm - sqrt of squarenorm.
 
template<typename S >
bool operator!= (const Matrix< n, m, S > &rhs) const
 Boolean operator != to check if matrices are exactly different.
 
template<int m, int n, typename T1 , typename T2 , typename Rt = hila::ntype_op<T1, T2>>
Rt operator* (const Matrix< 1, m, T1 > &A, const Matrix< n, 1, T2 > &B)
 Multiplication operator RowVector * Vector.
 
template<typename Mt , typename S , std::enable_if_t<(Mt::is_matrix() &&hila::is_complex_or_arithmetic< S >::value), int > = 0, typename Rt = hila::mat_scalar_type<Mt, S>>
Rt operator* (const Mt &mat, const S &rhs)
 Multiplication operator Matrix * scalar.
 
template<typename Mt1 , typename Mt2 , std::enable_if_t< Mt1::is_matrix() &&Mt2::is_matrix() &&!std::is_same< Mt1, Mt2 >::value, int > = 0, typename R = hila::ntype_op<hila::number_type<Mt1>, hila::number_type<Mt2>>, int n = Mt1::rows(), int m = Mt2::columns()>
Matrix< n, m, R > operator* (const Mt1 &a, const Mt2 &b)
 Multiplication operator.
 
template<typename Mt , typename S , std::enable_if_t<(Mt::is_matrix() &&hila::is_complex_or_arithmetic< S >::value), int > = 0, typename Rt = hila::mat_scalar_type<Mt, S>>
Rt operator* (const S &rhs, const Mt &mat)
 Multiplication operator Scalar * Matrix.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_mul< T, S > >::value, int > = 0>
Mtypeoperator*= (const DiagonalMatrix< m, S > &rhs)
 Multiply assign operator for DiagonalMatrix to Matrix.
 
template<int p, typename S , typename MT , std::enable_if_t< hila::is_assignable< T &, hila::type_mul< T, S > >::value, int > = 0>
Mtypeoperator*= (const Matrix_t< m, p, S, MT > &rhs)
 Multiply assign scalar or matrix.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_mul< T, S > >::value, int > = 0>
Mtypeoperator*= (const S rhs)
 Multiply assign operator scalar to Matrix.
 
const Mtypeoperator+ () const
 Addition operator.
 
template<typename Mtype , typename S , std::enable_if_t< Mtype::is_matrix() &&hila::is_complex_or_arithmetic< S >::value, int > = 0, typename Rtype = hila::mat_scalar_type<Mtype, S>>
Rtype operator+ (const Mtype &a, const S &b)
 Addition operator Matrix + scalar.
 
template<typename Mtype1 , typename Mtype2 , std::enable_if_t< Mtype1::is_matrix() &&Mtype2::is_matrix(), int > = 0, typename Rtype = hila::mat_x_mat_type<Mtype1, Mtype2>, std::enable_if_t<!std::is_same< Mtype1, Rtype >::value, int > = 0>
Rtype operator+ (const Mtype1 &a, const Mtype2 &b)
 Addition operator Matrix + Matrix.
 
template<typename Mtype , typename S , std::enable_if_t< Mtype::is_matrix() &&hila::is_complex_or_arithmetic< S >::value, int > = 0, typename Rtype = hila::mat_scalar_type<Mtype, S>>
Rtype operator+ (const S &b, const Mtype &a)
 Addition operator scalar + Matrix.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_plus< T, S > >::value, int > = 0>
Mtypeoperator+= (const DiagonalMatrix< n, S > &rhs)
 Addition assign operator for DiagonalMatrix to Matrix.
 
template<typename S , typename MT , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Mtypeoperator+= (const Matrix_t< n, m, S, MT > &rhs)
 Add assign operator Matrix to Matrix.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_plus< T, S > >::value, int > = 0>
Mtypeoperator+= (const S &rhs)
 Add assign operator scalar to Matrix.
 
Mtype operator- () const
 Unary operator.
 
template<typename Mtype , typename S , std::enable_if_t< Mtype::is_matrix() &&hila::is_complex_or_arithmetic< S >::value, int > = 0, typename Rtype = hila::mat_scalar_type<Mtype, S>>
Rtype operator- (const Mtype &a, const S &b)
 Subtraction operator Matrix - scalar.
 
template<typename Mtype1 , typename Mtype2 , std::enable_if_t< Mtype1::is_matrix() &&Mtype2::is_matrix(), int > = 0, typename Rtype = hila::mat_x_mat_type<Mtype1, Mtype2>>
Rtype operator- (const Mtype1 &a, const Mtype2 &b)
 Subtraction operator Matrix - Matrix.
 
template<typename Mtype , typename S , std::enable_if_t< Mtype::is_matrix() &&hila::is_complex_or_arithmetic< S >::value, int > = 0, typename Rtype = hila::mat_scalar_type<Mtype, S>>
Rtype operator- (const S &b, const Mtype &a)
 Subtraction operator Scalar - Matrix.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_plus< T, S > >::value, int > = 0>
Mtypeoperator-= (const DiagonalMatrix< n, S > &rhs)
 Subtract assign operator for DiagonalMatrix to Matrix.
 
template<typename S , typename MT , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Mtypeoperator-= (const Matrix_t< n, m, S, MT > &rhs)
 Subtract assign operator Matrix to MAtrix.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_minus< T, S > >::value, int > = 0>
Mtypeoperator-= (const S rhs)
 Subtract assign operator scalar to Matrix.
 
template<typename Mt , typename S , std::enable_if_t<(Mt::is_matrix() &&hila::is_complex_or_arithmetic< S >::value), int > = 0, typename Rt = hila::mat_scalar_type<Mt, S>>
Rt operator/ (const Mt &mat, const S &rhs)
 Division operator.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_div< T, S > >::value, int > = 0>
Mtypeoperator/= (const DiagonalMatrix< m, S > &rhs)
 Divide assign operator for DiagonalMatrix to Matrix.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_div< T, S > >::value, int > = 0>
Mtypeoperator/= (const S rhs)
 Divide assign oprator scalar with matrix.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Mtypeoperator= (const DiagonalMatrix< n, S > &rhs)
 Assignment from diagonal matrix.
 
template<typename S , typename MT , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Mtypeoperator= (const Matrix_t< n, m, S, MT > &rhs)
 Copy matrix assignment.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value &&n==m, int > = 0>
Mtypeoperator= (const S rhs)
 Assignment from scalar.
 
Mtypeoperator= (const std::nullptr_t &z)
 Zero assignment.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Mtypeoperator= (std::initializer_list< S > rhs)
 Initializer list assignment.
 
template<typename S >
bool operator== (const Matrix< n, m, S > &rhs) const
 Boolean operator == to determine if two matrices are exactly the same.
 
template<int q = n, int p = m, std::enable_if_t<(q==1||p==1), int > = 0>
operator[] (const int i) const
 Indexing operation [] defined only for vectors.
 
template<int p, int q, typename S , typename R = hila::type_mul<T, S>>
Matrix< n, p, R > outer_product (const Matrix< p, q, S > &rhs) const
 Outer product.
 
template<int N>
Mtype permute (const Vector< N, int > &permutation) const
 Permute elements of vector.
 
Mtype permute_columns (const Vector< m, int > &permutation) const
 permute columns of Matrix
 
Mtype permute_rows (const Vector< n, int > &permutation) const
 permute rows of Matrix
 
Mtyperandom ()
 dot with matrix - matrix
 
Matrix< n, m, hila::arithmetic_type< T > > real () const
 Returns real part of Matrix or Vector.
 
const RowVector< m, T > & row (int r) const
 Return reference to row in a matrix.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
void set_column (int c, const Vector< n, S > &v)
 get column of a matrix
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
void set_diagonal (const Vector< n, S > &v)
 Set diagonal of square matrix to Vector which is passed to the method.
 
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
void set_row (int r, const RowVector< m, S > &v)
 Set row of Matrix with RowVector if types are assignable.
 
Mtype sort (hila::sort order=hila::sort::ascending) const
 Sort method for Vector.
 
template<int N>
Mtype sort (Vector< N, int > &permutation, hila::sort order=hila::sort::ascending) const
 Sort method for Vector which returns permutation order.
 
hila::arithmetic_type< T > squarenorm () const
 Calculate square norm - sum of squared elements.
 
std::string str (int prec=8, char separator=' ') const
 
svd_result< Mtypesvd (enum hila::sort sorted=hila::sort::unsorted) const
 svd and svd_pivot - alternative interface
 
template<typename Et , typename Mt , typename MT >
int svd (Matrix_t< n, n, Mt, MT > &_U, DiagonalMatrix< n, Et > &_D, Matrix_t< n, n, Mt, MT > &_V, enum hila::sort sorted=hila::sort::unsorted) const
 Singular value decomposition: divide matrix A = U S V*, where U,V unitary and S diagonal matrix of real singular values. Unpivoted Jacobi rotations.
 
template<typename Et , typename Mt , typename MT >
int svd_pivot (Matrix_t< n, n, Mt, MT > &_U, DiagonalMatrix< n, Et > &_D, Matrix_t< n, n, Mt, MT > &_V, enum hila::sort sorted=hila::sort::unsorted) const
 Singular value decomposition: divide matrix A = U S V*, where U,V unitary and S diagonal matrix of real singular values. Fully pivoted Jacobi rotations.
 
trace () const
 Computes Trace for Matrix.
 
template<int mm = m, typename Rtype = typename std::conditional<n == m, Mtype, Matrix<m, n, T>>::type, std::enable_if_t<(mm !=1), int > = 0>
Rtype transpose () const
 Transpose of matrix.
 
template<int mm = m, std::enable_if_t< mm==1, int > = 0>
const RowVector< n, T > & transpose () const
 Transpose of vector.
 
template<int nn = n, std::enable_if_t< nn==1 &&m !=1, int > = 0>
const Vector< n, T > & transpose () const
 Transpose of RowVector.
 

Static Public Member Functions

static constexpr int columns ()
 Returns column length.
 
static constexpr bool is_square ()
 Returns true if matrix is a square matrix.
 
static constexpr bool is_vector ()
 Returns true if Matrix is a vector.
 
static constexpr int rows ()
 Define constant methods rows(), columns() - may be useful in template code.
 
template<int q = n, int p = m, std::enable_if_t< q==1, int > = 0>
static constexpr int size ()
 Returns size of Vector or square Matrix.
 

Public Attributes

c [n *m]
 The data as a one dimensional array.
 

Detailed Description

template<const int n, const int m, typename T, typename Mtype>
class Matrix_t< n, m, T, Mtype >

The main \( n \times m \) matrix type template Matrix_t. This is a base class type for "useful" types which are derived from this.

Uses curiously recurring template pattern (CRTP), where the last template parameter is the template itself

Example: the Matrix type below is defined as

template <int n, int m, typename T>
class Matrix : public Matrix_t<n, m, T, Matrix<n, m, T>> { .. }
The main matrix type template Matrix_t. This is a base class type for "useful" types which are deriv...
Definition matrix.h:102
Matrix class which defines matrix operations.
Definition matrix.h:1679

This pattern is used because stupid c++ makes it complicated to write generic code, in this case derived functions to return derived type

Template Parameters
nNumber of rows
mNumber of columns
TMatrix element type
MtypeSpecific "Matrix" type for CRTP

Definition at line 102 of file matrix.h.

Member Function Documentation

◆ abs()

template<const int n, const int m, typename T , typename Mtype >
auto Matrix_t< n, m, T, Mtype >::abs ( ) const
inline

Returns absolute value of Matrix.

For Matrix<n,m,Complex<T>> case type is changed to Matrix<n,m,T> as expected since absolute value of a complex number is real

Template Parameters
M
Returns
Mtype

Definition at line 1081 of file matrix.h.

◆ adjoint()

template<const int n, const int m, typename T , typename Mtype >
template<typename Rtype = typename std::conditional<n == m, Mtype, Matrix<m, n, T>>::type>
Rtype Matrix_t< n, m, T, Mtype >::adjoint ( ) const
inline

Adjoint of matrix.

Alias to dagger

Template Parameters
std::conditional<n,Mtype,Matrix<m,n,T>>::type
Returns
Rtype

Definition at line 1069 of file matrix.h.

◆ asArray()

template<const int n, const int m, typename T , typename Mtype >
const Array< n, m, T > & Matrix_t< n, m, T, Mtype >::asArray ( ) const
inline

Cast Matrix to Array.

used for array operations

Returns
Array<n, m, T>&

Definition at line 467 of file matrix.h.

◆ asDiagonalMatrix()

template<const int n, const int m, typename T , typename Mtype >
template<int mm = m, std::enable_if_t< mm==1, int > = 0>
const DiagonalMatrix< n, T > & Matrix_t< n, m, T, Mtype >::asDiagonalMatrix ( ) const
inline

Cast Vector to DiagonalMatrix.

Returns
DiagonalMatrix<n,T>

Definition at line 481 of file matrix.h.

◆ column()

template<const int n, const int m, typename T , typename Mtype >
Vector< n, T > Matrix_t< n, m, T, Mtype >::column ( int  c) const
inline

Returns column vector as value at index c.

M.random();
Vector<n,T> V = M.column(i);
Parameters
cindex of column vector to be returned
Returns
const Vector<n, T>

Definition at line 389 of file matrix.h.

◆ columns()

template<const int n, const int m, typename T , typename Mtype >
static constexpr int Matrix_t< n, m, T, Mtype >::columns ( )
inlinestaticconstexpr

Returns column length.

Returns
constexpr int

Definition at line 228 of file matrix.h.

◆ conj()

template<const int n, const int m, typename T , typename Mtype >
Mtype Matrix_t< n, m, T, Mtype >::conj ( ) const
inline

Returns complex conjugate of Matrix.

Returns
const Mtype

Definition at line 1036 of file matrix.h.

◆ dagger()

template<const int n, const int m, typename T , typename Mtype >
template<typename Rtype = typename std::conditional<n == m, Mtype, Matrix<m, n, T>>::type>
Rtype Matrix_t< n, m, T, Mtype >::dagger ( ) const
inline

Hermitian conjugate of matrix.

for square matrix return type is same, for non square it is Matrix<m,n,MyType>

Template Parameters
std::conditional<n,Mtype,Matrix<m,n,T>>::type
Returns
const Rtype

Definition at line 1052 of file matrix.h.

◆ det_laplace()

template<int n, int m, typename T , typename Mtype >
T Matrix_t< n, m, T, Mtype >::det_laplace

Determinant of matrix, using Laplace method.

Defined only for square matrices

For perfomance overloads exist for \( 1 \times 1 \), \( 2 \times 2 \) and \( 3 \times 3 \) matrices.

Parameters
matmatrix to compute determinant for
Returns
T result determinant

Definition at line 637 of file matrix_linalg.h.

◆ det_lu()

template<int n, int m, typename T , typename Mtype >
T Matrix_t< n, m, T, Mtype >::det_lu

following calculate the determinant of a square matrix det() is the generic interface, using laplace for small matrices and LU for large

Matrix determinant with LU decomposition.

Algorithm: numerical Recipes, 2nd ed. p. 47 ff Works for Real and Complex matrices Defined only for square matrices

Returns
Complex<radix> Determinant

Definition at line 677 of file matrix_linalg.h.

◆ diagonal()

template<const int n, const int m, typename T , typename Mtype >
DiagonalMatrix< n, T > Matrix_t< n, m, T, Mtype >::diagonal ( )
inline

Return diagonal of square matrix.

If called for non square matrix the program will throw an error.

DiagonalMatrix<n,T> D = M.diagonal();
Define type DiagonalMatrix<n,T>
Returns
Vector<n, T> returned vector.

Definition at line 430 of file matrix.h.

◆ dot()

template<const int n, const int m, typename T , typename Mtype >
template<int p, int q, typename S , typename R = hila::type_mul<T, S>>
R Matrix_t< n, m, T, Mtype >::dot ( const Matrix< p, q, S > &  rhs) const
inline

Dot product.

Only works between two Vector objects

.
.
.
V.dot(W); // valid operation
R dot(const Matrix< p, q, S > &rhs) const
Dot product.
Definition matrix.h:1251
.
.
.
V.dot(W); // not valid operation
Template Parameters
pRow length for rhs
qColumn length for rhs
SType for rhs
RGives resulting type of lhs and rhs multiplication
Parameters
rhsVector to compute dot product with
Returns
R Value of dot product

Definition at line 1251 of file matrix.h.

◆ e() [1/2]

template<const int n, const int m, typename T , typename Mtype >
template<int q = n, int p = m, std::enable_if_t<(q==1||p==1), int > = 0>
T Matrix_t< n, m, T, Mtype >::e ( const int  i) const
inline

Standard array indexing operation for vectors.

Accessing singular elements is insufficient, but Vector elements are often quite small.

MyType a = V.e(i) \\ i <= n
Complex definition.
Definition cmplx.h:50
T e(const int i, const int j) const
Standard array indexing operation for matrices.
Definition matrix.h:272
Template Parameters
qNumber of rows
pNumber of columns
Parameters
iIndex
Returns
T

Definition at line 298 of file matrix.h.

◆ e() [2/2]

template<const int n, const int m, typename T , typename Mtype >
T Matrix_t< n, m, T, Mtype >::e ( const int  i,
const int  j 
) const
inline

Standard array indexing operation for matrices.

Accessing singular elements is insufficient, but Matrix elements are often quite small.

Exammple for matrix:

MyType a = M.e(i,j); \\ i <= n, j <= m
Parameters
iRow index
jColumn index
Returns
T matrix element type

Definition at line 272 of file matrix.h.

◆ eigen_hermitean() [1/2]

template<int n, int m, typename T , typename Mtype >
template<typename Et , typename Mt , typename MT >
int Matrix_t< n, m, T, Mtype >::eigen_hermitean ( DiagonalMatrix< n, Et > &  E,
Matrix_t< n, n, Mt, MT > &  U,
enum hila::sort  sorted = hila::sort::unsorted 
) const

Calculate eigenvalues and -vectors of an hermitean (or symmetric) matrix.

Calculate eigenvalues and vectors of an hermitean or real symmetric matrix.

Returns the number of Jacobi iterations, or -1 if did not converge. Arguments will contain eigenvalues and eigenvectors in columns of the "eigenvectors" matrix. Computation is done in double precision despite the input matrix types.

Parameters
eigenvaluevecVector of computed eigenvalue
eigenvectorsEigenvectors as columns of \( n \times n \) Matrix

Algorithm uses fully pivoted Jacobi rotations.

Two interfaces:

H.eigen_hermitean(E, U, [optional: sort]); E: output is DiagnoalMatrix containing real eigenvalues U: nxn unitary matrix, columns are normalized eigenvectors

auto res = H.eigen_hermitean([optional: sort]); This saves the trouble of defining E and U (see below)

Thus, H = U E U^* and H U = U E

Both interfaces allow optional sorting according to eigenvalues: hila::sort::unsorted [default] hila::sort::ascending / descending

Example:

M = ... // make unitary
int rotations = M.eigen_hermitean(eigenvalues,eigenvectors,hila::sort::ascending);
Note
The matrix has to be hermitean/symmetric
Parameters
Ediagonal matrix of real eigenvalues
UUnitary nxn matrix of eigenvectors
sortedsorting of eigenvalues (default:unsorted)
Returns
int number of jacobi rotations

Definition at line 200 of file matrix_linalg.h.

◆ eigen_hermitean() [2/2]

template<int n, int m, typename T , typename Mtype >
eigen_result< Mtype > Matrix_t< n, m, T, Mtype >::eigen_hermitean ( enum hila::sort  sorted = hila::sort::unsorted) const

eigenvalues and -vectors of hermitean/symmetric matrix, alternative interface

result = eigen_hermitean(hila::sort [optional]) returns struct eigen_result<Mtype>, with fields eigenvalues: DiagonalMatrix of eigenvalues eigenvectors: nxn unitary matrix of eigenvectors

Example:

M[ALL] = .. // make symmetric
onsites(ALL) {
auto R = M[X].svd();
// Use EV decomposition to evaluate function: sin(M) = U sin(eigenvals) U^T
sn[X] = R.eigenvectors * sin(R.eigenvalues) * R.eigenvectors.dagger();
}
The field class implements the standard methods for accessing Fields. Hilapp replaces the parity acce...
Definition field.h:61
constexpr Parity ALL
bit pattern: 011

This interface saves the trouble of defining the eigenvalue and -vector variables.

Definition at line 306 of file matrix_linalg.h.

◆ fill()

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
const Mtype & Matrix_t< n, m, T, Mtype >::fill ( const S  rhs)
inline

Matrix fill.

Fills the matrix with element if it is assignable to matrix type T

Works as follows:

M.fill(2) \\ Matrix is filled with 2
Template Parameters
SScalar type to of rhs
Parameters
rhsElement to fill matrix with
Returns
const Mtype&

Definition at line 980 of file matrix.h.

◆ gaussian_random()

template<const int n, const int m, typename T , typename Mtype >
Mtype & Matrix_t< n, m, T, Mtype >::gaussian_random ( double  width = 1.0)
inline

Fills Matrix with gaussian random elements from gaussian distribution.

M.gaussian_random();
Parameters
width
Returns
Mtype&

Definition at line 1352 of file matrix.h.

◆ imag()

template<const int n, const int m, typename T , typename Mtype >
Matrix< n, m, hila::arithmetic_type< T > > Matrix_t< n, m, T, Mtype >::imag ( ) const
inline

Returns imaginary part of Matrix or Vector.

Returns
Matrix<n, m, hila::arithmetic_type<T>>

Definition at line 1114 of file matrix.h.

◆ is_square()

template<const int n, const int m, typename T , typename Mtype >
static constexpr bool Matrix_t< n, m, T, Mtype >::is_square ( )
inlinestaticconstexpr

Returns true if matrix is a square matrix.

Returns
true
false

Definition at line 138 of file matrix.h.

◆ is_vector()

template<const int n, const int m, typename T , typename Mtype >
static constexpr bool Matrix_t< n, m, T, Mtype >::is_vector ( )
inlinestaticconstexpr

Returns true if Matrix is a vector.

Returns
true
false

Definition at line 128 of file matrix.h.

◆ mul_trace()

template<const int n, const int m, typename T , typename Mtype >
template<int p, int q, typename S , typename MT >
hila::type_mul< T, S > Matrix_t< n, m, T, Mtype >::mul_trace ( const Matrix_t< p, q, S, MT > &  rm) const
inline

Multiply with given matrix and compute trace of result.

Slightly cheaper operation than

(M*N).trace()
T trace() const
Computes Trace for Matrix.
Definition matrix.h:1129
Template Parameters
p
q
S
MT
Parameters
rm
Returns
hila::type_mul<T, S>

Definition at line 1150 of file matrix.h.

◆ mult_by_2x2_left()

template<const int n, const int m, typename T , typename Mtype >
template<typename R , typename Mt >
void Matrix_t< n, m, T, Mtype >::mult_by_2x2_left ( int  p,
int  q,
const Matrix_t< 2, 2, R, Mt > &  M 
)
inline

Multiply \( n \times m \)-matrix from the left by \( n \times m \) matrix defined by \( 2 \times 2 \) sub matrix.

The multiplication is defined as follows, let \(M\) as the \( 2 \times 2 \) input matrix and \(B\) be (this) matrix, being the matrix stored in the object this method is called for. Let \(A = I\) be a \( n \times m \) unit matrix. We then set the values of A to be:

\begin{align} A_{p,p} = M_{0,0}, \hspace{5px} A_{p,q} = M_{0,1}, \hspace{5px} A_{q,p} = M_{1,0}, \hspace{5px} A_{q,q} = M_{1,1}. \end{align}

Then the resulting matrix will be:

\begin{align} B = A \cdot B \end{align}

Template Parameters
RElement type of M
MtMatrix type of M
Parameters
pFirst row and column
qSecond row and column
M\( 2 \times 2\) Matrix to multiply with

Definition at line 1532 of file matrix.h.

◆ mult_by_2x2_right()

template<const int n, const int m, typename T , typename Mtype >
template<typename R , typename Mt >
void Matrix_t< n, m, T, Mtype >::mult_by_2x2_right ( int  p,
int  q,
const Matrix_t< 2, 2, R, Mt > &  M 
)
inline

Multiply \( n \times m \)-matrix from the right by \( n \times m \) matrix defined by \( 2 \times 2 \) sub matrix.

See Matrix::mult_by_2x2_left, only difference being that the multiplication is from the right.

Template Parameters
RElement type of M
MtMatrix type of M
Parameters
pFirst row and column
qSecond row and column
M\( 2 \times 2\) Matrix to multiply with

Definition at line 1561 of file matrix.h.

◆ norm()

template<const int n, const int m, typename T , typename Mtype >
template<typename S = T, std::enable_if_t< hila::is_floating_point< hila::arithmetic_type< S > >::value, int > = 0>
hila::arithmetic_type< T > Matrix_t< n, m, T, Mtype >::norm ( ) const
inline

Calculate vector norm - sqrt of squarenorm.

Template Parameters
S
Returns
hila::arithmetic_type<T>

Definition at line 1183 of file matrix.h.

◆ operator!=()

template<const int n, const int m, typename T , typename Mtype >
template<typename S >
bool Matrix_t< n, m, T, Mtype >::operator!= ( const Matrix< n, m, S > &  rhs) const
inline

Boolean operator != to check if matrices are exactly different.

if matrices are exactly the same then this will return false

Template Parameters
SType for MAtrix which is being compared to
Parameters
rhsright hand side Matrix which we are comparing
Returns
true
false

Definition at line 580 of file matrix.h.

◆ operator*() [1/4]

template<int m, int n, typename T1 , typename T2 , typename Rt = hila::ntype_op<T1, T2>>
Rt operator* ( const Matrix< 1, m, T1 > &  A,
const Matrix< n, 1, T2 > &  B 
)

Multiplication operator RowVector * Vector.

Defined as standard dot product between vectors as long as vectors are of same length

//
// Fill V and W
//
auto S = V*W; // Results in MyType scalar
Template Parameters
mDimensions of RowVector
nDimension of Vector
T1Element type of RowVector
T2Element type of Vector
RtReturn type of T1 \( \cdot \) T2 product
Parameters
ARowVector to multiply
BVector to multiply
Returns
Rt

Definition at line 2410 of file matrix.h.

◆ operator*() [2/4]

template<typename Mt , typename S , std::enable_if_t<(Mt::is_matrix() &&hila::is_complex_or_arithmetic< S >::value), int > = 0, typename Rt = hila::mat_scalar_type<Mt, S>>
Rt operator* ( const Mt &  mat,
const S &  rhs 
)
inline

Multiplication operator Matrix * scalar.

Multiplication operator between Matrix and Scalar are defined in the usual way (element wise)

M.fill(1);
auto S = M*2; // Resulting Matrix is uniformly 2
Template Parameters
MtMatrix type of mat
sScalar type rhs
Parameters
matMatrix to multiply
rhsScalar to multiply
Returns
Rt

Definition at line 2445 of file matrix.h.

◆ operator*() [3/4]

template<typename Mt1 , typename Mt2 , std::enable_if_t< Mt1::is_matrix() &&Mt2::is_matrix() &&!std::is_same< Mt1, Mt2 >::value, int > = 0, typename R = hila::ntype_op<hila::number_type<Mt1>, hila::number_type<Mt2>>, int n = Mt1::rows(), int m = Mt2::columns()>
Matrix< n, m, R > operator* ( const Mt1 &  a,
const Mt2 &  b 
)
inline

Multiplication operator.

Multiplication type depends on the original types of the multiplied matrices. Defined for the following operations.

Matrix * Matrix

Standard matrix multiplication where LHS columns must match RHS rows

M.random();
N.random();
auto S = N * M; // Results in a 3 x 3 Matrix since N.rows() = 3 and M.columns = 3

Vector * RowVector is same as outer product which is equivalent to a matrix multiplication

auto S = W*V // Result in n x n Matrix of type MyType
Template Parameters
Mt1Matrix type for a
Mt2Matrix type for b
nNumber of rows
mNumber of columns
Parameters
aLeft Matrix or Vector
bRight Matrix or RowVector
Returns
Matrix<n, m, R>

Definition at line 2348 of file matrix.h.

◆ operator*() [4/4]

template<typename Mt , typename S , std::enable_if_t<(Mt::is_matrix() &&hila::is_complex_or_arithmetic< S >::value), int > = 0, typename Rt = hila::mat_scalar_type<Mt, S>>
Rt operator* ( const S &  rhs,
const Mt &  mat 
)
inline

Multiplication operator Scalar * Matrix.

Multiplication operator between Matrix and Scalar are defined in the usual way (element wise)

M.fill(1);
auto S = 2*M; // Resulting Matrix is uniformly 2
Template Parameters
MtMatrix type of mat
sScalar type rhs
Parameters
matMatrix to multiply
rhsScalar to multiply
Returns
Rt

Definition at line 2474 of file matrix.h.

◆ operator*=() [1/3]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_mul< T, S > >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator*= ( const DiagonalMatrix< m, S > &  rhs)
inline

Multiply assign operator for DiagonalMatrix to Matrix.

Simply defined as matrix multiplication, but since rhs is guaranteed to be diagonal the method is optimized to skip most of the elements.

Template Parameters
SElement type of rhs
Parameters
rhsDiagonalMatrix to multiply
Returns
Mtype&

Definition at line 933 of file matrix.h.

◆ operator*=() [2/3]

template<const int n, const int m, typename T , typename Mtype >
template<int p, typename S , typename MT , std::enable_if_t< hila::is_assignable< T &, hila::type_mul< T, S > >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator*= ( const Matrix_t< m, p, S, MT > &  rhs)
inline

Multiply assign scalar or matrix.

Multiplication works as defined for matrix multiplication

Matrix multiply assign only defined for square matrices, since the matrix dimensions would change otherwise.

.
. Fill matrices M and N
.
M *= N; \\ M = M*N
Parameters
rhsMatrix to multiply with
Returns
template <int p, typename S, typename MT, std::enable_if_t<hila::is_assignable<T &, hila::type_mul<T, S>>::value, int> = 0>&

Definition at line 829 of file matrix.h.

◆ operator*=() [3/3]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_mul< T, S > >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator*= ( const S  rhs)
inline

Multiply assign operator scalar to Matrix.

Multiply Matrix uniformly with scalar

.
. Fill whole matrix with 1
.
M *= 2 ; \\ M is filled with 2
Template Parameters
SScalar type of rhs
Parameters
rhsScalar to multiply
Returns
Mtype&

Definition at line 855 of file matrix.h.

◆ operator+() [1/4]

template<const int n, const int m, typename T , typename Mtype >
const Mtype & operator+ ( ) const
inline

Addition operator.

Identity operation

M == +M;
Template Parameters
Mtype1Matrix type for a
Mtype2Matrix type for b
Parameters
aLeft matrix
bRight matrix
Returns
Rtype

Definition at line 546 of file matrix.h.

◆ operator+() [2/4]

template<typename Mtype , typename S , std::enable_if_t< Mtype::is_matrix() &&hila::is_complex_or_arithmetic< S >::value, int > = 0, typename Rtype = hila::mat_scalar_type<Mtype, S>>
Rtype operator+ ( const Mtype a,
const S &  b 
)
inline

Addition operator Matrix + scalar.

Addition operator between matrix and scalar is defined in the usual way, where the scalar is added to the diagonal elements.

  • \( M + 2 = M + 2\cdot\mathbb{1} \)
M.fill(0);
S = M + 1; // Resulting matrix is identity matrix
Template Parameters
MtypeMatrix type of b
SType of scalar
Parameters
aMatrix to add to
bScalar to add
Returns
Rtype

Definition at line 2158 of file matrix.h.

◆ operator+() [3/4]

template<typename Mtype1 , typename Mtype2 , std::enable_if_t< Mtype1::is_matrix() &&Mtype2::is_matrix(), int > = 0, typename Rtype = hila::mat_x_mat_type<Mtype1, Mtype2>, std::enable_if_t<!std::is_same< Mtype1, Rtype >::value, int > = 0>
Rtype operator+ ( const Mtype1 &  a,
const Mtype2 &  b 
)
inline

Addition operator Matrix + Matrix.

Addition operator between matrices is defined in the usual way (element wise).

NOTE: Matrices must share same dimensionality.

M.fill(1);
N.fill(1);
S = M + N; // Resulting matrix is uniformly 2
Template Parameters
Mtype1Matrix type of a
Mtype2Matrix type of b
Parameters
aMatrix to add
bMatrix to add
Returns
Rtype Return matrix of compatible type between Mtype1 and Mtype2

Definition at line 2053 of file matrix.h.

◆ operator+() [4/4]

template<typename Mtype , typename S , std::enable_if_t< Mtype::is_matrix() &&hila::is_complex_or_arithmetic< S >::value, int > = 0, typename Rtype = hila::mat_scalar_type<Mtype, S>>
Rtype operator+ ( const S &  b,
const Mtype a 
)
inline

Addition operator scalar + Matrix.

Addition operator between Scalar and Matrix is defined in the usual way, where the scalar is treated as diagonal matrix which is then added to. \( 2 + M = 2\cdot\mathbb{1} + M \)

M = 0; // M = 0*I
R = 1 + M; // Resulting matrix is identity matrix
Template Parameters
MtypeMatrix type of a
Sscalar type of b
Parameters
bMatrix to add
aScalar to add to
Returns
Rtype

Definition at line 2195 of file matrix.h.

◆ operator+=() [1/3]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_plus< T, S > >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator+= ( const DiagonalMatrix< n, S > &  rhs)
inline

Addition assign operator for DiagonalMatrix to Matrix.

Works as long as Matrix to assign to is square

Template Parameters
SElement type of rhs
Parameters
rhsDiagonalMatrix to add
Returns
Mtype&

Definition at line 897 of file matrix.h.

◆ operator+=() [2/3]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , typename MT , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator+= ( const Matrix_t< n, m, S, MT > &  rhs)
inline

Add assign operator Matrix to Matrix.

M = 1;
N = 1;
M += N; \\M = 2*I
constexpr Imaginary_t< double > I(1.0)
Imaginary unit I - global variable.
Template Parameters
SElement type of rhs
MTMatrix type of rhs
Parameters
rhsMatrix to add
Returns
Mtype&

Definition at line 723 of file matrix.h.

◆ operator+=() [3/3]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_plus< T, S > >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator+= ( const S &  rhs)
inline

Add assign operator scalar to Matrix.

Adds scalar \( a \) to square matrix as \( M + a\cdot\mathbb{1} \)

M += 1 ; \\ M = 2*I
Template Parameters
SElement type of scalar rhs
Parameters
rhsSclar to add
Returns
Mtype&

Definition at line 772 of file matrix.h.

◆ operator-() [1/4]

template<const int n, const int m, typename T , typename Mtype >
Mtype Matrix_t< n, m, T, Mtype >::operator- ( ) const
inline

Unary operator.

Negation operation

M == -M;

Definition at line 521 of file matrix.h.

◆ operator-() [2/4]

template<typename Mtype , typename S , std::enable_if_t< Mtype::is_matrix() &&hila::is_complex_or_arithmetic< S >::value, int > = 0, typename Rtype = hila::mat_scalar_type<Mtype, S>>
Rtype operator- ( const Mtype a,
const S &  b 
)
inline

Subtraction operator Matrix - scalar.

Subtraction operator between matrix and scalar is defined in the usual way, where the scalar is subtracted from the diagonal elements.

\( M - 2 = M - 2\cdot\mathbb{1} \)

M = 2; // M = 2*I
R = M - 1; // Resulting matrix is identity matrix
Template Parameters
MtypeMatrix type of a
SScalar type of b
Parameters
aMatrix to subtract from
bScalar to subtract
Returns
Rtype

Definition at line 2227 of file matrix.h.

◆ operator-() [3/4]

template<typename Mtype1 , typename Mtype2 , std::enable_if_t< Mtype1::is_matrix() &&Mtype2::is_matrix(), int > = 0, typename Rtype = hila::mat_x_mat_type<Mtype1, Mtype2>>
Rtype operator- ( const Mtype1 &  a,
const Mtype2 &  b 
)
inline

Subtraction operator Matrix - Matrix.

Subtraction operator between matrices is defined in the usual way (element wise).

NOTE: Matrices must share same dimensionality.

M.fill(1);
N.fill(1);
S = M - N; // Resulting matrix is uniformly 0
Template Parameters
Mtype1Matrix type of a
Mtype2Matrix type of b
Parameters
aMatrix to subtract from
bMatrix to subtract
Returns
Rtype Return matrix of compatible type between Mtype1 and Mtype2

Definition at line 2120 of file matrix.h.

◆ operator-() [4/4]

template<typename Mtype , typename S , std::enable_if_t< Mtype::is_matrix() &&hila::is_complex_or_arithmetic< S >::value, int > = 0, typename Rtype = hila::mat_scalar_type<Mtype, S>>
Rtype operator- ( const S &  b,
const Mtype a 
)
inline

Subtraction operator Scalar - Matrix.

Subtraction operator between Scalar and Matrix is defined in the usual way, where the scalar is treated as diagonal matrix which is then subtracted from.

\( 2 - M = 2\cdot\mathbb{1} - M \)

M = 1; // M = 1*I
R = 2 - M; // Resulting matrix is identity matrix
Template Parameters
MtypeMatrix type a
SScalar type of b
Parameters
bScalar to subtract from
aMatrix to subtract
Returns
Rtype

Definition at line 2265 of file matrix.h.

◆ operator-=() [1/3]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_plus< T, S > >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator-= ( const DiagonalMatrix< n, S > &  rhs)
inline

Subtract assign operator for DiagonalMatrix to Matrix.

Works as long as Matrix to assign to is square

Template Parameters
SElement type of rhs
Parameters
rhsDiagonalMatrix to add
Returns
Mtype&

Definition at line 914 of file matrix.h.

◆ operator-=() [2/3]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , typename MT , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator-= ( const Matrix_t< n, m, S, MT > &  rhs)
inline

Subtract assign operator Matrix to MAtrix.

M = 3;
N = 1;
M -= N; \\M = 2*I
Parameters
rhsMatrix to subtract with
Returns
Mtype&

Definition at line 747 of file matrix.h.

◆ operator-=() [3/3]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_minus< T, S > >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator-= ( const S  rhs)
inline

Subtract assign operator scalar to Matrix.

Subtract scalar \( a \) to square matrix as \( M - a\cdot\mathbb{1} \)

M -= 1 ; \\ M = 2*I
Template Parameters
SElement type of scalar rhs
Parameters
rhsscalar to add
Returns
Mtype&

Definition at line 798 of file matrix.h.

◆ operator/()

template<typename Mt , typename S , std::enable_if_t<(Mt::is_matrix() &&hila::is_complex_or_arithmetic< S >::value), int > = 0, typename Rt = hila::mat_scalar_type<Mt, S>>
Rt operator/ ( const Mt &  mat,
const S &  rhs 
)
inline

Division operator.

Defined for following operations

Matrix / Scalar:

Division operator between Matrix and Scalar are defined in the usual way (element wise)

M.fill(2);
auto S = M/2; // Resulting matrix is uniformly 1
Template Parameters
MtMatrix type
SScalar type
Parameters
matMatrix to divide scalar with
rhsScalar to divide with
Returns
Rt Resulting Matrix

Definition at line 2504 of file matrix.h.

◆ operator/=() [1/2]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_div< T, S > >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator/= ( const DiagonalMatrix< m, S > &  rhs)
inline

Divide assign operator for DiagonalMatrix to Matrix.

Well defined since rhs is guaranteed to be Diagonal.

Let M be the Matrix which we divide and D be the DiagonalMatrix which we divide with then the operation is defined as \( M \cdot D^{-1} \).

Template Parameters
SElement type of rhs
Parameters
rhsDiagonalMatrix to divide
Returns
Mtype&

Definition at line 954 of file matrix.h.

◆ operator/=() [2/2]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, hila::type_div< T, S > >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator/= ( const S  rhs)
inline

Divide assign oprator scalar with matrix.

Divide works as defined for scalar matrix division.

.
. Fill whole matrix with 2
.
M /= 2 ; \\ M is filled with 1
Template Parameters
SScalar type of rhs
Parameters
rhsScalar to divide with
Returns

Definition at line 881 of file matrix.h.

◆ operator=() [1/5]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator= ( const DiagonalMatrix< n, S > &  rhs)
inline

Assignment from diagonal matrix.

Template Parameters
SElement type of Diagonal matrix
Parameters
rhsDiagonal matrix to assign
Returns
Mtype&

Definition at line 668 of file matrix.h.

◆ operator=() [2/5]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , typename MT , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator= ( const Matrix_t< n, m, S, MT > &  rhs)
inline

Copy matrix assignment.

.
. M_0 has values assigned to it
.
Matrix<n,m,MyType> M; \\ undefined matrix
M = M_0; \\ Assignment from M_0
Matrix()=default
Default constructo.
Parameters
rhsMatrix to assign
Returns

Definition at line 601 of file matrix.h.

◆ operator=() [3/5]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value &&n==m, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator= ( const S  rhs)
inline

Assignment from scalar.

Assigns the scalar to the diagonal elements as \( M = I\cdot a\)

M = a; M = I*a
double random()
Real valued uniform random number generator.
Definition hila_gpu.cpp:120
Template Parameters
SScalar type to assign
Parameters
rhsScalar to assign
Returns
Mtype&

Definition at line 645 of file matrix.h.

◆ operator=() [4/5]

template<const int n, const int m, typename T , typename Mtype >
Mtype & Matrix_t< n, m, T, Mtype >::operator= ( const std::nullptr_t &  z)
inline

Zero assignment.

M = 0; Zero matrix;
Parameters
z0
Returns
Mtype&

Definition at line 621 of file matrix.h.

◆ operator=() [5/5]

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
Mtype & Matrix_t< n, m, T, Mtype >::operator= ( std::initializer_list< S >  rhs)
inline

Initializer list assignment.

M = {1, 0
0, 1};
Template Parameters
SElement type of initializer list
Parameters
rhsInitializer list to assign
Returns
Mtype&

Definition at line 695 of file matrix.h.

◆ operator==()

template<const int n, const int m, typename T , typename Mtype >
template<typename S >
bool Matrix_t< n, m, T, Mtype >::operator== ( const Matrix< n, m, S > &  rhs) const
inline

Boolean operator == to determine if two matrices are exactly the same.

Tolerance for equivalence is zero, meaning that the matrices must be exactly the same.

Template Parameters
SType for Matrix which is being compared to
Parameters
rhsright hand side Matrix which we are comparing
Returns
true
false

Definition at line 561 of file matrix.h.

◆ operator[]()

template<const int n, const int m, typename T , typename Mtype >
template<int q = n, int p = m, std::enable_if_t<(q==1||p==1), int > = 0>
T Matrix_t< n, m, T, Mtype >::operator[] ( const int  i) const
inline

Indexing operation [] defined only for vectors.

Example:

MyType a = V[i] \\ i <= n
Template Parameters
qrow size n
pcolumn size m
Parameters
irow or vector index depending on which is being indexed
Returns
T

Definition at line 324 of file matrix.h.

◆ outer_product()

template<const int n, const int m, typename T , typename Mtype >
template<int p, int q, typename S , typename R = hila::type_mul<T, S>>
Matrix< n, p, R > Matrix_t< n, m, T, Mtype >::outer_product ( const Matrix< p, q, S > &  rhs) const
inline

Outer product.

Only works between two Vector objects

.
.
.
V.outer_product(W); \\ Valid operation
Matrix< n, p, R > outer_product(const Matrix< p, q, S > &rhs) const
Outer product.
Definition matrix.h:1294
.
.
.
V.outer_product(W); // not valid operation
Template Parameters
pRow length for rhs
qColumn length for rhs
SElement type for rhs
RType between lhs and rhs multiplication
Parameters
rhsVector to compute outer product with
Returns
Matrix<n, p, R>

Definition at line 1294 of file matrix.h.

◆ permute()

template<const int n, const int m, typename T , typename Mtype >
template<int N>
Mtype Matrix_t< n, m, T, Mtype >::permute ( const Vector< N, int > &  permutation) const
inline

Permute elements of vector.

Reordering is done based off of permutation vector

Parameters
permutationVector of integers to permute rows
Returns
Mtype

Definition at line 1415 of file matrix.h.

◆ permute_columns()

template<const int n, const int m, typename T , typename Mtype >
Mtype Matrix_t< n, m, T, Mtype >::permute_columns ( const Vector< m, int > &  permutation) const
inline

permute columns of Matrix

Reordering is done based off of permutation vector

Parameters
permutationVector of integers to permute columns
Returns
Mtype

Definition at line 1385 of file matrix.h.

◆ permute_rows()

template<const int n, const int m, typename T , typename Mtype >
Mtype Matrix_t< n, m, T, Mtype >::permute_rows ( const Vector< n, int > &  permutation) const
inline

permute rows of Matrix

Reordering is done based off of permutation vector

Parameters
permutationVector of integers to permute rows
Returns
Mtype

Definition at line 1399 of file matrix.h.

◆ random()

template<const int n, const int m, typename T , typename Mtype >
Mtype & Matrix_t< n, m, T, Mtype >::random ( )
inline

dot with matrix - matrix

Generate random elements

Fills Matrix with random elements from uniform distribution

M.random();
Returns
Mtype&

Definition at line 1330 of file matrix.h.

◆ real()

template<const int n, const int m, typename T , typename Mtype >
Matrix< n, m, hila::arithmetic_type< T > > Matrix_t< n, m, T, Mtype >::real ( ) const
inline

Returns real part of Matrix or Vector.

Returns
Matrix<n, m, hila::arithmetic_type<T>>

Definition at line 1101 of file matrix.h.

◆ row()

template<const int n, const int m, typename T , typename Mtype >
const RowVector< m, T > & Matrix_t< n, m, T, Mtype >::row ( int  r) const
inline

Return reference to row in a matrix.

Since the Matrix data is ordered in a row major order accessing a row returns a reference to the row.

M.random();
RowVector<n,T> V = M.row(i);
Parameters
rindex of row to be referenced
Returns
const RowVector<m, T>&

Definition at line 356 of file matrix.h.

◆ rows()

template<const int n, const int m, typename T , typename Mtype >
static constexpr int Matrix_t< n, m, T, Mtype >::rows ( )
inlinestaticconstexpr

Define constant methods rows(), columns() - may be useful in template code.

Returns row length

Returns
constexpr int

Definition at line 220 of file matrix.h.

◆ set_column()

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
void Matrix_t< n, m, T, Mtype >::set_column ( int  c,
const Vector< n, S > &  v 
)
inline

get column of a matrix

Set column of Matrix with Vector if types are assignable

V.random();
M.set_column(i,V);
Mtype & random()
dot with matrix - matrix
Definition matrix.h:1330
Template Parameters
SVector type
Parameters
cIndex of column to be set
vVector to be set

Definition at line 415 of file matrix.h.

◆ set_diagonal()

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
void Matrix_t< n, m, T, Mtype >::set_diagonal ( const Vector< n, S > &  v)
inline

Set diagonal of square matrix to Vector which is passed to the method.

If called for non square matrix the program will throw an error.

Example:

SquareMatrix<n,MyType> S = 0; \\ Zero matrix
Vector<n,MyType> V = 1; \\ Vector assigned to 1 at all elements
S.set_diagonal(V); \\ Results in Identity matrix of size n
void set_diagonal(const Vector< n, S > &v)
Set diagonal of square matrix to Vector which is passed to the method.
Definition matrix.h:454
static constexpr int size()
Returns size of Vector or square Matrix.
Definition matrix.h:242
Template Parameters
Stype vector to assign values to
Parameters
vVector to assign to diagonal

Definition at line 454 of file matrix.h.

◆ set_row()

template<const int n, const int m, typename T , typename Mtype >
template<typename S , std::enable_if_t< hila::is_assignable< T &, S >::value, int > = 0>
void Matrix_t< n, m, T, Mtype >::set_row ( int  r,
const RowVector< m, S > &  v 
)
inline

Set row of Matrix with RowVector if types are assignable.

V.random();
M.set_row(i,V);
Template Parameters
SRowVector type
Parameters
rIndex of row to be set
vRowVector to be set

Definition at line 374 of file matrix.h.

◆ size()

template<const int n, const int m, typename T , typename Mtype >
template<int q = n, int p = m, std::enable_if_t< q==1, int > = 0>
static constexpr int Matrix_t< n, m, T, Mtype >::size ( )
inlinestaticconstexpr

Returns size of Vector or square Matrix.

Template Parameters
qrow size n
pcolumn size m
Returns
constexpr int

Definition at line 242 of file matrix.h.

◆ sort() [1/2]

template<const int n, const int m, typename T , typename Mtype >
Mtype Matrix_t< n, m, T, Mtype >::sort ( hila::sort  order = hila::sort::ascending) const
inline

Sort method for Vector.

Order can be past as argument as either ascending (default) or descending

Ascending

V.random();
V.sort(); // V is sorted in ascending order
Mtype sort(Vector< N, int > &permutation, hila::sort order=hila::sort::ascending) const
Sort method for Vector which returns permutation order.
Definition matrix.h:1456

Descending

V.sort(hila::sort::descending);
Parameters
orderOrder to sort in
Returns
Mtype

Definition at line 1504 of file matrix.h.

◆ sort() [2/2]

template<const int n, const int m, typename T , typename Mtype >
template<int N>
Mtype Matrix_t< n, m, T, Mtype >::sort ( Vector< N, int > &  permutation,
hila::sort  order = hila::sort::ascending 
) const
inline

Sort method for Vector which returns permutation order.

Order can be past as argument as either ascending (default) or descending

Ascending

V.random();
V.sort(perm);
V.permute(perm);
Mtype permute(const Vector< N, int > &permutation) const
Permute elements of vector.
Definition matrix.h:1415

Descending

V.random();
V.sort(perm,hila::sort::descending);
V.permute(perm);
Template Parameters
N
Parameters
permutation
order
Returns
Mtype

Definition at line 1456 of file matrix.h.

◆ squarenorm()

template<const int n, const int m, typename T , typename Mtype >
hila::arithmetic_type< T > Matrix_t< n, m, T, Mtype >::squarenorm ( ) const
inline

Calculate square norm - sum of squared elements.

Returns
hila::arithmetic_type<T>

Definition at line 1167 of file matrix.h.

◆ str()

template<const int n, const int m, typename T , typename Mtype >
std::string Matrix_t< n, m, T, Mtype >::str ( int  prec = 8,
char  separator = ' ' 
) const
inline

Convert to string for printing

Definition at line 1646 of file matrix.h.

◆ svd() [1/2]

template<int n, int m, typename T , typename Mtype >
svd_result< Mtype > Matrix_t< n, m, T, Mtype >::svd ( enum hila::sort  sorted = hila::sort::unsorted) const

svd and svd_pivot - alternative interface

svd(hila::sort [optional]) returns struct svd_result<Mtype>, with fields U: nxn unitary matrix singularvalues: DiagonalMatrix of singular values (real, > 0) V: nxn unitary matrix

auto res = M.svd(); now res.U : nxn unitary res.singularvalues : DiagonalMatrix of singular values res.V : nxn unitary

result satifies M = res.U res.singularvalues res.V.dagger()

Definition at line 577 of file matrix_linalg.h.

◆ svd() [2/2]

template<int n, int m, typename T , typename Mtype >
template<typename Et , typename Mt , typename MT >
int Matrix_t< n, m, T, Mtype >::svd ( Matrix_t< n, n, Mt, MT > &  _U,
DiagonalMatrix< n, Et > &  _S,
Matrix_t< n, n, Mt, MT > &  _V,
enum hila::sort  sorted = hila::sort::unsorted 
) const

Singular value decomposition: divide matrix A = U S V*, where U,V unitary and S diagonal matrix of real singular values. Unpivoted Jacobi rotations.

Unpivoted rotation is generally faster than pivoted (esp. on gpu), but a bit less accurate

Use: M.svd(U, S, V, [sorted]);

Result satisfies M = U S V.dagger()

The algorithm works by one-sided Jacobi rotations.

  • U = V = 1
  • Loop
    • for i=0..(n-2); j=(i+1)..(n-1)
      • calculate B_ii, B_ij, B_jj, (B_ij = (A^* A)_ij)
      • If B_ij > 0
        • diagonalize J^* [ B_ii B_ij ] J = diag(D_ii, D_jj) [ B_ji B_jj ]
        • A <- A J , Columns i and j change
        • V <- V J
    • endfor
    • if nothing changed, break Loop
  • Endloop Now A = U S, A^* A = S* S = S^2
  • S_ii = A_ii / |A_i| (norm of column i)
  • U = A / S

Definition at line 469 of file matrix_linalg.h.

◆ svd_pivot()

template<int n, int m, typename T , typename Mtype >
template<typename Et , typename Mt , typename MT >
int Matrix_t< n, m, T, Mtype >::svd_pivot ( Matrix_t< n, n, Mt, MT > &  _U,
DiagonalMatrix< n, Et > &  _S,
Matrix_t< n, n, Mt, MT > &  _V,
enum hila::sort  sorted = hila::sort::unsorted 
) const

Singular value decomposition: divide matrix A = U S V*, where U,V unitary and S diagonal matrix of real singular values. Fully pivoted Jacobi rotations.

Use: M.svd_pivot(U, S, V, [sorted]);

The algorithm works by one-sided Jacobi rotations.

  • U = V = 1
  • Compute Gram matrix B = A^* A. B is Hermitean, with B_ii >= 0.
  • Loop:
    • Find largest B_ij, j > i - full pivoting if |B_ij|^2 <= tol * B_ii B_jj we're done
    • find J to diagonalize J^* [ B_ii B_ij ] J = diag(D_ii, D_jj) [ B_ji B_jj ]
    • A <- A J , Columns i and j change
    • V <- V J
    • Recompute B_ik and B_jk, k /= i,j (B_ii = D_ii, B_jj = D_jj, B_ij = 0) (n^2)
  • Endloop Now A = U S, A^* A = S* S = S^2
  • S_ii = A_ii / |A_i| (norm of column i)
  • U = A / S
Parameters
U

Definition at line 344 of file matrix_linalg.h.

◆ trace()

template<const int n, const int m, typename T , typename Mtype >
T Matrix_t< n, m, T, Mtype >::trace ( ) const
inline

Computes Trace for Matrix.

Not define for non square matrices. Static assert will stop code from compiling if executed for non square matrices.

Returns
T

Definition at line 1129 of file matrix.h.

◆ transpose() [1/3]

template<const int n, const int m, typename T , typename Mtype >
template<int mm = m, typename Rtype = typename std::conditional<n == m, Mtype, Matrix<m, n, T>>::type, std::enable_if_t<(mm !=1), int > = 0>
Rtype Matrix_t< n, m, T, Mtype >::transpose ( ) const
inline

Transpose of matrix.

Return type for square matrix is same input, for non square return type is Matrix<n,m,MyType>

Template Parameters
mm
std::conditional<n,Mtype,Matrix<m,n,T>>::type
Returns
const Rtype

Definition at line 997 of file matrix.h.

◆ transpose() [2/3]

template<const int n, const int m, typename T , typename Mtype >
template<int mm = m, std::enable_if_t< mm==1, int > = 0>
const RowVector< n, T > & Matrix_t< n, m, T, Mtype >::transpose ( ) const
inline

Transpose of vector.

Returns reference

Template Parameters
mm
Returns
const RowVector<n, T>&

Definition at line 1014 of file matrix.h.

◆ transpose() [3/3]

template<const int n, const int m, typename T , typename Mtype >
template<int nn = n, std::enable_if_t< nn==1 &&m !=1, int > = 0>
const Vector< n, T > & Matrix_t< n, m, T, Mtype >::transpose ( ) const
inline

Transpose of RowVector.

Returns reference

Template Parameters
mm
Returns
const RowVector<n, T>&

Definition at line 1026 of file matrix.h.


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