Home › Matrix Math › Lesson 7

Inner Products, Gram–Schmidt and QR

Lesson 7 of 12

Key idea of this section

A = QR,  Q*Q = I,  R upper triangular        Pb = QQ*b is the closest point of col(A) to b
least squares:  A*A x = A*b   ⇔   R x = Q*b

Inner products and the Cauchy–Schwarz inequality

An inner product on a vector space V over 𝔽 is a map ⟨·, ·⟩ : V × V → 𝔽 that is linear in its first argument, conjugate symmetric (⟨y, x⟩ is the complex conjugate of ⟨x, y⟩) and positive definite (⟨x, x⟩ > 0 for x ≠ 0). The standard inner product on 𝔽ⁿ is ⟨x, y⟩ = y*x = ∑ xi ȳi, with norm ‖x‖ = ⟨x, x⟩1/2. Other important examples are ⟨x, y⟩_M = y*Mx for a positive definite matrix M (Lesson 9), and ⟨f, g⟩ = ∫₀¹ f(t) g(t) dt on polynomials, which leads to the Legendre polynomials and to the Hilbert matrix of Lesson 12.

Theorem 7.1 (Cauchy–Schwarz). |⟨x, y⟩| ≤ ‖x‖ ‖y‖, with equality if and only if x and y are linearly dependent.

Proof. The case y = 0 is trivial. Otherwise let p = (⟨x, y⟩/‖y‖²) y. Then ⟨x − p, y⟩ = ⟨x, y⟩ − ⟨x, y⟩ = 0, so x − p ⊥ p and Pythagoras gives ‖x‖² = ‖p‖² + ‖x − p‖² ≥ ‖p‖² = |⟨x, y⟩|²/‖y‖². Equality holds exactly when x = p, that is, when x is a multiple of y. ∎

The triangle inequality ‖x + y‖ ≤ ‖x‖ + ‖y‖ follows by expanding ‖x + y‖² and applying Cauchy–Schwarz to the cross term.

Gram–Schmidt and the QR factorization

Given linearly independent a₁, …, aₙ, the Gram–Schmidt process sets q₁ = a₁/‖a₁‖ and, for k ≥ 2,

vk = ak − ∑j<k ⟨ak, qj⟩ qj,        qk = vk / ‖vk‖

By induction q₁, …, qk is an orthonormal basis of span(a₁, …, ak) for every k; vk ≠ 0 because ak is not in the span of its predecessors. Solving for ak gives ak = ∑j≤k rjk qj with rjk = ⟨ak, qj⟩ for j < k and rkk = ‖vk‖ > 0. In matrix form this is the QR factorization.

Theorem 7.2 (QR factorization). If A ∈ 𝔽m×n has linearly independent columns, then A = QR with Q ∈ 𝔽m×n having orthonormal columns and R ∈ 𝔽n×n upper triangular with positive diagonal. This factorization is unique.

Proof. Existence is Gram–Schmidt. For uniqueness, suppose Q₁R₁ = Q₂R₂. Then A*A = R₁*R₁ = R₂*R₂, so the matrix X = R₂R₁⁻¹ equals (R₂*)⁻¹R₁* = (R₁R₂⁻¹)*. The first expression is upper triangular and the last is lower triangular, so X is diagonal with positive diagonal entries. Moreover X*X = R₁⁻* R₂*R₂ R₁⁻¹ = R₁⁻* R₁*R₁ R₁⁻¹ = I, so each diagonal entry has modulus 1 and therefore equals 1. Hence R₁ = R₂ and Q₁ = Q₂. ∎

Worked example: let a₁ = (1, 1, 0) and a₂ = (1, 0, 1). Then q₁ = (1, 1, 0)/√2, ⟨a₂, q₁⟩ = 1/√2, and v₂ = a₂ − (1/2)(1, 1, 0) = (1/2, −1/2, 1) with ‖v₂‖ = √(3/2). Thus q₂ = (1, −1, 2)/√6 and

