TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
nda::lapack::gelss_worker< T >

#include <nda/lapack/gelss_worker.hpp>

Detailed Description

template<typename T>
class nda::lapack::gelss_worker< T >

Worker class for solving linear least squares problems.

Solving a linear least squares problem means finding the minimum norm solution \( \mathbf{x} \) of a linear system of equations, i.e.

\[ \min_x | \mathbf{b} - \mathbf{A x} |_2 \; , \]

where \( \mathbf{A} \) is a given matrix and \( \mathbf{b} \) is a given vector (although it can also be a matrix, in this case one searches for a solution matrix \( \mathbf{X} \)).

Let \( \mathbf{A} \in \mathbb{C}^{M \times N} \) with rank \( \rho \leq \min(M, N) \). Its singular value decomposition (SVD) is given by

\[ \mathbf{A} = \mathbf{U} \mathbf{\Sigma} \mathbf{V}^H = \begin{bmatrix} \mathbf{U}_R & \mathbf{U}_N \end{bmatrix} \begin{bmatrix} \mathbf{S} & 0 \\ 0 & 0 \end{bmatrix} \begin{bmatrix} \mathbf{V}_R^H \\ \mathbf{V}_N^H \end{bmatrix} \; , \]

where \( \mathbf{U}_R \in \mathbb{C}^{M \times \rho} \), \( \mathbf{U}_N \in \mathbb{C}^{M \times (M - \rho)} \), \( \mathbf{S} \in \mathbb{R}^{\rho \times \rho} \), \( \mathbf{V}_R \in \mathbb{C}^{N \times \rho} \), and \( \mathbf{V}_N \in \mathbb{C}^{N \times (N - \rho)} \).

The least squares solution can then be written as

\[ \mathbf{x} = \mathbf{A}^{+} \mathbf{b} = \mathbf{V} \mathbf{\Sigma}^{+} \mathbf{U}^H \mathbf{b} = \mathbf{V}_R \mathbf{S}^{-1} \mathbf{U}_R^H \mathbf{b} \; , \]

where \( \mathbf{M}^{+} \) denotes the Moore-Penrose pseudo-inverse of the matrix \( \mathbf{M} \), and the residual error as

\[ \epsilon = \left\| \mathbf{A} \mathbf{x} - \mathbf{b} \right\|_2 = \left\| \mathbf{U}_N^H \mathbf{b} \right\|_2 \; . \]

See this question on math.stackexchange for more information.

Template Parameters
TValue type of the given problem.

Definition at line 88 of file gelss_worker.hpp.

Public Member Functions

 gelss_worker (matrix_const_view< T > A)
 Construct a new worker object for a given matrix \( \mathbf{A} \) .
 
int n_var () const
 Get the number of variables of the given problem, i.e. the size of the vector \( \mathbf{x} \).
 
auto operator() (matrix_const_view< T > B, std::optional< long >={}) const
 Solve the least squares problem for a given right hand side matrix \( \mathbf{B} \).
 
auto operator() (vector_const_view< T > b, std::optional< long >={}) const
 Solve the least squares problem for a given right hand side vector \( \mathbf{b} \).
 
array< double, 1 > const & S_vec () const
 Get the singular values, i.e. the diagonal elements of the matrix \( \mathbf{S} \).
 

Constructor & Destructor Documentation

◆ gelss_worker()

template<typename T>
nda::lapack::gelss_worker< T >::gelss_worker ( matrix_const_view< T > A)
inline

Construct a new worker object for a given matrix \( \mathbf{A} \) .

It performs the SVD decomposition of the given matrix \( \mathbf{A} \) and stores its pseudo inverse \( \mathbf{A}^{+} \), its singular values \( \mathbf{s} = \mathrm{diag}(\mathbf{S}) \) and the matrix \(\mathbf{U}_N^H \). The latter is used to calculate the error of the least squares problem.

Parameters
AMatrix \( \mathbf{A} \) used in the least squares problem.

Definition at line 123 of file gelss_worker.hpp.

Member Function Documentation

◆ n_var()

template<typename T>
int nda::lapack::gelss_worker< T >::n_var ( ) const
inline

Get the number of variables of the given problem, i.e. the size of the vector \( \mathbf{x} \).

Returns
Number of columns of the matrix \( \mathbf{A} \) .

Definition at line 106 of file gelss_worker.hpp.

◆ operator()() [1/2]

template<typename T>
auto nda::lapack::gelss_worker< T >::operator() ( matrix_const_view< T > B,
std::optional< long > = {} ) const
inline

Solve the least squares problem for a given right hand side matrix \( \mathbf{B} \).

It calculates and returns the least squares solution \( \mathbf{X} = \mathbf{A}^{+} \mathbf{B} \) and the error

\[ \epsilon = \max\left\{ \frac{ \left\| \mathbf{U}_N^H \mathbf{b} \right\|_2 }{ \sqrt{N} } : \mathbf{b} \text{ is a column vector in } \mathbf{B} \right\} \; . \]

Parameters
BRight hand side matrix.
Returns
A std::pair<matrix<T>, double> containing the solution matrix \( \mathbf{X} \) and the error \(\epsilon \).

Definition at line 158 of file gelss_worker.hpp.

◆ operator()() [2/2]

template<typename T>
auto nda::lapack::gelss_worker< T >::operator() ( vector_const_view< T > b,
std::optional< long > = {} ) const
inline

Solve the least squares problem for a given right hand side vector \( \mathbf{b} \).

It calculates and returns the least squares solution \( \mathbf{x} = \mathbf{A}^{+} \mathbf{b} \) and the error \( \epsilon = \frac{ \left\| \mathbf{U}_N^H \mathbf{b} \right\|_2 }{ \sqrt{N} } \).

Parameters
bRight hand side vector.
Returns
A std::pair<vector<T>, double> containing the solution vector \( \mathbf{x} \) and the error \(\epsilon \).

Definition at line 179 of file gelss_worker.hpp.

◆ S_vec()

template<typename T>
array< double, 1 > const & nda::lapack::gelss_worker< T >::S_vec ( ) const
inlinenodiscard

Get the singular values, i.e. the diagonal elements of the matrix \( \mathbf{S} \).

Returns
1-dimensional nda::array containing the singular values.

Definition at line 112 of file gelss_worker.hpp.


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