QR Decomposition

QR Decomposition factors any matrix into a product , where has orthonormal columns and is upper triangular. It exists for every real or complex matrix, is the engine behind least-squares solvers and the QR algorithm for eigenvalues, and was developed through the work of Gram, Schmidt, Householder, and Givens in the late 19th and 20th centuries.

The QR decomposition expresses a matrix (with ) as , where the columns of form an orthonormal set and is upper (right) triangular. Geometrically, it performs Gram–Schmidt orthogonalization on the columns of , recording the projection coefficients in . It matters because orthonormal bases make numerical computation stable: solving least-squares problems, computing eigenvalues, and orthogonalizing data all reduce to QR. A surprising fact is that although the "textbook" Gram–Schmidt procedure that motivates QR is numerically unreliable, the same decomposition computed via Householder reflections is one of the most stable algorithms in all of numerical linear algebra.

The Core Insight

Any spanning set of vectors can be replaced by an orthonormal basis built one vector at a time, subtracting off the parts already accounted for. The orthonormal vectors become the columns of ; the coefficients describing how each original column is reconstructed from the orthonormal ones form the upper-triangular . Triangularity is not imposed — it falls out automatically because the -th original column lies in the span of only the first orthonormal vectors.

Formal Statement

QR Decomposition

Let with . A QR decomposition of is a factorization

1
⟳This is the defining equation; its existence is established constructively in the proof below.

where the symbols mean:

  • — the input matrix whose columns are ;
  • — an matrix with orthonormal columns, i.e. (in the reduced / thin form);
  • — an upper-triangular matrix, with entries for .

In the full form, is orthogonal () and is upper triangular with zero rows at the bottom:

2
⟳Append orthonormal vectors to the columns of the thin to complete an orthonormal basis of ; the extra columns multiply the zero block.

Existence and Uniqueness

Every admits a QR decomposition. If and has full column rank (), then the thin decomposition is unique under the normalization that the diagonal entries of are positive:

3
⟳Uniqueness follows from the uniqueness of Gram–Schmidt with a fixed sign convention, or equivalently from the uniqueness of the Cholesky factor of .

Why It Holds / Derivation

Constructive Existence via Gram–Schmidt

Assume has full column rank with columns .

Step 1. Define the first orthogonal vector and normalize:
4
⟳ by full rank, so dividing by its norm is valid; .
Step 2. For , subtract the components of along the already-built orthonormal vectors:
5
⟳Each term is the orthogonal projection coefficient onto ; removing them leaves for all . Full rank guarantees .
Step 3. By construction is orthonormal, so satisfies . Solving the relations above for gives
6
with and for .
⟳Rearrange equation [5]; the sum ranges only over , which is exactly the upper-triangular pattern.
Step 4. Collecting these relations into matrix form yields where is upper triangular with positive diagonal.
⟳The -th column of equals , matching equation [6] column-by-column.

Uniqueness

Suppose with both 's having positive diagonals. Then . The product of upper-triangular matrices is upper triangular, so is upper triangular; but is orthogonal. An orthogonal upper-triangular matrix is diagonal, and a diagonal orthogonal matrix has entries . Positive diagonals of force , hence and .

⟳Key facts — (i) inverse of upper triangular is upper triangular; (ii) an orthogonal matrix whose entries vanish below the diagonal must be diagonal because its columns are unit-length and mutually orthogonal.

Erhard Schmidt | 1876–1959

Erhard Schmidt, a student of David Hilbert, formalized the orthogonalization procedure in 1907 while developing the theory of integral equations and Hilbert spaces — the abstract setting where "orthonormal basis" became a precise concept. Jørgen Pedersen Gram had introduced essentially the same idea in 1883 in the context of least squares. Although neither wrote "," their procedure is the conceptual heart of the decomposition. The numerically robust matrix algorithms (Householder, Givens) arrived only with the computer era in the 1950s–60s.

Building Intuition

Think of the columns of as a skewed, non-perpendicular set of axes. Gram–Schmidt straightens them: points along ; keeps only the part of perpendicular to ; keeps only the part of perpendicular to the plane spanned by ; and so on. The matrix is the "translation dictionary" telling you how to rebuild the original skewed columns from the clean perpendicular ones. Because only ever involves , the dictionary is upper triangular. Multiplying by an orthogonal is a rotation/reflection — it preserves lengths and angles — so QR says: every set of vectors is a rigid rotation of a "stretched-and-sheared" triangular set.

Gram–Schmidt: projecting out the parallel component to build $q_2$ from $a_2$

a₁ (= q₁ dir.) a₂ proj = (q₁ᵀa₂) q₁ u₂ = a₂ − proj q₂ (normalized)

Worked Examples

A 2×2 Decomposition by Hand

Given:
7
⟳We apply equations [4]–[6] directly.
Step 1 — first column. , so
8
⟳.
Step 2 — second column. Projection coefficient:
9
⟳Dot product of with .
10
⟳Subtract the parallel part; note since their dot product is .
11
⟳.
Boxed answer:
12
⟳Verify : the entry is , etc. Quantities here are dimensionless.

Solving a Least-Squares Problem with QR

