Guides / Linear algebra
Gram–Schmidt and the QR decomposition: subtract the projections, normalize, then R = QᵀA
Short answer
The projection of b onto the line through a is a · ba · a a; onto a unit vector q it is (q · b) q. Gram–Schmidt: take q1 = a1/‖a1‖; for each next vector, subtract its projection onto every q already found, uj = aj − Σ(qi · aj) qi, then normalize, qj = uj/‖uj‖. QR: the q's are the columns of Q, and R = QTA is upper triangular, with the lengths ‖uj‖ on its diagonal. Least squares: solve R x̂ = QTb by back substitution.
Why it works
Split b into a part along a and a part perpendicular to a. The part along a is some multiple ta, and the rest, b − ta, must have zero dot product with a: a · b − t(a · a) = 0, so t = (a · b)/(a · a). Subtracting the projection leaves exactly the perpendicular part. Gram–Schmidt does this repeatedly: what is left of aj after removing its components along q1, …, qj−1 is perpendicular to all of them. Because the q's are already perpendicular to each other, the projections can be subtracted one at a time without interfering.
Rearranged, each step says aj = (q1 · aj) q1 + ⋯ + ‖uj‖ qj: column j of A is a combination of the first j columns of Q only. Those coefficients are column j of R, which is why R is upper triangular. Since QTQ = I, multiplying A = QR on the left by QT gives R = QTA.
Example 1: projection onto a line
Project b = (4, 7) onto the line through a = (1, 2).
- a · b = 4 + 14 = 18, a · a = 1 + 4 = 5The two dot products the formula needs.
- p = 185(1, 2) = (3.6, 7.2)The coefficient 18/5 is a · b divided by a · a, because a isn't a unit vector.
- b − p = (0.4, −0.2), (0.4, −0.2) · (1, 2) = 0The leftover is perpendicular to a, which is the check that the projection is right.
Example 2: Gram–Schmidt in the plane, and coordinates by dot products
Apply Gram–Schmidt to a1 = (1, 2) and a2 = (3, 1), then write x = (4, 7) in the new basis.
- q1 = 1√5(1, 2)‖a1‖ = √5.
- u2 = (3, 1) − 55(1, 2) = (2, −1)a1 · a2 = 3 + 2 = 5 and a1 · a1 = 5. Check: (2, −1) · (1, 2) = 0.
- q2 = 1√5(2, −1)‖u2‖ = √5.
- x = 18√5 q1 + 1√5 q2In an orthonormal basis each coordinate is a dot product: q1 · x = (4 + 14)/√5 and q2 · x = (8 − 7)/√5. No system to solve. Check: (18(1, 2) + (2, −1))/5 = (20, 35)/5 = (4, 7).
Example 3: the QR decomposition of a 3×2 matrix
Find the QR decomposition of A = 24152−2.
- q1 = 13(2, 1, 2), r11 = 3‖a1‖ = √(4 + 1 + 4) = 3.
- r12 = q1 · a2 = (8 + 5 − 4)/3 = 3The component of a2 along q1.
- u2 = (4, 5, −2) − 3 · 13(2, 1, 2) = (2, 4, −4)Subtract that component. Check: (2, 4, −4) · (2, 1, 2) = 4 + 4 − 8 = 0.
- r22 = ‖u2‖ = 6, q2 = 13(1, 2, −2)√(4 + 16 + 16) = 6.
- Q = 1321122−2, R = 3306Check the first column of QR: 3q1 = (2, 1, 2) = a1. The second: 3q1 + 6q2 = (2, 1, 2) + (2, 4, −4) = (4, 5, −2) = a2.
Example 4: least squares with QR
Find the least-squares solution of Ax = b for the A of Example 3 and b = (1, 2, 7).
- QTb = (6, −3)q1 · b = (2 + 2 + 14)/3 = 6 and q2 · b = (1 + 4 − 14)/3 = −3: the coordinates of the projection of b in the basis q1, q2.
- 3306 x̂ = 6−3With A = QR, AᵀA = RᵀR and Aᵀb = RᵀQᵀb, so the normal equations AᵀAx̂ = Aᵀb become Rx̂ = Qᵀb once the invertible Rᵀ is canceled.
- x̂2 = −12, x̂1 = (6 − 3(−½))/3 = 52Back substitution, bottom row first, as with any triangular system.
- Ax̂ = (3, 0, 6)The best fit: (5/2)(2, 1, 2) − (1/2)(4, 5, −2). It is the projection of b onto the plane of the columns.
- b − Ax̂ = (−2, 2, 1), length 3The residual is perpendicular to both columns of A: (−2, 2, 1) · (2, 1, 2) = 0 and (−2, 2, 1) · (4, 5, −2) = 0. That is what makes x̂ the best fit.
Common mistakes, and the exact line they happen on
1. Projecting without dividing by a · a
The second step of Example 2.
- a1 · a2 = 5Right so far.
- u2 = (3, 1) − 5(1, 2) = (−2, −9)This is the mistake. (a · b)a is the projection only when a is a unit vector. ‖a1‖² = 5, so the coefficient is 5/5 = 1. The check catches it at once: (−2, −9) · (1, 2) = −20, not 0. Either divide by a · a, or normalize first and use q1.
2. R computed as AᵀQ
The decomposition from Example 3.
- Q = 1321122−2Right so far.
- R = ATQ = 3036This is the mistake. AᵀQ is the transpose of QᵀA, so it comes out lower triangular, which is already the warning. Multiplying back, its first column gives 3q1 + 3q2 = (3, 3, 0), not a1 = (2, 1, 2). Row i of R is qi dotted with each column of A: R = QᵀA.
3. Treating QQᵀ as the identity
The least-squares problem from Example 4.
- QTQ = IRight so far: the columns of Q are orthonormal.
- Ax̂ = QRx̂ = QQTb = b, so the fit is exactThis is the mistake. Q is 3×2, so it has no inverse, and QQᵀ is not I. It is the projection onto the plane spanned by the columns: QQᵀb = 6q1 − 3q2 = (3, 0, 6), not (1, 2, 7). Only a square Q has QQᵀ = I. A quick check: the trace of QQᵀ is 2, the dimension of the plane, while the trace of the 3×3 identity is 3.
Practice
Find the angle between (1, 0, 1) and (0, 1, 1).
Show answer
cos θ = 1√2 · √2 = 12, so θ = 60°.
Apply Gram–Schmidt to (1, 1, 0) and (1, 0, 1).
Show answer
q1 = (1, 1, 0)/√2. The coefficient is ½, so u2 = (1, 0, 1) − ½(1, 1, 0) = (½, −½, 1), and q2 = (1, −1, 2)/√6.
Find the QR decomposition of A = 023344.
Show answer
q1 = (0, 3, 4)/5, q1 · a2 = 25/5 = 5, u2 = (2, 3, 4) − (0, 3, 4) = (2, 0, 0), so q2 = (1, 0, 0), and R = 5502.
With q1 = (1, 1)/√2 and q2 = (1, −1)/√2, write (5, 1) as c1q1 + c2q2.
Show answer
c1 = 6/√2 = 3√2 and c2 = 4/√2 = 2√2.
For A = 1321, find R, and use det A = det Q · det R to get det A.
Show answer
From Example 2, R = √5√50√5, with det R = 5. Q = 1√5122−1 has det Q = −1, so det A = −5, which matches 1 − 6. R gives |det A|; the sign comes from Q.