NumericAL
A header-only C++20 lexer built on REAL
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
numerical::qr_decomposition< T > Class Template Reference

A QR factorisation A = Q R of an m × n matrix, as NumPy's qr, reduced by default. More...

#include <factorizations.hpp>

Public Member Functions

 qr_decomposition (const matrix< T > &a, qr_mode mode=qr_mode::reduced)
 Factorises a.
 
 qr_decomposition (matrix< T > &&a, qr_mode mode=qr_mode::reduced)
 Factorises a in its own storage, an expiring matrix's elements taken over: no copy of them.
 
matrix< T > q () const &
 The factor Q (orthonormal columns): m × k, k = min(m, n), or m × m complete.
 
matrix< T > q () &&
 Q, moved out of an expiring decomposition (r stays).
 
matrix< T > r () const &
 The upper-trapezoidal factor R: k × n, k = min(m, n), or m × n complete.
 
matrix< T > r () &&
 R, moved out of an expiring decomposition (q stays).
 
vector< T > solve (const vector< T > &b) const
 The least-squares solution of A x ≈ b (x = R⁻¹ Qᴴ b) for a full-rank A.
 
matrix< T > solve (const matrix< T > &b) const
 The least-squares solutions of A X ≈ B (X = R⁻¹ Qᴴ B) for a full-rank A, every column of b at once: Qᴴ B through the matrix product kernel, then the back substitution by rows of R over all the columns (each column solve's to rounding: the product sums its terms in another order).
 

Private Attributes

std::size_t rows_
 The number of rows (m).
 
std::size_t cols_
 The number of columns (n).
 
std::size_t depth_
 min(m, n).
 
std::size_t width_
 Q's columns and R's rows: k, or m complete.
 
buffer< T > q_
 The factor Q, row-major, m × width_.
 
buffer< T > r_
 The upper-trapezoidal factor R, row-major, width_ × n.
 

Detailed Description

template<field T>
class numerical::qr_decomposition< T >

A QR factorisation A = Q R of an m × n matrix, as NumPy's qr, reduced by default.

Template Parameters
TThe scalar element type (a field: double, std::complex<double>, …).

With k = min(m, n), Q is m × k with orthonormal columns (Qᴴ Q = I) and R is k × n upper trapezoidal (triangular when m ≥ n) with a real, non-negative diagonal; complete (qr_mode::complete), Q is m × m and R m × n, its rows past k zero. Computed by Householder reflections, so Q stays orthonormal to rounding however ill-conditioned A is, and a rank-deficient A factorises too (with zeros on R's diagonal). solve returns the least-squares solution of A x ≈ b for a full-column-rank A (m ≥ n); for a rank-deficient or wide one, numerical::lstsq gives the minimum-norm solution.

Definition at line 3798 of file factorizations.hpp.

Constructor & Destructor Documentation

◆ qr_decomposition() [1/2]

template<field T>
numerical::qr_decomposition< T >::qr_decomposition ( const matrix< T > &  a,
qr_mode  mode = qr_mode::reduced 
)
inlineexplicit

Factorises a.

Parameters
[in]aThe matrix, any shape.
[in]modeqr_mode::reduced (Q m × k) or qr_mode::complete (Q m × m).
Exceptions
non_finite_errorIf an entry is infinite or NaN.

Definition at line 3807 of file factorizations.hpp.

◆ qr_decomposition() [2/2]

template<field T>
numerical::qr_decomposition< T >::qr_decomposition ( matrix< T > &&  a,
qr_mode  mode = qr_mode::reduced 
)
inlineexplicit

Factorises a in its own storage, an expiring matrix's elements taken over: no copy of them.

Parameters
[in]aThe matrix, any shape.
[in]modeqr_mode::reduced (Q m × k) or qr_mode::complete (Q m × m).
Exceptions
non_finite_errorIf an entry is infinite or NaN.

Definition at line 3819 of file factorizations.hpp.

Member Function Documentation

◆ q() [1/2]

template<field T>
matrix< T > numerical::qr_decomposition< T >::q ( ) &&
inline

Q, moved out of an expiring decomposition (r stays).

Definition at line 3838 of file factorizations.hpp.

◆ q() [2/2]

template<field T>
matrix< T > numerical::qr_decomposition< T >::q ( ) const &
inline

The factor Q (orthonormal columns): m × k, k = min(m, n), or m × m complete.

Definition at line 3832 of file factorizations.hpp.

◆ r() [1/2]

template<field T>
matrix< T > numerical::qr_decomposition< T >::r ( ) &&
inline

R, moved out of an expiring decomposition (q stays).

Definition at line 3850 of file factorizations.hpp.

◆ r() [2/2]

template<field T>
matrix< T > numerical::qr_decomposition< T >::r ( ) const &
inline

The upper-trapezoidal factor R: k × n, k = min(m, n), or m × n complete.

Definition at line 3844 of file factorizations.hpp.

◆ solve() [1/2]

template<field T>
matrix< T > numerical::qr_decomposition< T >::solve ( const matrix< T > &  b) const
inline

The least-squares solutions of A X ≈ B (X = R⁻¹ Qᴴ B) for a full-rank A, every column of b at once: Qᴴ B through the matrix product kernel, then the back substitution by rows of R over all the columns (each column solve's to rounding: the product sums its terms in another order).

Parameters
[in]bThe right-hand sides (m × k).
Returns
X (n × k).
Exceptions
dimension_errorIf b's row count differs from the matrix's, or A is wide (as solve).
singular_matrix_errorIf A is rank-deficient (as solve).

Definition at line 3908 of file factorizations.hpp.

◆ solve() [2/2]

template<field T>
vector< T > numerical::qr_decomposition< T >::solve ( const vector< T > &  b) const
inline

The least-squares solution of A x ≈ b (x = R⁻¹ Qᴴ b) for a full-rank A.

Parameters
[in]bThe right-hand side (size m).
Returns
The solution x (size n).
Exceptions
dimension_errorIf b's size differs from the row count, or A is wide (m < n: its system is underdetermined, and numerical::lstsq gives its minimum-norm solution).
singular_matrix_errorIf A is rank-deficient: a diagonal entry of R is under detail::rank_tolerance (numerical::lstsq solves such a system).

Definition at line 3864 of file factorizations.hpp.

Member Data Documentation

◆ cols_

template<field T>
std::size_t numerical::qr_decomposition< T >::cols_
private

The number of columns (n).

Definition at line 3954 of file factorizations.hpp.

◆ depth_

template<field T>
std::size_t numerical::qr_decomposition< T >::depth_
private

min(m, n).

Definition at line 3955 of file factorizations.hpp.

◆ q_

template<field T>
buffer<T> numerical::qr_decomposition< T >::q_
private

The factor Q, row-major, m × width_.

Definition at line 3957 of file factorizations.hpp.

◆ r_

template<field T>
buffer<T> numerical::qr_decomposition< T >::r_
private

The upper-trapezoidal factor R, row-major, width_ × n.

Definition at line 3958 of file factorizations.hpp.

◆ rows_

template<field T>
std::size_t numerical::qr_decomposition< T >::rows_
private

The number of rows (m).

Definition at line 3953 of file factorizations.hpp.

◆ width_

template<field T>
std::size_t numerical::qr_decomposition< T >::width_
private

Q's columns and R's rows: k, or m complete.

Definition at line 3956 of file factorizations.hpp.


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