QR Decomposition
QR decomposition is a matrix factorization in numerical linear algebra that expresses a real or complex matrix as the product of an orthogonal or unitary matrix Q and an upper triangular matrix R.
QR decomposition, also known as QR factorization, is a foundational procedure in numerical linear algebra and scientific computing that factors any real matrix A of dimension m × n (with m ≥ n) into the product A = QR, where Q is an orthogonal matrix satisfying QTQ = I and R is an upper triangular matrix. For complex-valued matrices, Q is a unitary matrix satisfying QQ = I, where Q denotes the conjugate transpose. Because orthogonal transformations preserve the Euclidean norm of vectors and leave the spectral condition number of a matrix unchanged, QR decomposition serves as the standard numerical backbone for solving overdetermined linear least-squares problems, constructing orthonormal bases of Krylov subspaces in methods such as the generalized minimal residual method, and computing matrix eigenvalues via the iterative QR algorithm.
Unlike LU decomposition, which relies on non-orthogonal row operations that can amplify rounding errors unless stabilized by pivoting, or Cholesky decomposition, which applies only to symmetric positive-definite matrices, QR factorization exists for every rectangular or square matrix. Computing Q via Householder reflections or Givens rotations achieves backward stability down to machine epsilon, avoiding the condition-number squaring of the normal equations ATAx = ATb.
Mathematical Formulation and Full vs. Reduced Forms
Let A ∈ ℝm×n be a rectangular matrix with m ≥ n. The full QR decomposition represents A as:
A = QR = [Q1 Q2] [R1 ; 0]
Here, Q ∈ ℝm×m is a square orthogonal matrix partitioned into Q1 ∈ ℝm×n, whose columns form an orthonormal basis for range(A) when A has full column rank, and Q2 ∈ ℝm×(m−n), whose columns span the orthogonal complement null(AT). The upper block R1 ∈ ℝn×n is upper triangular, while the bottom block consists of m − n rows of zeros.
Dropping the zero rows of R and the columns of Q2 yields the thin (or reduced / economy-size) QR decomposition A = Q1R1. If A has full column rank and the diagonal entries of R1 are positive (rii > 0), the thin QR factorization is unique, and R1 coincides with the upper triangular Cholesky factor of the Gram matrix ATA = R1TR1.
Algorithms for Computing QR Factorization
Three algorithmic families construct the QR factorization of a dense or sparse matrix, trading arithmetic operation counts against numerical orthogonality and parallelizability.
Gram–Schmidt Orthogonalization and Modified Gram–Schmidt
The classical Gram–Schmidt (CGS) process builds the columns q1, …, qn of Q1 sequentially by subtracting from each column aj of A its orthogonal projections onto the previously computed vectors q1, …, qj−1:
vj = aj − ∑i=1j−1 (qiTaj) qi, rij = qiTaj, rjj = ‖vj‖2, qj = vj / rjj.
Although algebraically transparent and requiring 2mn2 floating-point operations (flops), classical Gram–Schmidt suffers from substantial loss of orthogonality in finite-precision arithmetic whenever A is ill-conditioned: subtracting nearly parallel vectors triggers numerical cancellation, causing ‖Q1TQ1 − I‖2 to grow without bound. Modified Gram–Schmidt (MGS) reorganizes the inner loop as a sequence of rank-one projections against each freshly normalized vector qi. MGS performs the same 2mn2 flops as CGS but bounds the departure from orthogonality by O(εmach κ2(A)), where κ2(A) = σmax(A)/σmin(A) is the 2-norm condition number. When machine-precision orthogonality is required inside Krylov solvers such as the Lanczos algorithm or GMRES, MGS is paired with a second reorthogonalization pass ("twice is enough," as proved by Kahan and Parlett).
Householder Reflections
Introduced by Alston Scott Householder in 1958, Householder QR operates from the left by applying a sequence of n orthogonal elementary reflectors H1, H2, …, Hn to zero out all subdiagonal entries of A one column at a time. Each Householder matrix has the form:
Hk = I − 2 vkvkT / (vkTvk),
where the reflection vector vk = x + sign(x1)‖x‖2e1 maps the active subcolumn x onto a multiple of the first unit coordinate vector e1 without numerical cancellation. After n steps, Hn⋯H1A = R, and Q = H1⋯Hn is stored implicitly below the diagonal of R. Householder QR requires 2mn2 − (2/3)n3 flops and achieves unconditional backward stability: ‖Q̂TQ̂ − I‖2 = O(εmach) independent of κ2(A). Modern libraries such as LAPACK aggregate groups of b reflectors into a block WY form I − VTVT to execute Level-3 BLAS matrix-matrix multiplications.
Givens Rotations
Whereas a Householder reflector zeros an entire subcolumn simultaneously, a Givens rotation G(i, k, θ) is a plane rotation matrix that differs from the identity only in rows and columns i and k, with cosine c = cos θ on the diagonal and sine s = ±sin θ on the off-diagonal. Choosing c = a / √(a2 + b2) and s = −b / √(a2 + b2) selectively zeroes a single entry b. For a dense m × n matrix, Givens QR requires 3mn2 − n3 flops, but for upper Hessenberg matrices in Arnoldi iteration or tridiagonal matrices in the Lanczos algorithm, Givens rotations reduce the matrix to upper triangular form in only O(n2) or O(n) operations.
| Method | Arithmetic Cost (m ≥ n) | Orthogonality Error ‖Q̂TQ̂ − I‖2 | Primary Computational Use Case |
|---|---|---|---|
| Classical Gram–Schmidt (CGS) | 2mn2 flops | Can reach O(1) for ill-conditioned A | Parallel block orthogonalization with reorthogonalization |
| Modified Gram–Schmidt (MGS) | 2mn2 flops | O(εmach κ2(A)) | Incremental Krylov basis construction in GMRES |
| Householder Reflections | 2mn2 − (2/3)n3 flops | O(εmach) (unconditional) | Dense least-squares solvers and LAPACK dgeqrf |
| Givens Rotations | 3mn2 − n3 flops | O(εmach) (unconditional) | Sparse, banded, and upper Hessenberg matrices |
Applications in Least Squares, Eigenvalues, and Data Science
Linear Least Squares and Rank-Revealing QR
Given an overdetermined linear system Ax ≈ b with m > n, minimizing the Euclidean residual ‖Ax − b‖22 via the normal equations ATAx = ATb squares the condition number to κ2(ATA) = [κ2(A)]2. Using the orthogonal invariance of the 2-norm, ‖Ax − b‖22 = ‖R1x − Q1Tb‖22 + ‖Q2Tb‖22. The least-squares minimizer x̂ is therefore obtained by solving the n × n upper triangular system R1x̂ = Q1Tb by back-substitution, retaining a condition sensitivity proportional to κ2(A). When A is rank-deficient, QR with column pivoting (AP = QR, where P is a permutation matrix selected at step k to swap in the column with the largest remaining residual norm) exposes the numerical rank of A along the diagonal of R and underpins subset selection and principal component analysis preprocessing.
The QR Algorithm for Eigenvalue Problems
Developed independently by John G. F. Francis and Vera N. Kublanovskaya in 1961–1962, the QR algorithm computes all eigenvalues and eigenvectors of a square matrix A ∈ ℝn×n. After an initial orthogonal reduction A0 = UTAU to upper Hessenberg form, each iteration factors the shifted matrix Ak − μkI = QkRk and forms the reverse product Ak+1 = RkQk + μkI = QkTAkQk. With Wilkinson shifts μk, the subdiagonal entries decay at a quadratic or cubic rate, deflating Ak into real Schur form.
Frequently Asked Questions
Why is QR decomposition preferred over normal equations for least squares?
Forming the Gram matrix ATA squares the condition number of A: if κ2(A) = 108, then κ2(ATA) = 1016, exhausting IEEE 754 double-precision mantissa bits and causing Cholesky decomposition of ATA to lose accuracy. Householder QR works directly on A without squaring κ2(A).
Do we need to form the full matrix Q explicitly?
Rarely. In Householder QR, applying QTb using the stored reflection vectors v1, …, vn takes only 2mn − n2 flops, whereas multiplying out Q ∈ ℝm×m explicitly requires 4(m2n − mn2 + n3/3) flops and m2 memory words.
References
- Householder, A. S. (1958). Unitary triangularization of a nonsymmetric matrix. Journal of the ACM, 5(4), 339–342. https://doi.org/10.1145/320941.320947
- Francis, J. G. F. (1961). The QR transformation: A unitary analogue to the LR transformation—Part 1. The Computer Journal, 4(3), 265–271. https://doi.org/10.1093/comjnl/4.3.265
- Björck, Å. (1967). Solving linear least squares problems by Gram–Schmidt orthogonalization. BIT Numerical Mathematics, 7(1), 1–21. https://doi.org/10.1007/BF01934122
- Golub, G. H., & Van Loan, C. F. (2013). Matrix Computations (4th ed.). Johns Hopkins University Press. https://doi.org/10.56021/9781421407944