Orthogonality and Least Squares/The Gram-Schmidt Process and QR Factorization

Lesson 6.3731 words

The Gram-Schmidt Process and QR Factorization

Gram-Schmidt turns any basis into an orthogonal one by repeatedly subtracting off projections onto the span already built. Normalizing the result and recording the coefficients factors the matrix as A = QR, with Q orthonormal and R upper triangular, the factorization behind stable least-squares and eigenvalue algorithms.

╌╌╌╌

An orthogonal basis makes coordinates and projections trivial, but a subspace usually arrives with an arbitrary basis instead. The Gram–Schmidt process converts one into the other: given any basis for a subspace, it produces an orthogonal basis for the same subspace, one vector at a time. Recording the arithmetic as a matrix product yields the QR factorization, the numerically preferred route to least-squares solutions and eigenvalues.

Subtracting the projection

Start with a basis of a subspace . Keep the first vector, . To make the second vector orthogonal to the first, subtract from its projection onto the line :

By the orthogonal decomposition theorem, is the component of orthogonal to , and it stays in because it is a combination of and . Each later vector subtracts its projection onto the span of all the orthogonal vectors already built.

One Gram-Schmidt step in R^3: v2 is x2 minus its projection p onto the line through x1, leaving the part of x2 perpendicular to x1 while staying in the plane W.

The general process

The span condition is the point of the construction: the orthogonal set built from the first inputs spans exactly the same subspace as those inputs. The proof is induction. Suppose is an orthogonal basis for . Define . By the orthogonal decomposition theorem it is orthogonal to ; it lies in ; and it is nonzero because is not in . So is an orthogonal basis of the -dimensional space .

Gram-Schmidt builds a nested chain of subspaces: v1 spans a line, v2 completes the plane W2, and v3 rises out of it, each new vector orthogonal to every one already built.
Algorithm:Gram-Schmidt(x1,,xp)\textsc{Gram-Schmidt}(\mathbf{x}_1, \dots, \mathbf{x}_p) — orthogonal basis for the span
  1. 1
    v1x1\mathbf{v}_1 \gets \mathbf{x}_1
  2. 2
    for k=2k = 2 to pp do
  3. 3
    vkxk\mathbf{v}_k \gets \mathbf{x}_k
  4. 4
    for j=1j = 1 to k1k-1 do
  5. 5
    vkvkxkvjvjvjvj\mathbf{v}_k \gets \mathbf{v}_k - \dfrac{\mathbf{x}_k \cdot \mathbf{v}_j}{\mathbf{v}_j \cdot \mathbf{v}_j}\, \mathbf{v}_j
    remove the component along vj\mathbf{v}_j
  6. 6
    return {v1,,vp}\{\mathbf{v}_1, \dots, \mathbf{v}_p\}

An orthonormal basis follows by normalizing each at the end. Doing so only after the orthogonal set is complete avoids writing square roots at every step of a hand calculation.

Orthogonalizing a basis

The QR factorization

Apply Gram–Schmidt with normalization to the columns of a matrix. Each original column lies in , so it is a combination of the first orthonormal vectors only. Collecting those combinations as columns records the factorization.

Let be the orthonormal basis from Gram–Schmidt. Because , there are scalars with

and the entries below the diagonal are zero, making upper triangular. Choosing (flip the sign of if needed) makes the diagonal positive. Then for each , so .

A = QR splits an m-by-n matrix with independent columns into an orthonormal Q of the same shape and a small upper-triangular R; the shaded lower part of R is all zeros.

Once is known, is a single product. Since ,

so .

Numerical stability of QR

The invertibility of is immediate: if , then , and the independent columns of force . So exists, and and share the same column space.

In floating point, the classical Gram–Schmidt recurrence loses orthogonality: rounding makes the computed drift out of perpendicularity as grows. Reordering the subtractions (the modified Gram–Schmidt variant) helps, but production QR routines instead left-multiply by a sequence of orthogonal reflectors until it becomes upper triangular. That approach parallels the elementary-matrix reduction behind the LU factorization and yields a more accurate ; the details belong to the numerical least-squares lesson.

In floating point the classical Gram-Schmidt recurrence loses orthogonality as the step index grows, while the reordered (modified) variant stays far closer to orthonormal.
MethodOutputCostNumerical behavior
Classical Gram–Schmidt then normalizeloworthogonality degrades for large
Modified Gram–Schmidtsame, reordered updateslowmarkedly better, still imperfect
Reflector-based QRabout twicemost accurate

Summary

  • Gram–Schmidt converts a basis into an orthogonal basis by subtracting, from each vector, its projection onto the span of the vectors already orthogonalized, preserving the span at every step.
  • Normalization at the end gives an orthonormal basis, the columns of .
  • QR factorization records the process as a matrix product: has orthonormal columns spanning , and is upper triangular and invertible.

The QR factorization gives a stable route to the least-squares solution of an inconsistent system.1

Footnotes

  1. Lay, §6.4 — The Gram–Schmidt Process: Theorem 11 (orthogonalization with the span property), the orthonormal variant, and Theorem 12 (the QR factorization with ), including the numerical notes on loss of orthogonality.

╌╌ END ╌╌