numerics
Loading...
Searching...
No Matches
qr.hpp
Go to the documentation of this file.
1/// @file qr.hpp
2/// @brief QR factorization via Householder reflections
3#pragma once
4
5#include "core/matrix.hpp"
6#include "core/policy.hpp"
7
8namespace num {
9
10/// @brief Result of a QR factorization: A = Q * R
11///
12/// Q is an mxm orthogonal matrix (Q^T * Q = I).
13/// R is an mxn upper triangular matrix (entries below the diagonal are zero).
14///
15/// For an overdetermined system (m > n), the least-squares solution minimises
16/// ||A*x - b||_2 and is obtained by back-substituting into R[:n,:n] * x = (Q^T*b)[:n].
17/// The residual norm is ||(Q^T*b)[n:]||_2.
18struct QRResult {
19 Matrix Q; ///< mxm orthogonal
20 Matrix R; ///< mxn upper triangular
21};
22
23/// @brief QR factorization of an mxn matrix A (m >= n) via Householder reflections.
24///
25/// Each Householder step k:
26/// 1. Extract column k of the current working matrix from row k downward.
27/// 2. Compute v = x + sign(x[0]) * ||x|| * e_1 (sign chosen to avoid cancellation).
28/// 3. Normalise v.
29/// 4. Apply H_k = I - 2*v*v^T to the trailing submatrix.
30///
31/// After min(m-1, n) steps, the matrix is upper triangular.
32/// Q is reconstructed by accumulating the reflectors in reverse order.
33///
34/// Householder QR is backward-stable (||deltaA||/||A|| = O(u) for machine unit u),
35/// unlike classical Gram-Schmidt which is only forward-stable.
36QRResult qr(const Matrix& A);
37
38/// @brief Solve the least-squares problem min ||A*x - b||_2.
39///
40/// Algorithm:
41/// 1. y = Q^T * b
42/// 2. Back-substitute: solve R[:n,:n] * x = y[:n]
43///
44/// For square non-singular A this returns the exact solution.
45/// For overdetermined A (m > n) this returns the minimum-residual solution.
46void qr_solve(const QRResult& f, const Vector& b, Vector& x);
47
48} // namespace num
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
Matrix operations.
void qr_solve(const QRResult &f, const Vector &b, Vector &x)
Solve the least-squares problem min ||A*x - b||_2.
Definition qr.cpp:119
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
QRResult qr(const Matrix &A)
QR factorization of an mxn matrix A (m >= n) via Householder reflections.
Definition qr.cpp:41
Backend enum for linear algebra operations.
Result of a QR factorization: A = Q * R.
Definition qr.hpp:18
Matrix R
mxn upper triangular
Definition qr.hpp:20
Matrix Q
mxm orthogonal
Definition qr.hpp:19