[ 1  1 ]   [ 1/√2    1/√6 ]
[ 1  0 ] = [ 1/√2   −1/√6 ] [ √2   1/√2  ]
[ 0  1 ]   [ 0       2/√6 ] [ 0    √(3/2) ]

Orthogonal projection and least squares

Let W be a subspace with orthonormal basis given by the columns of Q, and let P = QQ*. Then P² = P = P*, Pb ∈ W, and b − Pb ⊥ W.

Theorem 7.3 (Best approximation). For every w ∈ W, ‖b − w‖ ≥ ‖b − Pb‖, with equality only for w = Pb.

Proof. Write b − w = (b − Pb) + (Pb − w). The first summand is orthogonal to W and the second lies in W, so ‖b − w‖² = ‖b − Pb‖² + ‖Pb − w‖². ∎

Now consider an overdetermined system Ax = b with A ∈ 𝔽m×n, m > n, of full column rank. The least-squares problem asks for x minimizing ‖Ax − b‖. By Theorem 7.3 the minimizer satisfies Ax = Pb, the projection of b onto col(A); equivalently the residual is orthogonal to col(A), that is, A*(b − Ax) = 0. These are the normal equations A*A x = A*b. Since A*A is invertible when A has full column rank, x = (A*A)⁻¹A*b, and the projection onto col(A) is P = A(A*A)⁻¹A*. With A = QR the normal equations collapse to R x = Q*b, which is solved by back substitution.

Worked example: fit a line y = c₀ + c₁t to the data (0, 1), (1, 2), (2, 2). Here A has rows (1, 0), (1, 1), (1, 2) and b = (1, 2, 2), so AᵀA = [ 3 3 ; 3 5 ] and Aᵀb = (5, 6). The normal equations give c₀ = 7/6 and c₁ = 1/2. The residuals are −1/6, 1/3, −1/6; they sum to zero and are orthogonal to (0, 1, 2), as the normal equations require.

Householder reflections and numerical orthogonality

For a nonzero vector v, the Householder matrix H = I − 2vv*/(v*v) is Hermitian and unitary, and it reflects in the hyperplane orthogonal to v. Given x, the choice v = x + sign(x₁)‖x‖e₁ (for real x) yields Hx = −sign(x₁)‖x‖e₁, and the sign is chosen to avoid cancellation. Applying n such reflections annihilates the subdiagonal of A column by column and produces A = QR with Q orthogonal to working precision. In floating-point arithmetic the classical Gram–Schmidt process can lose orthogonality badly when A is ill conditioned; the modified Gram–Schmidt process, which subtracts each projection from the current vector immediately, loses orthogonality only in proportion to κ(A) times the unit roundoff (Björck, 1967); Householder QR is backward stable regardless of conditioning. Solving least-squares problems through the normal equations is avoided in practice because κ(A*A) = κ(A)², a fact explained in Lesson 12.

The process is named after Jørgen Pedersen Gram (1883) and Erhard Schmidt (1907), although the underlying computation already appears in work of Laplace. Alston Householder introduced his reflections for QR factorization in 1958.

Exercise

  • (a)
    Find the matrix of orthogonal projection onto the line spanned by (1, 2) in ℝ².
  • (b)
    Find the distance from b = (1, 2, 3) to the line spanned by (1, 1, 1).
  • (c)
    Find the Householder reflection H with H(3, 4) = (5, 0).
Show solution

Answers: (a) P = aaᵀ/(aᵀa) = (1/5) [ 1 2 ; 2 4 ]. (b) The projection is ((1 + 2 + 3)/3)(1, 1, 1) = (2, 2, 2), the residual is (−1, 0, 1), and the distance is √2. (c) Take v = (3, 4) − (5, 0) = (−2, 4); then H = I − 2vvᵀ/20 = I − (1/10) [ 4 −8 ; −8 16 ] = [ 0.6 0.8 ; 0.8 −0.6 ], and indeed H(3, 4) = (1.8 + 3.2, 2.4 − 2.4) = (5, 0).

V.3.1