39 auto sq = [](
double x) {
return x * x; };
41 for (
size_t k = 0; k <
RW.
cols(); ++k) {
47 for (
size_t i = k; i <
RW.
rows(); ++i)
48 sq_norm_x += sq(
RW(i, k));
49 double norm_x = std::sqrt(sq_norm_x);
55 double &x_0 =
RW(k, k);
105 if (norm_x >= std::numeric_limits<double>::min() * 2) {
106 double x_p = -std::copysign(norm_x, x_0);
107 double v_0 = x_0 - x_p;
108 double norm_v_sq2 = std::sqrt(std::abs(x_0) * norm_x + sq_norm_x);
116 for (
size_t i = k; i <
RW.
rows(); ++i)
117 RW(i, k) /= norm_v_sq2;
170 for (
size_t c = k + 1; c <
RW.
cols(); ++c) {
172 double dot_product = 0;
173 for (
size_t r = k; r <
RW.
rows(); ++r)
174 dot_product +=
RW(r, k) *
RW(r, c);
176 for (
size_t r = k; r <
RW.
rows(); ++r)
177 RW(r, c) -=
RW(r, k) * dot_product;
193 for (
size_t i = 0; i < B.
cols(); ++i) {
197 for (
size_t k = 0; k <
RW.
cols(); ++k) {
199 double dot_product = 0;
200 for (
size_t r = k; r <
RW.
rows(); ++r)
201 dot_product +=
RW(r, k) * B(r, i);
203 for (
size_t r = k; r <
RW.
rows(); ++r)
204 B(r, i) -=
RW(r, k) * dot_product;
219 for (
size_t i = 0; i < X.
cols(); ++i) {
223 for (
size_t k =
RW.
cols(); k-- > 0;) {
225 double dot_product = 0;
226 for (
size_t r = k; r <
RW.
rows(); ++r)
227 dot_product +=
RW(r, k) * X(r, i);
229 for (
size_t r = k; r <
RW.
rows(); ++r)
230 X(r, i) -=
RW(r, k) * dot_product;
257 for (
size_t i = 0; i < B.
cols(); ++i) {
258 for (
size_t k =
RW.
cols(); k-- > 0;) {
260 for (
size_t j = k + 1; j <
RW.
cols(); ++j) {
261 X(k, i) -=
RW(k, j) * X(j, i);
Vector R_diag
Contains the diagonal elements of R.
void apply_QT_inplace(Matrix &B) const
Compute the product QᵀB, overwriting B with the result.
enum HouseholderQR::State state
void back_subs(const Matrix &B, Matrix &X) const
Back substitution algorithm for solving upper-triangular systems RX = B.
Matrix RW
Result of a Householder QR factorization: stores the strict upper-triangular part of matrix R and the...
void solve_inplace(Matrix &B) const
Solve the system AX = B or QRX = B.
bool is_factored() const
Check if this object contains a valid factorization.
void compute_factorization()
The actual QR factorization algorithm.
void apply_Q_inplace(Matrix &X) const
Compute the product QB, overwriting B with the result.
size_t rows() const
Get the number of rows of the matrix.
size_t cols() const
Get the number of columns of the matrix.
size_t size() const
Get the number of elements in the vector.