Linear Algebra  arduino
Accessible implementations of linear algebra algorithms
NoPivotLU.ipp
Go to the documentation of this file.
1 #ifndef ARDUINO
2 #include <linalg/NoPivotLU.hpp>
3 #else
5 #endif
6 
7 #include <cassert>
8 
10  LU = std::move(matrix);
12 }
13 
14 void NoPivotLU::compute(const SquareMatrix &matrix) {
15  LU = matrix;
17 }
18 
20  assert(has_LU());
22  for (size_t c = 0; c < LU.cols(); ++c) {
23  // Elements above the diagonal are zero
24  for (size_t r = 0; r < c; ++r)
25  LU(r, c) = 0;
26  // Diagonal elements are one
27  LU(c, c) = 1;
28  // Elements below the diagonal are stored in LU already
29  }
30  return std::move(LU);
31 }
32 
34  assert(has_LU());
35  assert(L.rows() == LU.rows());
36  assert(L.cols() == LU.cols());
37  for (size_t c = 0; c < L.cols(); ++c) {
38  // Elements above the diagonal are zero
39  for (size_t r = 0; r < c; ++r)
40  L(r, c) = 0;
41  // Diagonal elements are one
42  L(c, c) = 1;
43  // Elements below the diagonal are stored in LU
44  for (size_t r = c + 1; r < L.rows(); ++r)
45  L(r, c) = LU(r, c);
46  }
47 }
48 
50  SquareMatrix L(LU.rows());
51  get_L_inplace(L);
52  return L;
53 }
54 
56  assert(has_LU());
58  for (size_t c = 0; c < LU.cols(); ++c) {
59  // Elements above and on the diagonal are stored in LU already
60  // Elements below the diagonal are zero
61  for (size_t r = c + 1; r < LU.rows(); ++r)
62  LU(r, c) = 0;
63  }
64  return std::move(LU);
65 }
66 
68  assert(has_LU());
69  assert(U.rows() == LU.rows());
70  assert(U.cols() == LU.cols());
71  for (size_t c = 0; c < U.cols(); ++c) {
72  // Elements above and on the diagonal are stored in LU
73  for (size_t r = 0; r <= c; ++r)
74  U(r, c) = LU(r, c);
75  // Elements below the diagonal are zero
76  for (size_t r = c + 1; r < U.rows(); ++r)
77  U(r, c) = 0;
78  }
79 }
80 
82  SquareMatrix U(LU.rows());
83  get_U_inplace(U);
84  return U;
85 }
86 
89  return std::move(LU);
90 }
91 
92 Matrix NoPivotLU::solve(const Matrix &B) const {
93  Matrix B_cpy = B;
94  solve_inplace(B_cpy);
95  return B_cpy;
96 }
97 
99  solve_inplace(B);
100  return std::move(B);
101 }
102 
103 Vector NoPivotLU::solve(const Vector &b) const {
104  return Vector(solve(static_cast<const Matrix &>(b)));
105 }
106 
108  solve_inplace(b);
109  return std::move(b);
110 }
111 
112 #ifndef NO_IOSTREAM_SUPPORT
113 
114 #include <iomanip>
115 #include <iostream>
116 
117 // LCOV_EXCL_START
118 
119 std::ostream &operator<<(std::ostream &os, const NoPivotLU &lu) {
120  if (!lu.is_factored()) {
121  os << "Not factored." << std::endl;
122  return os;
123  }
124 
125  // Output field width (characters)
126  int w = os.precision() + 9;
127  auto &LU = lu.get_LU();
128 
129  os << "L = " << std::endl;
130  for (size_t r = 0; r < LU.cols(); ++r) {
131  for (size_t c = 0; c < r; ++c)
132  os << std::setw(w) << 0;
133  for (size_t c = r; c < LU.cols(); ++c)
134  os << std::setw(w) << LU(r, c);
135  os << std::endl;
136  }
137 
138  os << "U = " << std::endl;
139  for (size_t r = 0; r < LU.cols(); ++r) {
140  for (size_t c = 0; c < r; ++c)
141  os << std::setw(w) << LU(r, c);
142  os << std::setw(w) << 1;
143  for (size_t c = r; c < LU.cols(); ++c)
144  os << std::setw(w) << 0;
145  os << std::endl;
146  }
147  return os;
148 }
149 
150 // LCOV_EXCL_STOP
151 
152 #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
LU factorization without pivoting.
Definition: NoPivotLU.hpp:20
SquareMatrix LU
Result of a LU factorization: stores the upper-triangular matrix U and the strict lower-triangular pa...
Definition: NoPivotLU.hpp:137
void get_U_inplace(Matrix &U) const
Copy the upper-triangular matrix U to the given matrix.
Definition: NoPivotLU.ipp:67
SquareMatrix && steal_LU()
Get the internal storage of the upper-triangular matrix U and the strict lower-triangular part of mat...
Definition: NoPivotLU.ipp:87
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: NoPivotLU.hpp:117
bool has_LU() const
Check if this object contains valid L and U factors.
Definition: NoPivotLU.hpp:107
Matrix solve(const Matrix &B) const
Solve the system AX = B or LUX = B.
Definition: NoPivotLU.ipp:92
void get_L_inplace(Matrix &L) const
Copy the lower-triangular matrix L to the given matrix.
Definition: NoPivotLU.ipp:33
void compute(SquareMatrix &&matrix)
Perform the LU factorization of the given matrix.
Definition: NoPivotLU.ipp:9
SquareMatrix && steal_L()
Get the lower-triangular matrix L, reusing the internal storage.
Definition: NoPivotLU.ipp:19
void solve_inplace(Matrix &B) const
Solve the system AX = B or LUX = B.
Definition: NoPivotLU.cpp:238
bool is_factored() const
Check if this object contains a factorization.
Definition: NoPivotLU.hpp:104
void compute_factorization()
The actual LU factorization algorithm.
Definition: NoPivotLU.cpp:21
enum NoPivotLU::State state
SquareMatrix get_L() const &
Get a copy of the lower-triangular matrix L.
Definition: NoPivotLU.ipp:49
SquareMatrix get_U() const &
Get a copy of the upper-triangular matrix U.
Definition: NoPivotLU.ipp:81
SquareMatrix && steal_U()
Get the upper-triangular matrix U, reusing the internal storage.
Definition: NoPivotLU.ipp:55
Square matrix class.
Definition: Matrix.hpp:572
A column vector (nĂ—1 matrix).
Definition: Matrix.hpp:278