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
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:
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:
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: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 .
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$
Worked Examples
A 2×2 Decomposition by Hand
Given:Solving a Least-Squares Problem with QR
Given: Fit a line through the data points . The overdetermined system is withCommon 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 tonumpy.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
- 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. - Eigenvalue computation (QR algorithm) — LAPACK's
dgeev/dsyevrepeatedly apply shifted QR steps; this underlies eigenvalue routines in essentially all scientific software. - Signal processing & adaptive filters — recursive least-squares (RLS) filters use QR updates (via Givens rotations) for real-time channel equalization and noise cancellation.
- Computer graphics / robotics — orthogonalizing a drifting rotation matrix back to a proper rotation uses the from QR (polar/QR re-orthonormalization).
- Krylov subspace methods — GMRES builds an orthonormal Arnoldi basis whose Hessenberg least-squares subproblem is solved with Givens-based QR each iteration.
- Geodesy and surveying — overdetermined coordinate networks are adjusted by QR-based least squares to control numerical error in large sparse systems.
- 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.