TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
Linear algebra tools

Detailed Description

Generic functions that wrap the BLAS and LAPACK interfaces to provide a more user-friendly approach for the most common linear algebra related tasks.

Functions

template<typename V >
auto nda::linalg::cross_product (V const &x, V const &y)
 Compute the cross product of two 3-dimensional vectors.
 
template<typename... A>
requires (nda::clef::is_any_lazy<A...>)
auto nda::clef::determinant (A &&...__a)
 Lazy version of nda::determinant.
 
template<typename M >
auto nda::determinant (M const &m)
 Compute the determinant of a square matrix/view.
 
template<typename M >
requires (is_matrix_or_view_v<M>)
auto nda::determinant_in_place (M &m)
 Compute the determinant of a square matrix/view.
 
template<typename X , typename Y >
auto nda::dot (X &&x, Y &&y)
 Compute the dot product of two real arrays/views.
 
template<typename X , typename Y >
auto nda::dotc (X &&x, Y &&y)
 Compute the dot product of two complex arrays/views.
 
template<typename M >
auto nda::linalg::eigenelements (M const &m)
 Find the eigenvalues and eigenvectors of a symmetric (real) or hermitian (complex) matrix/view.
 
template<typename M >
auto nda::linalg::eigenvalues (M const &m)
 Find the eigenvalues of a symmetric (real) or hermitian (complex) matrix/view.
 
template<typename M >
auto nda::linalg::eigenvalues_in_place (M &m)
 Find the eigenvalues of a symmetric (real) or hermitian (complex) matrix/view.
 
template<Matrix M>
requires (get_algebra<M> == 'M')
auto nda::inverse (M const &m)
 Compute the inverse of an n-by-n matrix.
 
template<MemoryMatrix M>
requires (get_algebra<M> == 'M' and mem::on_host<M>)
void nda::inverse1_in_place (M &&m)
 Compute the inverse of a 1-by-1 matrix.
 
template<MemoryMatrix M>
requires (get_algebra<M> == 'M' and mem::on_host<M>)
void nda::inverse2_in_place (M &&m)
 Compute the inverse of a 2-by-2 matrix.
 
template<MemoryMatrix M>
requires (get_algebra<M> == 'M' and mem::on_host<M>)
void nda::inverse3_in_place (M &&m)
 Compute the inverse of a 3-by-3 matrix.
 
template<MemoryMatrix M>
requires (get_algebra<M> == 'M')
void nda::inverse_in_place (M &&m)
 Compute the inverse of an n-by-n matrix.
 
template<typename A >
bool nda::is_matrix_diagonal (A const &a, bool print_error=false)
 Check if a given array/view is diagonal, i.e. if it is square (see nda::is_matrix_square) and all the the off-diagonal elements are zero.
 
template<typename A >
bool nda::is_matrix_square (A const &a, bool print_error=false)
 Check if a given array/view is square, i.e. if the first dimension has the same extent as the second dimension.
 
template<Matrix A, Matrix B>
auto nda::matmul (A &&a, B &&b)
 Perform a matrix-matrix multiplication.
 
template<Matrix A, Vector X>
auto nda::matvecmul (A &&a, X &&x)
 Perform a matrix-vector multiplication.
 
template<ArrayOfRank< 1 > A>
double nda::norm (A const &a, double p=2.0)
 Calculate the p-norm of an nda::ArrayOfRank<1> object \( \mathbf{x} \) with scalar values. The p-norm is defined as.
 

Function Documentation

◆ cross_product()

template<typename V >
auto nda::linalg::cross_product ( V const & x,
V const & y )

#include <nda/linalg/cross_product.hpp>

Compute the cross product of two 3-dimensional vectors.

Template Parameters
VVector type.
Parameters
xLeft hand side vector.
yRight hand side vector.
Returns
nda::array of rank 1 containing the cross product of the two vectors.

Definition at line 40 of file cross_product.hpp.

◆ determinant()

template<typename M >
auto nda::determinant ( M const & m)

#include <nda/linalg/det_and_inverse.hpp>

Compute the determinant of a square matrix/view.

The given matrix/view is not modified. It first makes a copy of the given matrix/view and then calls nda::determinant_in_place with the copy.

Template Parameters
MType of the matrix/view.
Parameters
mMatrix/view object.
Returns
Determinant of the matrix/view.

Definition at line 143 of file det_and_inverse.hpp.

◆ determinant_in_place()

template<typename M >
requires (is_matrix_or_view_v<M>)
auto nda::determinant_in_place ( M & m)

#include <nda/linalg/det_and_inverse.hpp>

Compute the determinant of a square matrix/view.

It uses nda::lapack::getrf to compute the LU decomposition of the matrix and then calculates the determinant by multiplying the diagonal elements of the \( \mathbf{U} \) matrix and taking into account that getrf may change the ordering of the rows/columns of the matrix.

The given matrix/view is modified in place.

