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 <decomposition.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.
 

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<typename 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 2554 of file decomposition.hpp.

Constructor & Destructor Documentation

◆ qr_decomposition() [1/2]

template<typename 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 2563 of file decomposition.hpp.

◆ qr_decomposition() [2/2]

template<typename 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 2575 of file decomposition.hpp.

Member Function Documentation

◆ q() [1/2]

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

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

Definition at line 2594 of file decomposition.hpp.

◆ q() [2/2]

template<typename 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 2588 of file decomposition.hpp.

◆ r() [1/2]

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

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

Definition at line 2606 of file decomposition.hpp.

◆ r() [2/2]

template<typename 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 2600 of file decomposition.hpp.

◆ solve()

template<typename 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 2620 of file decomposition.hpp.

Member Data Documentation

◆ cols_

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

The number of columns (n).

Definition at line 2658 of file decomposition.hpp.

◆ depth_

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

min(m, n).

Definition at line 2659 of file decomposition.hpp.

◆ q_

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

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

Definition at line 2661 of file decomposition.hpp.

◆ r_

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

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

Definition at line 2662 of file decomposition.hpp.

◆ rows_

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

The number of rows (m).

Definition at line 2657 of file decomposition.hpp.

◆ width_

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

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

Definition at line 2660 of file decomposition.hpp.


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