QR Decomposition Explained: Gram-Schmidt, Orthogonal Factors, and Numerical Stability
Understand QR decomposition — how any matrix factors into an orthogonal Q and upper triangular R. Learn the Gram-Schmidt process, see worked examples, and apply QR to least squares and eigenvalue computation.
QR decomposition is one of the most important and widely used matrix factorizations in numerical linear algebra. It decomposes any matrix A into the product A = QR, where Q is an orthogonal matrix (its columns form an orthonormal set) and R is an upper triangular matrix. This factorization is the computational backbone of the QR algorithm for eigenvalues, the standard method for solving least squares problems, and a key ingredient in many signal processing and data analysis algorithms.
The elegance of QR decomposition lies in its construction through the Gram-Schmidt orthogonalization process. Just as you can orthogonalize a set of vectors to form a basis, you can decompose any matrix into an orthonormal factor and a triangular factor that records the "coefficients" of the original columns expressed in terms of the orthonormal basis. This connection between matrix factorization and basis construction gives QR decomposition its geometric meaning and computational power.
Key Takeaway
QR decomposition factors any matrix A = QR where Q has orthonormal columns (QTQ = I) and R is upper triangular. It is computed via the Gram-Schmidt process, Householder reflections, or Givens rotations. QR decomposition is essential for solving least squares problems (x = R−1QTb), computing eigenvalues (the QR algorithm), and is numerically more stable than normal equations for ill-conditioned systems.
1. What Is QR Decomposition?
QR decomposition expresses a matrix A ∈ ℝm×n (with m ≥ n) as the product A = QR, where Q ∈ ℝm×n has orthonormal columns (QTQ = In) and R ∈ ℝn×n is upper triangular with positive diagonal entries. The columns of Q span the same subspace as the columns of A, and R encodes how the original columns relate to the orthonormal basis.
A = Q × R
Q: orthogonal matrix, QTQ = I (columns are orthonormal)
R: upper triangular matrix (Rij = 0 for i > j)
For A ∈ ℝm×n with m ≥ n: Q ∈ ℝm×n, R ∈ ℝn×n.
The Q Factor (Orthonormal Columns)
The matrix Q has orthonormal columns, meaning each column has unit length and any two distinct columns are orthogonal (their dot product is zero). Geometrically, QTQ = I means that applying Q preserves norms and angles — it is an isometry restricted to the column space. The columns of Q form an orthonormal basis for the column space of A (also called the range of A).
The R Factor (Upper Triangular)
The matrix R is upper triangular, meaning all entries below the main diagonal are zero. The diagonal entries Rii are positive and represent the "lengths" of the projections of the original columns of A onto the orthonormal directions. The off-diagonal entries Rij (for i < j) represent the components of column j of A that lie along the first i orthonormal directions. In essence, R records the Gram-Schmidt coefficients.
2. The Gram-Schmidt Process
The classical Gram-Schmidt process constructs the QR decomposition by orthogonalizing the columns of A one at a time. For each column aj, you subtract its projections onto the previously computed orthonormal vectors q₁, q₂, ..., qj−1, and then normalize the result. The projection coefficients become the entries of R.
Step-by-Step Algorithm
- Initialize: Set q₁ = a₁ / ‖a₁‖ and R₁₁ = ‖a₁‖.
- For j = 2 to n: Compute the projection of aj onto each previous qi: Rij = qiT aj for i = 1, ..., j−1.
- Subtract projections: v = aj − Σi=1j−1 Rij qi.
- Normalize: Rjj = ‖v‖ and qj = v / Rjj.
Projection: projq(a) = (qTa / qTq) × q = (qTa) × q (when ‖q‖ = 1)
Gram-Schmidt for column j:
vj = aj − Σi=1j−1 (qiTaj) qi
Rjj = ‖vj‖, qj = vj / Rjj
Worked Example: 3×2 Matrix
Let A = [[1, 1], [1, 0], [0, 1]]. The columns are a₁ = [1, 1, 0]T and a₂ = [1, 0, 1]T.
Step 1: q₁ = a₁ / ‖a₁‖ = [1, 1, 0]T / √2 ≈ [0.707, 0.707, 0]T. R₁₁ = √2 ≈ 1.414.
Step 2: R₁₂ = q₁T a₂ = 0.707 × 1 + 0.707 × 0 + 0 × 1 = 0.707.
Step 3: v₂ = a₂ − R₁₂ q₁ = [1, 0, 1] − 0.707 × [0.707, 0.707, 0] = [0.5, −0.5, 1].
Step 4: R₂₂ = ‖v₂‖ = √(0.25 + 0.25 + 1) = √1.5 ≈ 1.225. q₂ = v₂ / R₂₂ ≈ [0.408, −0.408, 0.816]T.
| Step | Operation | Result |
|---|---|---|
| 1 | Normalize a₁ | q₁ ≈ [0.707, 0.707, 0]T, R₁₁ ≈ 1.414 |
| 2 | Project a₂ onto q₁ | R₁₂ ≈ 0.707 |
| 3 | Subtract projection | v₂ = [0.5, −0.5, 1] |
| 4 | Normalize v₂ | q₂ ≈ [0.408, −0.408, 0.816]T, R₂₂ ≈ 1.225 |
The final result is Q = [[0.707, 0.408], [0.707, −0.408], [0, 0.816]] and R = [[1.414, 0.707], [0, 1.225]]. You can verify: QR = [[0.707, 0.408], [0.707, −0.408], [0, 0.816]] × [[1.414, 0.707], [0, 1.225]] ≈ [[1, 1], [1, 0], [0, 1]] = A.
3. Computing QR: Full Example with a 3×3 Matrix
For a full 3×3 matrix A, the QR decomposition proceeds identically — you orthogonalize three columns. Consider A = [[1, 1, 0], [1, 0, 1], [0, 1, 1]]. After applying Gram-Schmidt:
q₁ = [1/√2, 1/√2, 0]T, q₂ = [1/√6, −1/√6, 2/√6]T, q₃ = [1/√3, −1/√3, −1/√3]T.
The R matrix is computed as R = QTA, which is the most direct way to obtain R once Q is known:
R = QT A
R₁₁ = q₁Ta₁ = √2 ≈ 1.414
R₁₂ = q₁Ta₂ = 1/√2 ≈ 0.707
R₁₃ = q₁Ta₃ = 1/√2 ≈ 0.707
R₂₂ = q₂Ta₂ = √(3/2) ≈ 1.225
R₂₃ = q₂Ta₃ = −1/√6 ≈ −0.408
R₃₃ = q₃Ta₃ = √3 ≈ 1.732
R = [[1.414, 0.707, 0.707], [0, 1.225, −0.408], [0, 0, 1.732]]
Note that Rij = 0 for i > j (upper triangular), and all diagonal entries are positive — this is the standard QR decomposition with positive diagonal R. The factorization is unique when A has full column rank.
4. Properties and Applications
Solving Least Squares Problems
The primary application of QR decomposition is solving the least squares problem: minimize ‖Ax − b‖₂ for an overdetermined system (m > n). Using A = QR, the normal equations become:
‖Ax − b‖₂ = ‖QRx − b‖₂ = ‖QT(QRx − b)‖₂ (since QT preserves norms)
= ‖Rx − QTb‖₂
Minimized when: Rx = QTb (top n rows)
Solution: x = R−1 (QTb)1:n
This avoids computing ATA, which squares the condition number. QR is numerically superior.
The key advantage over the normal equations (x = (ATA)−1ATb) is numerical stability. Computing ATA squares the condition number of the problem, amplifying rounding errors. QR decomposition avoids this squaring, making it the preferred method for ill-conditioned systems.
The QR Algorithm for Eigenvalues
The QR algorithm computes all eigenvalues of a matrix by repeatedly applying QR decomposition and reversing the factors. Starting with A₀ = A, compute Ak+1 = Rk Qk (reversed order from Ak = Qk Rk). The matrices Ak converge to an upper triangular (or Schur) form whose diagonal entries are the eigenvalues. With shifts and deflation, this algorithm is the standard method for computing eigenvalues in production software.
Solving Linear Systems
For a square invertible system Ax = b, QR decomposition gives x = R−1QTb. Back substitution on the triangular system Rx = QTb solves for x in O(n²) time. While LU decomposition is generally faster for square systems, QR is preferred when the matrix is ill-conditioned or when you also need the condition number (which comes free from the R factor).
- Numerical stability: QR decomposition does not square the condition number, making it safer than normal equations.
- Minimum-norm solutions: For underdetermined systems, QR decomposition of AT gives the minimum-norm solution.
- Rank determination: The diagonal entries of R reveal the numerical rank of A — small Rii values indicate near-dependence.
- Updating solutions: QR decomposition can be efficiently updated when a row or column is added (Givens rotations), making it useful for recursive least squares.
5. Common Mistakes
- Using normal equations instead of QR: Computing (ATA)−1ATb squares the condition number. Always prefer QR decomposition for ill-conditioned problems.
- Confusing Q with QT: When solving Ax = b via QR, you compute QTb (not Qb). QT rotates the right-hand side into the coordinate system defined by Q.
- Assuming R has positive diagonal: The standard QR decomposition requires positive diagonal entries in R. If you use Householder reflections, some Rii may be negative — multiply the corresponding column of Q and row of R by −1 to fix this.
- Using Gram-Schmidt for large matrices: Classical Gram-Schmidt is numerically unstable for large or ill-conditioned matrices. Modified Gram-Schmidt or Householder reflections should be used instead.
- Forgetting QR is for m ≥ n: QR decomposition is defined for matrices with at least as many rows as columns. For m < n, decompose AT instead.
6. Frequently Asked Questions
Is QR decomposition unique?
Yes, when A has full column rank and we require positive diagonal entries in R, the QR decomposition is unique. If A is rank-deficient, R may have zero diagonal entries and Q is not uniquely determined.
What is the difference between Gram-Schmidt, Householder, and Givens QR?
Gram-Schmidt orthogonalizes columns sequentially. Householder uses reflection matrices to zero out subdiagonal entries, working column by column from left to right. Givens rotations zero out individual entries using 2×2 rotation matrices. Householder is the most numerically stable and efficient for dense matrices. Givens rotations are preferred for sparse or banded matrices because they can target specific entries.
How does QR decomposition relate to LU decomposition?
Both are matrix factorizations used for solving linear systems. LU factors A = LU (lower × upper triangular) and is faster for square systems. QR factors A = QR (orthogonal × upper triangular) and is more stable for ill-conditioned and overdetermined systems. QR is preferred for least squares; LU is preferred for square systems with multiple right-hand sides.
Can QR decomposition be used for non-square matrices?
Yes. For an m × n matrix with m > n (more rows than columns), the "thin" QR gives Q as m × n and R as n × n. For m < n, decompose AT to get a "thin" QR of the transpose. The full (economy) QR uses the complete orthonormal basis but is less efficient.
What is the computational complexity of QR decomposition?
For an m × n matrix with m ≥ n: Gram-Schmidt is O(mn²), Householder is O(2mn² − 2n³/3), and Givens rotations are O(3mn²). Householder is the standard for dense matrices. For sparse matrices, the complexity depends on the sparsity pattern and can be significantly lower.
Try It Yourself
Use our QR Decomposition Calculator to factor any matrix into its Q and R components. Related tools: LU Decomposition Calculator, SVD Calculator, Matrix Rank Calculator.