Linear Algebra  arduino
Accessible implementations of linear algebra algorithms
RowPivotLU.ipp
Go to the documentation of this file.
1 #ifndef ARDUINO
2 #include <linalg/RowPivotLU.hpp>
3 #else
5 #endif
6 
7 #include <cassert>
8 
10  LU = std::move(matrix);
11  P.resize(LU.rows());
12  P.fill_identity();
14 }
15 
16 void RowPivotLU::compute(const SquareMatrix &matrix) {
17  LU = matrix;
18  P.resize(LU.rows());
19  P.fill_identity();
21 }
22 
24  assert(has_LU());
26  valid_LU = false;
27  for (size_t c = 0; c < LU.cols(); ++c) {
28  // Elements above the diagonal are zero
29  for (size_t r = 0; r < c; ++r)
30  LU(r, c) = 0;
31  // Diagonal elements are one
32  LU(c, c) = 1;
33  // Elements below the diagonal are stored in LU already
34  }
35  return std::move(LU);
36 }
37 
39  assert(has_LU());
40  assert(L.rows() == LU.rows());
41  assert(L.cols() == LU.cols());
42  for (size_t c = 0; c < L.cols(); ++c) {
43  // Elements above the diagonal are zero
44  for (size_t r = 0; r < c; ++r)
45  L(r, c) = 0;
46  // Diagonal elements are one
47  L(c, c) = 1;
48  // Elements below the diagonal are stored in LU
49  for (size_t r = c + 1; r < L.rows(); ++r)
50  L(r, c) = LU(r, c);
51  }
52 }
53 
55  SquareMatrix L(LU.rows());
56  get_L_inplace(L);
57  return L;
58 }
59 
61  assert(has_LU());
63  valid_LU = false;
64  for (size_t c = 0; c < LU.cols(); ++c) {
65  // Elements above and on the diagonal are stored in LU already
66  // Elements below the diagonal are zero
67  for (size_t r = c + 1; r < LU.rows(); ++r)
68  LU(r, c) = 0;
69  }
70  return std::move(LU);
71 }
72 
74  assert(has_LU());
75  assert(U.rows() == LU.rows());
76  assert(U.cols() == LU.cols());
77  for (size_t c = 0; c < U.cols(); ++c) {
78  // Elements above and on the diagonal are stored in LU
79  for (size_t r = 0; r <= c; ++r)
80  U(r, c) = LU(r, c);
81  // Elements below the diagonal are zero
82  for (size_t r = c + 1; r < U.rows(); ++r)
83  U(r, c) = 0;
84  }
85 }
86 
88  SquareMatrix U(LU.rows());
89  get_U_inplace(U);
90  return U;
91 }
92 
95  valid_P = false;
96  return std::move(P);
97 }
98 
100  state = NotFactored;
101  valid_LU = false;
102  return std::move(LU);
103 }
104 
105 Matrix RowPivotLU::solve(const Matrix &B) const {
106  Matrix B_cpy = B;
107  solve_inplace(B_cpy);
108  return B_cpy;
109 }
110 
112  solve_inplace(B);
113  return std::move(B);
114 }
115 
116 Vector RowPivotLU::solve(const Vector &b) const {
117  return Vector(solve(static_cast<const Matrix &>(b)));
118 }
119 
121  solve_inplace(b);
122  return std::move(b);
123 }
124 
125 #ifndef NO_IOSTREAM_SUPPORT
126 
127 #include <iomanip>
128 #include <iostream>
129 
130 // LCOV_EXCL_START
131 
132 std::ostream &operator<<(std::ostream &os, const RowPivotLU &lu) {
133  if (!lu.is_factored()) {
134  os << "Not factored." << std::endl;
135  return os;
136  }
137 
138  // Output field width (characters)
139  int w = os.precision() + 9;
140  auto &LU = lu.get_LU();
141 
142  os << "L = " << std::endl;
143  for (size_t r = 0; r < LU.cols(); ++r) {
144  for (size_t c = 0; c < r; ++c)
145  os << std::setw(w) << 0;
146  for (size_t c = r; c < LU.cols(); ++c)
147  os << std::setw(w) << LU(r, c);
148  os << std::endl;
149  }
150 
151  os << "U = " << std::endl;
152  for (size_t r = 0; r < LU.cols(); ++r) {
153  for (size_t c = 0; c < r; ++c)
154  os << std::setw(w) << LU(r, c);
155  os << std::setw(w) << 1;
156  for (size_t c = r; c < LU.cols(); ++c)
157  os << std::setw(w) << 0;
158  os << std::endl;
159  }
160  return os;
161 }
162 
163 // LCOV_EXCL_STOP
164 
165 #endif
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
size_t rows() const
Get the number of rows of the matrix.
Definition: Matrix.hpp:81
size_t cols() const
Get the number of columns of the matrix.
Definition: Matrix.hpp:83
Class that represents matrices that permute the rows or columns of other matrices.
void resize(size_t size)
Resize the permutation matrix.
void fill_identity()
Fill the matrix as an identity permutation.
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
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 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 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
SquareMatrix get_U() const &
Get a copy of the upper-triangular matrix U.
Definition: RowPivotLU.ipp:87
PermutationMatrix && steal_P()
Get the permutation matrix P, reusing the internal storage.
Definition: RowPivotLU.ipp:93
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
enum RowPivotLU::State state
Square matrix class.
Definition: Matrix.hpp:572
A column vector (nĂ—1 matrix).
Definition: Matrix.hpp:278