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