Template Parameters
MType of the matrix/view.
Parameters
mMatrix/view object.
Returns
Determinant of the matrix/view.

Definition at line 100 of file det_and_inverse.hpp.

◆ dot()

template<typename X , typename Y >
auto nda::dot ( X && x,
Y && y )

#include <nda/linalg/dot.hpp>

Compute the dot product of two real arrays/views.

It is generic in the sense that it allows the input arrays to belong to a different nda::mem::AddressSpace (as long as they are compatible).

If possible, it uses nda::blas::dot, otherwise it calls nda::blas::dot_generic.

Template Parameters
XType of the left hand side array/view.
YType of the right hand side array/view.
Parameters
xLeft hand side array/view.
yRight hand side array/view.
Returns
The dot product of the two arrays/views.

Definition at line 54 of file dot.hpp.

◆ dotc()

template<typename X , typename Y >
auto nda::dotc ( X && x,
Y && y )

#include <nda/linalg/dot.hpp>

Compute the dot product of two complex arrays/views.

It is generic in the sense that it allows the input arrays to belong to a different nda::mem::AddressSpace (as long as they are compatible).

If possible, it uses nda::blas::dotc, otherwise it calls nda::blas::dotc_generic.

Template Parameters
XType of the left hand side array/view.
YType of the right hand side array/view.
Parameters
xLeft hand side array/view.
yRight hand side array/view.
Returns
The dot product of the two arrays/views.

Definition at line 97 of file dot.hpp.

◆ eigenelements()

template<typename M >
auto nda::linalg::eigenelements ( M const & m)

#include <nda/linalg/eigenelements.hpp>

Find the eigenvalues and eigenvectors of a symmetric (real) or hermitian (complex) matrix/view.

For a real symmetric matrix/view, it calls the LAPACK routine syev. For a complex hermitian matrix/view, it calls the LAPACK routine heev.

The given matrix/view is copied and the original is not modified.

Template Parameters
MType of the matrix/view.
Parameters
mMatrix/View to diagonalize.
Returns
std::pair consisting of the array of eigenvalues in ascending order and the matrix containing the eigenvectors in its columns.

Definition at line 97 of file eigenelements.hpp.

◆ eigenvalues()

template<typename M >
auto nda::linalg::eigenvalues ( M const & m)

#include <nda/linalg/eigenelements.hpp>

Find the eigenvalues of a symmetric (real) or hermitian (complex) matrix/view.

For a real symmetric matrix/view, it calls the LAPACK routine syev. For a complex hermitian matrix/view, it calls the LAPACK routine heev.

The given matrix/view is copied and the original is not modified.

Template Parameters
MType of the matrix/view.
Parameters
mMatrix/View to diagonalize.
Returns
An nda::array of rank 1 containing the eigenvalues in ascending order.

Definition at line 116 of file eigenelements.hpp.

◆ eigenvalues_in_place()

template<typename M >
auto nda::linalg::eigenvalues_in_place ( M & m)

#include <nda/linalg/eigenelements.hpp>

Find the eigenvalues of a symmetric (real) or hermitian (complex) matrix/view.

For a real symmetric matrix/view, it calls the LAPACK routine syev. For a complex hermitian matrix/view, it calls the LAPACK routine heev.

The given matrix/view will be modified by the diagonalization process.

Template Parameters
MType of the matrix/view.
Parameters
mMatrix/View to diagonalize.
Returns
An nda::array of rank 1 containing the eigenvalues in ascending order.

Definition at line 134 of file eigenelements.hpp.

◆ inverse()

template<Matrix M>
requires (get_algebra<M> == 'M')
auto nda::inverse ( M const & m)

#include <nda/linalg/det_and_inverse.hpp>

Compute the inverse of an n-by-n matrix.

The given matrix/view is not modified. It first makes copy of the given matrix/view and then calls nda::inverse_in_place with the copy.

Template Parameters
Mnda::MemoryMatrix type.
Parameters
mnda::MemoryMatrix object to be inverted.
Returns
Inverse of the matrix.

Definition at line 297 of file det_and_inverse.hpp.

◆ inverse1_in_place()

template<MemoryMatrix M>
requires (get_algebra<M> == 'M' and mem::on_host<M>)
void nda::inverse1_in_place ( M && m)

#include <nda/linalg/det_and_inverse.hpp>

Compute the inverse of a 1-by-1 matrix.

The inversion is performed in place.

Template Parameters
Mnda::MemoryMatrix type.
Parameters
mnda::MemoryMatrix object to be inverted.

Definition at line 169 of file det_and_inverse.hpp.

◆ inverse2_in_place()

template<MemoryMatrix M>
requires (get_algebra<M> == 'M' and mem::on_host<M>)
void nda::inverse2_in_place ( M && m)

#include <nda/linalg/det_and_inverse.hpp>

Compute the inverse of a 2-by-2 matrix.

The inversion is performed in place.

