Linear Algebra  arduino
Accessible implementations of linear algebra algorithms
HouseholderQR.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Matrix.hpp"
4 
19  public:
22 
24  HouseholderQR() = default;
26  HouseholderQR(const Matrix &matrix) { compute(matrix); }
28  HouseholderQR(Matrix &&matrix) { compute(std::move(matrix)); }
29 
31 
32  public:
35 
37  void compute(Matrix &&matrix);
39  void compute(const Matrix &matrix);
40 
42 
43  public:
46 
48  void apply_QT_inplace(Matrix &B) const;
50  Matrix apply_QT(const Matrix &B) const;
52  Matrix &&apply_QT(Matrix &&B) const;
53 
55  void apply_Q_inplace(Matrix &X) const;
57  Matrix apply_Q(const Matrix &X) const;
59  Matrix &&apply_Q(Matrix &&B) const;
60 
62  void get_Q_inplace(SquareMatrix &Q) const;
64  SquareMatrix get_Q() const;
65 
67 
68  public:
71 
75  Matrix &&steal_R();
76 
78  void get_R_inplace(Matrix &R) const;
80  Matrix get_R() const &;
82  Matrix &&get_R() && { return steal_R(); }
83 
85 
86  public:
89 
94  void solve_inplace(Matrix &B) const;
96  Matrix solve(const Matrix &B) const;
98  Matrix &&solve(Matrix &&B) const;
100  Vector solve(const Vector &B) const;
102  Vector &&solve(Vector &&B) const;
103 
105 
106  public:
109 
111  bool is_factored() const { return state == Factored; }
112 
115  const Matrix &get_RW() const & { return RW; }
117  Matrix &&get_RW() && { return std::move(RW); }
119  const Vector &get_R_diag() const & { return R_diag; }
121  Vector &&get_R_diag() && { return std::move(R_diag); }
122 
124 
125  private:
127  void compute_factorization();
129  void back_subs(const Matrix &B, Matrix &X) const;
130 
131  private:
138 
139  enum State {
141  Factored = 1,
143 };
144 
147 std::ostream &operator<<(std::ostream &os, const HouseholderQR &qr);
QR factorization using Householder reflectors.
Vector R_diag
Contains the diagonal elements of R.
std::ostream & operator<<(std::ostream &os, const HouseholderQR &qr)
Print the Q and R matrices of a HouseholderQR object.
HouseholderQR()=default
Default constructor.
void apply_QT_inplace(Matrix &B) const
Compute the product QᵀB, overwriting B with the result.
void get_Q_inplace(SquareMatrix &Q) const
Compute the unitary matrix Q and copy it to the given matrix.
Matrix solve(const Matrix &B) const
Solve the system AX = B or QRX = B.
Matrix apply_Q(const Matrix &X) const
Compute the product QB.
Matrix get_R() const &
Get a copy of the upper-triangular matrix R.
const Vector & get_R_diag() const &
Get the internal storage of the diagonal elements of R.
Vector && get_R_diag() &&
Get the internal storage of the diagonal elements of R.
Matrix && steal_R()
Get the upper-triangular matrix R, reusing the internal storage.
enum HouseholderQR::State state
void back_subs(const Matrix &B, Matrix &X) const
Back substitution algorithm for solving upper-triangular systems RX = B.
Matrix RW
Result of a Householder QR factorization: stores the strict upper-triangular part of matrix R and the...
Matrix apply_QT(const Matrix &B) const
Compute the product QᵀB.
void solve_inplace(Matrix &B) const
Solve the system AX = B or QRX = B.
bool is_factored() const
Check if this object contains a valid factorization.
void compute_factorization()
The actual QR factorization algorithm.
SquareMatrix get_Q() const
Compute the unitary matrix Q.
HouseholderQR(Matrix &&matrix)
Factorize the given matrix.
const Matrix & get_RW() const &
Get the internal storage of the strict upper-triangular part of R and the Householder reflector vecto...
HouseholderQR(const Matrix &matrix)
Factorize the given matrix.
void compute(Matrix &&matrix)
Perform the QR factorization of the given matrix.
void get_R_inplace(Matrix &R) const
Copy the upper-triangular matrix R to the given matrix.
Matrix && get_RW() &&
Get the internal storage of the strict upper-triangular part of R and the Householder reflector vecto...
void apply_Q_inplace(Matrix &X) const
Compute the product QB, overwriting B with the result.
General matrix class.
Definition: Matrix.hpp:34
Square matrix class.
Definition: Matrix.hpp:572
A column vector (n×1 matrix).
Definition: Matrix.hpp:278