Given: Fit a line through the data points . The overdetermined system is with
13
⟳Each row encodes .
Step 1 — QR of . Column 1: , , . Column 2: . Then
14
⟳Subtract the mean (here removes the average value from each entry of ).
Step 2 — transform the right side. The least-squares solution solves :
15
⟳Multiply by each orthonormal column.
Step 3 — back-substitution in , with :
16
⟳Solve the bottom equation first, substitute upward.
Boxed answer:
17
⟳This is the same answer the normal equations give, but obtained more stably. Variables carry the units of and respectively.

Common Misconceptions

The decomposition is not unique without a sign convention. If you negate a column of and the corresponding row of , you get another valid QR. Software packages (LAPACK, NumPy) do not generally return positive diagonal ; comparing your hand result to numpy.linalg.qr may show sign flips that are entirely legitimate. Uniqueness in Theorem [3] holds only after fixing . Classical Gram–Schmidt is numerically dangerous. Although equations [4]–[6] are mathematically exact, in floating point the computed columns of lose orthogonality catastrophically when the columns of are nearly dependent. Never implement classical Gram–Schmidt for serious numerical work — use Modified Gram–Schmidt, Householder reflections, or Givens rotations instead, all of which compute the same but with vastly better stability.

Consider with (a Läuchli-type matrix). Classical Gram–Schmidt computes whose inner product with is of order in floating point — orthogonality instead of . Modified Gram–Schmidt cuts this error to machine precision, and Householder QR delivers orthogonality to full machine precision regardless of conditioning. This is the classic demonstration that the same exact decomposition can be computed well or badly.

Connections

QR sits at the crossroads of several pillars of linear algebra. It is the matrix encoding of Gram-Schmidt Process, and its factor is precisely the Cholesky Decomposition factor of the normal matrix (since ). It is the workhorse of Least Squares Problems, where solving avoids forming the ill-conditioned . Iterated QR is the QR Algorithm for Eigenvalues, the standard method for computing the Eigenvalues and Eigenvectors of dense matrices. It complements the Singular Value Decomposition (rank-revealing but costlier) and the LU Decomposition (for square solves), and the orthogonal embodies Orthogonal Matrices.

Real-World Applications

  1. Least-squares regression & data fitting — every linear regression in statistics software (R's lm, MATLAB's backslash) factors the design matrix via QR to find best-fit coefficients stably.
  2. Eigenvalue computation (QR algorithm) — LAPACK's dgeev/dsyev repeatedly apply shifted QR steps; this underlies eigenvalue routines in essentially all scientific software.
  3. Signal processing & adaptive filters — recursive least-squares (RLS) filters use QR updates (via Givens rotations) for real-time channel equalization and noise cancellation.
  4. Computer graphics / robotics — orthogonalizing a drifting rotation matrix back to a proper rotation uses the from QR (polar/QR re-orthonormalization).
  5. Krylov subspace methods — GMRES builds an orthonormal Arnoldi basis whose Hessenberg least-squares subproblem is solved with Givens-based QR each iteration.
  6. Geodesy and surveying — overdetermined coordinate networks are adjusted by QR-based least squares to control numerical error in large sparse systems.
  7. Machine learning — QR-based orthogonalization stabilizes solving linear models, computing pseudoinverses, and whitening features.

Historical Development

  • 1883 — Jørgen Pedersen Gram introduces an orthogonalization procedure in his study of least squares and series expansions.
  • 1907 — Erhard Schmidt, working in Hilbert's circle on integral equations, presents the procedure in the abstract Hilbert-space setting; it becomes "Gram–Schmidt."
  • 1909 — Issai Schur and others develop triangular factorizations of matrices, foreshadowing matrix forms of orthogonalization.
  • 1948 — Alan Turing's analysis of rounding errors in matrix computations launches modern numerical stability theory, motivating better factorizations.
  • 1951–1954 — Cornelius Lanczos and Walter Arnoldi develop iterative orthogonalization (Krylov) methods that rely on QR-type steps.
  • 1958 — Alston Householder introduces elementary reflectors (Householder transformations), giving a backward-stable way to compute QR.
  • 1958–1961 — John G. F. Francis (and independently Vera Kublanovskaya) invent the QR algorithm for eigenvalues, one of the top algorithms of the 20th century.
  • 1958 — Wallace Givens formalizes plane-rotation ("Givens") QR, ideal for sparse and streaming updates.
  • 1965–1971 — Gene Golub and collaborators establish QR as the standard reliable method for least squares and codify it in what becomes LINPACK/LAPACK.

Subtleties & Limits

Rank-deficiency and rank-revealing QR. When does not have full column rank, the plain QR can place tiny or zero pivots anywhere, masking the true rank. Rank-revealing QR (RRQR) uses column pivoting ( with permutation ) to push the "small" directions to the trailing columns, but no pivoting strategy is provably optimal for all matrices — constructing a strong rank-revealing factorization that is both cheap and guaranteed accurate remains an active area. Relatedly, communication-avoiding QR (TSQR/CAQR) for tall-skinny matrices on parallel hardware trades classical pivoting for blocked orthogonalization, and balancing stability against minimal data movement on modern GPUs/exascale machines is an ongoing engineering and analysis challenge. For the complex case, becomes unitary () and the same theory carries over, but high-performance complex QR with careful rounding analysis still attracts research.

See Also

Gram-Schmidt Process · Cholesky Decomposition · LU Decomposition · Singular Value Decomposition · Orthogonal Matrices · Householder Reflections · Givens Rotations · Least Squares Problems · QR Algorithm for Eigenvalues

Before this

Where this leads