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