Template Parameters
Mnda::MemoryMatrix type.
Parameters
mnda::MemoryMatrix object to be inverted.

Definition at line 184 of file det_and_inverse.hpp.

◆ inverse3_in_place()

template<MemoryMatrix M>
requires (get_algebra<M> == 'M' and mem::on_host<M>)
void nda::inverse3_in_place ( M && m)

#include <nda/linalg/det_and_inverse.hpp>

Compute the inverse of a 3-by-3 matrix.

The inversion is performed in place.

Template Parameters
Mnda::MemoryMatrix type.
Parameters
mnda::MemoryMatrix object to be inverted.

Definition at line 210 of file det_and_inverse.hpp.

◆ inverse_in_place()

template<MemoryMatrix M>
requires (get_algebra<M> == 'M')
void nda::inverse_in_place ( M && m)

#include <nda/linalg/det_and_inverse.hpp>

Compute the inverse of an n-by-n matrix.

The inversion is performed in place.

For small matrices (1-by-1, 2-by-2, 3-by-3), we directly compute the matrix inversion using the optimized routines: nda::inverse1_in_place, nda::inverse2_in_place, nda::inverse3_in_place.

For larger matrices, it uses nda::lapack::getrf and nda::lapack::getri.

Template Parameters
Mnda::MemoryMatrix type.
Parameters
mnda::MemoryMatrix object to be inverted.

Definition at line 254 of file det_and_inverse.hpp.

◆ is_matrix_diagonal()

template<typename A >
bool nda::is_matrix_diagonal ( A const & a,
bool print_error = false )

#include <nda/linalg/det_and_inverse.hpp>

Check if a given array/view is diagonal, i.e. if it is square (see nda::is_matrix_square) and all the the off-diagonal elements are zero.

Note
It does not check if the array/view has rank 2.
Template Parameters
AArray/View type.
Parameters
aArray/View object.
print_errorIf true, print an error message if the matrix is not diagonal.
Returns
True if the array/view is diagonal, false otherwise.

Definition at line 80 of file det_and_inverse.hpp.

◆ is_matrix_square()

template<typename A >
bool nda::is_matrix_square ( A const & a,
bool print_error = false )

#include <nda/linalg/det_and_inverse.hpp>

Check if a given array/view is square, i.e. if the first dimension has the same extent as the second dimension.

Note
It does not check if the array/view has rank 2.
Template Parameters
AArray/View type.
Parameters
aArray/View object.
print_errorIf true, print an error message if the matrix is not square.
Returns
True if the array/view is square, false otherwise.

Definition at line 61 of file det_and_inverse.hpp.

◆ matmul()

template<Matrix A, Matrix B>
auto nda::matmul ( A && a,
B && b )

#include <nda/linalg/matmul.hpp>

Perform a matrix-matrix multiplication.

It is generic in the sense that it allows the input matrices to belong to a different nda::mem::AddressSpace (as long as they are compatible).

If possible, it uses nda::blas::gemm, otherwise it calls nda::blas::gemm_generic.

Template Parameters
Anda::Matrix type of lhs operand.
Bnda::Matrix type of rhs operand.
Parameters
aLeft hand side matrix operand.
bRight hand side matrix operand.
Returns
Result of the matrix-matrix multiplication.

Definition at line 87 of file matmul.hpp.

◆ matvecmul()

template<Matrix A, Vector X>
auto nda::matvecmul ( A && a,
X && x )

#include <nda/linalg/matmul.hpp>

Perform a matrix-vector multiplication.

It is generic in the sense that it allows the input matrix and vector to belong to a different nda::mem::AddressSpace (as long as they are compatible).

If possible, it uses nda::blas::gemv, otherwise it calls nda::blas::gemv_generic.

Template Parameters
Anda::Matrix type of lhs operand.
Xnda::Vector type of rhs operand.
Parameters
aLeft hand side matrix operand.
xRight hand side vector operand.
Returns
Result of the matrix-vector multiplication.

Definition at line 152 of file matmul.hpp.

◆ norm()

template<ArrayOfRank< 1 > A>
double nda::norm ( A const & a,
double p = 2.0 )

#include <nda/linalg/norm.hpp>

Calculate the p-norm of an nda::ArrayOfRank<1> object \( \mathbf{x} \) with scalar values. The p-norm is defined as.

\[ || \mathbf{x} ||_p = \left( \sum_{i=0}^{N-1} |x_i|^p \right)^{1/p} \]

with the special cases (following numpy.linalg.norm convention)

  • \( || \mathbf{x} ||_0 = \text{number of non-zero elements} \),
  • \( || \mathbf{x} ||_{\infty} = \max_i |x_i| \),
  • \( || \mathbf{x} ||_{-\infty} = \min_i |x_i| \).
Template Parameters
Anda::ArrayOfRank<1> type.
Parameters
anda::ArrayOfRank<1> object.
pOrder of the norm.
Returns
Norm of the array/view as a double.

Definition at line 58 of file norm.hpp.