Linear Algebra  arduino
Accessible implementations of linear algebra algorithms
RowPivotLU.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Matrix.hpp"
4 #include "PermutationMatrix.hpp"
5 
16 class RowPivotLU {
17  public:
20 
22  RowPivotLU() = default;
24  RowPivotLU(const SquareMatrix &matrix) { compute(matrix); }
26  RowPivotLU(SquareMatrix &&matrix) { compute(std::move(matrix)); }
27 
29 
30  public:
33 
35  void compute(SquareMatrix &&matrix);
37  void compute(const SquareMatrix &matrix);
38 
40 
41  public:
44 
51 
53  void get_L_inplace(Matrix &L) const;
55  SquareMatrix get_L() const &;
57  SquareMatrix &&get_L() && { return steal_L(); }
58 
60 
61  public:
64 
71 
73  void get_U_inplace(Matrix &U) const;
75  SquareMatrix get_U() const &;
77  SquareMatrix &&get_U() && { return steal_U(); }
78 
80 
81  public:
84 
90 
92  PermutationMatrix get_P() const & { return P; }
94  PermutationMatrix &&get_P() && { return steal_P(); }
95 
97 
98  public:
101 
104  void solve_inplace(Matrix &B) const;
106  Matrix solve(const Matrix &B) const;
108  Matrix &&solve(Matrix &&B) const;
110  Vector solve(const Vector &B) const;
112  Vector &&solve(Vector &&B) const;
113 
115 
116  public:
119 
121  bool is_factored() const { return state == Factored; }
122 
124  bool has_LU() const { return valid_LU; }
125 
127  bool has_P() const { return valid_P; }
128 
136 
139  const SquareMatrix &get_LU() const & { return LU; }
142  SquareMatrix &&get_LU() && { return steal_LU(); }
143 
145 
146  private:
148  void compute_factorization();
150  void back_subs(const Matrix &B, Matrix &X) const;
153  void forward_subs(const Matrix &B, Matrix &X) const;
154 
155  private:
162 
163  enum State {
165  Factored = 1,
167 
168  bool valid_LU = false;
169  bool valid_P = false;
170 };
171 
174 std::ostream &operator<<(std::ostream &os, const RowPivotLU &lu);
std::ostream & operator<<(std::ostream &os, const HouseholderQR &qr)
Print the Q and R matrices of a HouseholderQR object.
General matrix class.
Definition: Matrix.hpp:34
Class that represents matrices that permute the rows or columns of other matrices.
@ RowPermutation
Can be used for permuting rows only.
LU factorization with row pivoting.
Definition: RowPivotLU.hpp:16
SquareMatrix LU
Result of a LU factorization: stores the upper-triangular matrix U and the strict lower-triangular pa...
Definition: RowPivotLU.hpp:159
void get_U_inplace(Matrix &U) const
Copy the upper-triangular matrix U to the given matrix.
Definition: RowPivotLU.ipp:73
SquareMatrix && steal_LU()
Get the internal storage of the upper-triangular matrix U and the strict lower-triangular part of mat...
Definition: RowPivotLU.ipp:99
const SquareMatrix & get_LU() const &
Get a copy of the internal storage of the upper-triangular matrix U and the strict lower-triangular p...
Definition: RowPivotLU.hpp:139
bool has_LU() const
Check if this object contains valid L and U factors.
Definition: RowPivotLU.hpp:124
RowPivotLU()=default
Default constructor.
PermutationMatrix && get_P() &&
Get the permutation matrix P.
Definition: RowPivotLU.hpp:94
Matrix solve(const Matrix &B) const
Solve the system AX = B or LUX = B.
Definition: RowPivotLU.ipp:105
void get_L_inplace(Matrix &L) const
Copy the lower-triangular matrix L to the given matrix.
Definition: RowPivotLU.ipp:38
void compute(SquareMatrix &&matrix)
Perform the LU factorization of the given matrix.
Definition: RowPivotLU.ipp:9
SquareMatrix && steal_L()
Get the lower-triangular matrix L, reusing the internal storage.
Definition: RowPivotLU.ipp:23
void back_subs(const Matrix &B, Matrix &X) const
Back substitution algorithm for solving upper-triangular systems UX = B.
Definition: RowPivotLU.cpp:153
void solve_inplace(Matrix &B) const
Solve the system AX = B or LUX = B.
Definition: RowPivotLU.cpp:218
bool is_factored() const
Check if this object contains a factorization.
Definition: RowPivotLU.hpp:121
void forward_subs(const Matrix &B, Matrix &X) const
Forward substitution algorithm for solving lower-triangular systems LX = B.
Definition: RowPivotLU.cpp:185
void compute_factorization()
The actual LU factorization algorithm.
Definition: RowPivotLU.cpp:29
SquareMatrix get_L() const &
Get a copy of the lower-triangular matrix L.
Definition: RowPivotLU.ipp:54
PermutationMatrix get_P() const &
Get a copy of the permutation matrix P.
Definition: RowPivotLU.hpp:92
SquareMatrix get_U() const &
Get a copy of the upper-triangular matrix U.
Definition: RowPivotLU.ipp:87
SquareMatrix && get_LU() &&
Get the internal storage of the upper-triangular matrix U and the strict lower-triangular part of mat...
Definition: RowPivotLU.hpp:142
PermutationMatrix && steal_P()
Get the permutation matrix P, reusing the internal storage.
Definition: RowPivotLU.ipp:93
RowPivotLU(SquareMatrix &&matrix)
Factorize the given matrix.
Definition: RowPivotLU.hpp:26
SquareMatrix && steal_U()
Get the upper-triangular matrix U, reusing the internal storage.
Definition: RowPivotLU.ipp:60
PermutationMatrix P
The permutation of A that maximizes pivot size.
Definition: RowPivotLU.hpp:161
RowPivotLU(const SquareMatrix &matrix)
Factorize the given matrix.
Definition: RowPivotLU.hpp:24
enum RowPivotLU::State state
bool has_P() const
Check if this object contains a valid permutation matrix P.
Definition: RowPivotLU.hpp:127
Square matrix class.
Definition: Matrix.hpp:572
A column vector (nĂ—1 matrix).
Definition: Matrix.hpp:278