numerics 0.1.0
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 =
17/// (Q^T*b)[:n]. 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
24/// reflections.
25///
26/// Each Householder step k:
27/// 1. Extract column k of the current working matrix from row k downward.
28/// 2. Compute v = x + sign(x[0]) * ||x|| * e_1 (sign chosen to avoid
29/// cancellation).
30/// 3. Normalise v.
31/// 4. Apply H_k = I - 2*v*v^T to the trailing submatrix.
32///
33/// After min(m-1, n) steps, the matrix is upper triangular.
34/// Q is reconstructed by accumulating the reflectors in reverse order.
35///
36/// @param backend Backend::lapack uses LAPACKE_dgeqrf (default when
37/// available).
38/// Backend::seq uses our Householder implementation.
39QRResult qr(const Matrix &A, Backend backend = lapack_backend);
40
41/// @brief Solve the least-squares problem min ||A*x - b||_2.
42///
43/// Algorithm:
44/// 1. y = Q^T * b
45/// 2. Back-substitute: solve R[:n,:n] * x = y[:n]
46///
47/// For square non-singular A this returns the exact solution.
48/// For overdetermined A (m > n) this returns the minimum-residual solution.
49void qr_solve(const QRResult &f, const Vector &b, Vector &x);
50
51} // namespace num
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
Backend enum for linear algebra operations.
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:26
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
QRResult qr(const Matrix &A, Backend backend=lapack_backend)
QR factorization of an mxn matrix A (m >= n) via Householder reflections.
Definition qr.cpp:15
constexpr Backend lapack_backend
Definition policy.hpp:124
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