Linear Algebra
master
Accessible implementations of linear algebra algorithms
|
#include <linalg/HouseholderQR.hpp>
QR factorization using Householder reflectors.
Factorizes an m×n matrix with m >= n into an m×m unitary factor Q and an m×n upper triangular factor R.
It can be used for solving square systems of equations or for finding a least squares solution to an overdetermined system of equations.
This version does not use column pivoting, and is not rank-revealing.
Definition at line 18 of file HouseholderQR.hpp.
Constructors | |
HouseholderQR ()=default | |
Default constructor. More... | |
HouseholderQR (const Matrix &matrix) | |
Factorize the given matrix. More... | |
HouseholderQR (Matrix &&matrix) | |
Factorize the given matrix. More... | |
Factorization | |
void | compute (Matrix &&matrix) |
Perform the QR factorization of the given matrix. More... | |
void | compute (const Matrix &matrix) |
Perform the QR factorization of the given matrix. More... | |
Retrieving the Q factor | |
void | apply_QT_inplace (Matrix &B) const |
Compute the product QᵀB, overwriting B with the result. More... | |
Matrix | apply_QT (const Matrix &B) const |
Compute the product QᵀB. More... | |
Matrix && | apply_QT (Matrix &&B) const |
Compute the product QᵀB. More... | |
void | apply_Q_inplace (Matrix &X) const |
Compute the product QB, overwriting B with the result. More... | |
Matrix | apply_Q (const Matrix &X) const |
Compute the product QB. More... | |
Matrix && | apply_Q (Matrix &&B) const |
Compute the product QB. More... | |
void | get_Q_inplace (SquareMatrix &Q) const |
Compute the unitary matrix Q and copy it to the given matrix. More... | |
SquareMatrix | get_Q () const |
Compute the unitary matrix Q. More... | |
Retrieving the R factor | |
Matrix && | steal_R () |
Get the upper-triangular matrix R, reusing the internal storage. More... | |
void | get_R_inplace (Matrix &R) const |
Copy the upper-triangular matrix R to the given matrix. More... | |
Matrix | get_R () const & |
Get a copy of the upper-triangular matrix R. More... | |
Matrix && | get_R () && |
Get the upper-triangular matrix R. More... | |
Solving systems of equations and least-squares problems | |
void | solve_inplace (Matrix &B) const |
Solve the system AX = B or QRX = B. More... | |
Matrix | solve (const Matrix &B) const |
Solve the system AX = B or QRX = B. More... | |
Matrix && | solve (Matrix &&B) const |
Solve the system AX = B or QRX = B. More... | |
Vector | solve (const Vector &B) const |
Solve the system Ax = b or QRx = b. More... | |
Vector && | solve (Vector &&B) const |
Solve the system Ax = b or QRx = b. More... | |
Access to internal representation | |
bool | is_factored () const |
Check if this object contains a valid factorization. More... | |
const Matrix & | get_RW () const & |
Get the internal storage of the strict upper-triangular part of R and the Householder reflector vectors W. More... | |
Matrix && | get_RW () && |
Get the internal storage of the strict upper-triangular part of R and the Householder reflector vectors W. More... | |
const Vector & | get_R_diag () const & |
Get the internal storage of the diagonal elements of R. More... | |
Vector && | get_R_diag () && |
Get the internal storage of the diagonal elements of R. More... | |
Private Types | |
enum | State { NotFactored = 0 , Factored = 1 } |
Private Member Functions | |
void | compute_factorization () |
The actual QR factorization algorithm. More... | |
void | back_subs (const Matrix &B, Matrix &X) const |
Back substitution algorithm for solving upper-triangular systems RX = B. More... | |
Private Attributes | |
Matrix | RW |
Result of a Householder QR factorization: stores the strict upper-triangular part of matrix R and the full matrix of scaled Householder reflection vectors W. More... | |
Vector | R_diag |
Contains the diagonal elements of R. More... | |
enum HouseholderQR::State | state = NotFactored |
Related Functions | |
(Note that these are not member functions.) | |
std::ostream & | operator<< (std::ostream &os, const HouseholderQR &qr) |
Print the Q and R matrices of a HouseholderQR object. More... | |
|
private |
Enumerator | |
---|---|
NotFactored | |
Factored |
Definition at line 139 of file HouseholderQR.hpp.
|
default |
Default constructor.
|
inline |
Factorize the given matrix.
Definition at line 26 of file HouseholderQR.hpp.
|
inline |
Factorize the given matrix.
Definition at line 28 of file HouseholderQR.hpp.
void compute | ( | Matrix && | matrix | ) |
Perform the QR factorization of the given matrix.
Definition at line 7 of file HouseholderQR.ipp.
void compute | ( | const Matrix & | matrix | ) |
Perform the QR factorization of the given matrix.
Definition at line 13 of file HouseholderQR.ipp.
void apply_QT_inplace | ( | Matrix & | B | ) | const |
Compute the product QᵀB, overwriting B with the result.
Definition at line 185 of file HouseholderQR.cpp.
Compute the product QᵀB.
Definition at line 19 of file HouseholderQR.ipp.
Compute the product QᵀB.
Definition at line 25 of file HouseholderQR.ipp.
void apply_Q_inplace | ( | Matrix & | X | ) | const |
Compute the product QB, overwriting B with the result.
Definition at line 211 of file HouseholderQR.cpp.
Compute the product QB.
Definition at line 36 of file HouseholderQR.ipp.
void get_Q_inplace | ( | SquareMatrix & | Q | ) | const |
Compute the unitary matrix Q and copy it to the given matrix.
Definition at line 78 of file HouseholderQR.ipp.
SquareMatrix get_Q | ( | ) | const |
Compute the unitary matrix Q.
Definition at line 85 of file HouseholderQR.ipp.
Matrix && steal_R | ( | ) |
Get the upper-triangular matrix R, reusing the internal storage.
Definition at line 64 of file HouseholderQR.ipp.
void get_R_inplace | ( | Matrix & | R | ) | const |
Copy the upper-triangular matrix R to the given matrix.
Definition at line 41 of file HouseholderQR.ipp.
Matrix get_R | ( | ) | const & |
Get a copy of the upper-triangular matrix R.
Definition at line 58 of file HouseholderQR.ipp.
|
inline |
Get the upper-triangular matrix R.
Definition at line 82 of file HouseholderQR.hpp.
void solve_inplace | ( | Matrix & | B | ) | const |
Solve the system AX = B or QRX = B.
Matrix B is overwritten with the result X. If the matrix A is square, no new allocations occur, and the storage of B is reused for X. If A is not square, new storage will be allocated for X.
Definition at line 270 of file HouseholderQR.cpp.
Solve the system AX = B or QRX = B.
Definition at line 91 of file HouseholderQR.ipp.
Solve the system AX = B or QRX = B.
Definition at line 98 of file HouseholderQR.ipp.
Solve the system Ax = b or QRx = b.
Definition at line 103 of file HouseholderQR.ipp.
Solve the system Ax = b or QRx = b.
Definition at line 107 of file HouseholderQR.ipp.
|
inline |
Check if this object contains a valid factorization.
Definition at line 111 of file HouseholderQR.hpp.
|
inline |
Get the internal storage of the strict upper-triangular part of R and the Householder reflector vectors W.
Definition at line 115 of file HouseholderQR.hpp.
|
inline |
Get the internal storage of the strict upper-triangular part of R and the Householder reflector vectors W.
Definition at line 117 of file HouseholderQR.hpp.
|
inline |
Get the internal storage of the diagonal elements of R.
Definition at line 119 of file HouseholderQR.hpp.
|
inline |
Get the internal storage of the diagonal elements of R.
Definition at line 121 of file HouseholderQR.hpp.
|
private |
The actual QR factorization algorithm.
RW
contains the matrix A to be factorized RW.rows() >= RW.cols()
R_diag.size() == RW.cols()
RW
contains the strict upper-triangular part of factor R. R_diag
contains the diagonal of R. RW
contains the Householder reflectors wₖ, with ‖wₖ‖ = √2. apply_Q(get_R()) == A == get_Q() * get_R()
(up to rounding errors)Definition at line 22 of file HouseholderQR.cpp.
Back substitution algorithm for solving upper-triangular systems RX = B.
Definition at line 237 of file HouseholderQR.cpp.
|
related |
Print the Q and R matrices of a HouseholderQR object.
Definition at line 114 of file HouseholderQR.ipp.
|
private |
Result of a Householder QR factorization: stores the strict upper-triangular part of matrix R and the full matrix of scaled Householder reflection vectors W.
The reflection vectors have norm √2.
Definition at line 135 of file HouseholderQR.hpp.
|
private |
Contains the diagonal elements of R.
Definition at line 137 of file HouseholderQR.hpp.
|
private |