35 auto sq = [](
double x) {
return x * x; };
37 for (
size_t k = 0; k <
RW.
cols(); ++k) {
43 for (
size_t i = k; i <
RW.
rows(); ++i)
44 sq_norm_x += sq(
RW(i, k));
45 double norm_x = std::sqrt(sq_norm_x);
51 double &x_0 =
RW(k, k);
101 if (norm_x >= std::numeric_limits<double>::min() * 2) {
102 double x_p = -std::copysign(norm_x, x_0);
103 double v_0 = x_0 - x_p;
104 double norm_v_sq2 = std::sqrt(std::abs(x_0) * norm_x + sq_norm_x);
112 for (
size_t i = k; i <
RW.
rows(); ++i)
113 RW(i, k) /= norm_v_sq2;
166 for (
size_t c = k + 1; c <
RW.
cols(); ++c) {
168 double dot_product = 0;
169 for (
size_t r = k; r <
RW.
rows(); ++r)
170 dot_product +=
RW(r, k) *
RW(r, c);
172 for (
size_t r = k; r <
RW.
rows(); ++r)
173 RW(r, c) -=
RW(r, k) * dot_product;
189 for (
size_t i = 0; i < B.
cols(); ++i) {
193 for (
size_t k = 0; k <
RW.
cols(); ++k) {
195 double dot_product = 0;
196 for (
size_t r = k; r <
RW.
rows(); ++r)
197 dot_product +=
RW(r, k) * B(r, i);
199 for (
size_t r = k; r <
RW.
rows(); ++r)
200 B(r, i) -=
RW(r, k) * dot_product;
215 for (
size_t i = 0; i < X.
cols(); ++i) {
219 for (
size_t k =
RW.
cols(); k-- > 0;) {
221 double dot_product = 0;
222 for (
size_t r = k; r <
RW.
rows(); ++r)
223 dot_product +=
RW(r, k) * X(r, i);
225 for (
size_t r = k; r <
RW.
rows(); ++r)
226 X(r, i) -=
RW(r, k) * dot_product;
253 for (
size_t i = 0; i < B.
cols(); ++i) {
254 for (
size_t k =
RW.
cols(); k-- > 0;) {
256 for (
size_t j = k + 1; j <
RW.
cols(); ++j) {
257 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.