TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
Solving equations and inverting matrices

Detailed Description

Functions to solve linear systems of equations and to invert matrices.

Functions

template<Matrix M>
requires (get_algebra<M> == 'M' and is_blas_lapack_v<get_value_t<M>>)
auto nda::linalg::inv (M const &m)
 Compute the inverse \( \mathbf{M}^{-1} \) of an \( n \times n \) matrix \( \mathbf{M} \).
template<blas_lapack::BlasArray< 2 > M>
requires (get_algebra<M> == 'M' and mem::have_host_compatible_addr_space<M>)
void nda::linalg::inv_in_place (M &&m)
 Compute the inverse \( \mathbf{M}^{-1} \) of an \( n \times n \) matrix \( \mathbf{M} \) in place.
template<Matrix A, Array B>
requires (have_same_value_type_v<A, B> and mem::have_compatible_addr_space<A, B> and is_blas_lapack_v<get_value_t<A>>)
auto nda::linalg::solve (A const &a, B const &b)
 Solve a system of linear equations.
template<blas_lapack::BlasArray< 2 > A, blas_lapack::BlasArrayFor< A > B>
requires ((get_rank<B> == 1 || get_rank<B> == 2) and blas_lapack::has_F_layout<B>)
void nda::linalg::solve_in_place (A &&a, B &&b)
 Solve a system of linear equations in place.

Function Documentation

◆ inv()

template<Matrix M>
requires (get_algebra<M> == 'M' and is_blas_lapack_v<get_value_t<M>>)
auto nda::linalg::inv ( M const & m)

#include <nda/linalg/inv.hpp>

Compute the inverse \( \mathbf{M}^{-1} \) of an \( n \times n \) matrix \( \mathbf{M} \).

The given matrix/view is not modified. Depending on the address space of the input matrix, the function does the following:

This function makes copies of the input arrays/views. When working on the device memory space, this may lead to runtime errors if the copying fails.

Note
\( \mathbf{M} \) is required to have algebra M and to have a value type that satisfies nda::is_blas_lapack_v.
Template Parameters
Mnda::Matrix type.
Parameters
mInput matrix \( \mathbf{M} \).
Returns
The inverse matrix \( \mathbf{M}^{-1} \).

Definition at line 150 of file inv.hpp.

◆ inv_in_place()

template<blas_lapack::BlasArray< 2 > M>
requires (get_algebra<M> == 'M' and mem::have_host_compatible_addr_space<M>)
void nda::linalg::inv_in_place ( M && m)

#include <nda/linalg/inv.hpp>

Compute the inverse \( \mathbf{M}^{-1} \) of an \( n \times n \) matrix \( \mathbf{M} \) in place.

For small matrices ( \( n < 4 \)), it uses optimized direct inversion formulas.

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

An exception is thrown, if the matrix is not invertible, i.e. if \( \det(\mathbf{M}) = 0 \), or if a LAPACK call fails.

Note
\( \mathbf{M} \) is required to satisfy nda::mem::have_host_compatible_addr_space and to have algebra 'M'.
Template Parameters
Mnda::blas_lapack::BlasArray<2> type.
Parameters
mInput/output matrix. On entry, the matrix \( \mathbf{M} \). On exit, the matrix \( \mathbf{M}^{-1} \).

Definition at line 101 of file inv.hpp.

◆ solve()

template<Matrix A, Array B>
requires (have_same_value_type_v<A, B> and mem::have_compatible_addr_space<A, B> and is_blas_lapack_v<get_value_t<A>>)
auto nda::linalg::solve ( A const & a,
B const & b )

#include <nda/linalg/solve.hpp>

Solve a system of linear equations.

It makes a copy of the input matrix \( \mathbf{A} \) and the right hand side matrix \( \mathbf{B} \) or vector \( \mathbf{b} \) and calls nda::linalg::solve_in_place.

The solution matrix \( \mathbf{X} \) is always in nda::F_layout.

This function makes copies of the input arrays/views. When working on the device memory space, this may lead to runtime errors if the copying fails.

Note
\( \mathbf{A} \) and \( \mathbf{B} \)/ \( \mathbf{b} \) are required to satisfy nda::mem::have_compatible_addr_space and to have the same value type that satisfies nda::is_blas_lapack_v.
Template Parameters
Anda::Matrix type.
Bnda::Array type of rank 1 or 2.
Parameters
aInput matrix. The \( n \times n \) matrix \( \mathbf{A} \) determining the linear system.
bInput array. Right hand side matrix \( \mathbf{B} \) or vector \( \mathbf{b} \).
Returns
Solution matrix \( \mathbf{X} \) or vector \( \mathbf{x} \).

Definition at line 101 of file solve.hpp.

◆ solve_in_place()

template<blas_lapack::BlasArray< 2 > A, blas_lapack::BlasArrayFor< A > B>
requires ((get_rank<B> == 1 || get_rank<B> == 2) and blas_lapack::has_F_layout<B>)
void nda::linalg::solve_in_place ( A && a,
B && b )

#include <nda/linalg/solve.hpp>

Solve a system of linear equations in place.

The function solves a system of linear equations

  • \( \mathbf{A X} = \mathbf{B} \) or
  • \( \mathbf{A x} = \mathbf{b} \),

with a general \( n \times n \) matrix \( \mathbf{A} \) and either \( n \times n_{\mathrm{rhs}} \) matrices \( \mathbf{X} \) and \( \mathbf{B} \) or vectors \( \mathbf{x} \) and \( \mathbf{b} \) of size \( n \).

It uses nda::lapack::getrf to compute the LU factorization of the matrix \( \mathbf{A} \) and then nda::lapack::getrs to solve the system of linear equations.

An exception is thrown, if a LAPACK/cuSOLVER call fails.

Note
\( \mathbf{B} \) is required to have nda::F_layout.
Template Parameters
Anda::blas_lapack::BlasArray<2> type.
Bnda::blas_lapack::BlasArrayFor type of rank 1 or 2.
Parameters
aInput/Output matrix. On entry, the \( n \times n \) matrix \( \mathbf{A} \) determining the linear system. On exit, its LU factorization as calculated by nda::lapack::getrf.
bInput/Output array. On entry, the right hand side matrix \( \mathbf{B} \) or vector \( \mathbf{b} \). On exit, the solution matrix \( \mathbf{X} \) or vector \( \mathbf{x} \).

Definition at line 62 of file solve.hpp.