Gram-Schmidt Process & QR Decomposition
2164 words
11 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Gram-Schmidt Process & QR Decomposition ## 🎯 Learning Objectives After this topic you will be able to: - Apply the Gram-Schmidt process to convert any basis into an orthonormal basis - Compute the $QR$ decomposition of a matrix - Use Gram-Schmidt to find orthogonal bases for subspaces - Understand the geometric i...

Gram-Schmidt Process & QR Decomposition
🎯 Learning Objectives
After this topic you will be able to:
- Apply the Gram-Schmidt process to convert any basis into an orthonormal basis
- Compute the QR decomposition of a matrix
- Use Gram-Schmidt to find orthogonal bases for subspaces
- Understand the geometric intuition of subtracting projections
📋 Prerequisites
- Orthogonality (this week) — orthonormal bases, projections
- Inner Products (Week 7) — needed for projections
- Gram-Schmidt is the algorithm that makes orthonormal bases computable
1. Intuition: Building Orthogonality Step by Step
Given any basis {v1,…,vn}, Gram-Schmidt produces an orthonormal basis {u1,…,un} for the same space.
Idea: For each vector vk, subtract its projections onto all previous ui to get an orthogonal vector, then normalise.
(Diagram)
2. The Gram-Schmidt Algorithm
>u1w2w3=∥v1∥v1=v2−⟨v2,u1⟩u1,u2=∥w2∥w2=v3−⟨v3,u1⟩u1−⟨v3,u2⟩u2,u3=∥w3∥w3>Gram-Schmidt Process. Given a basis {v1,…,vn}, define:
Continue: wk=vk−∑i=1k−1⟨vk,ui⟩ui, then uk=∥wk∥wk. Example 1: Gram-Schmidt in ℝ²Basis: v1=(3,1), v2=(2,2).Step 1: ∥v1∥=9+1=10, u1=(103,101).Step 2: ⟨v2,u1⟩=106+2=108.w2=(2,2)−108(103,101)=(2,2)−108(3,1)=(2,2)−(2.4,0.8)=(−0.4,1.2).∥w2∥=0.16+1.44=1.6=104.u2=4/10(−0.4,1.2)=(−101,103).Check: ⟨u1,u2⟩=103(−101)+101(103)=−103+103=0 ✓ Example 2: Gram-Schmidt in ℝ³Basis: v1=(1,2,2), v2=(−1,0,2), v3=(0,0,1).Step 1: ∥v1∥=1+4+4=3, u1=(31,32,32).Step 2: ⟨v2,u1⟩=3−1+0+4=33=1.w2=(−1,0,2)−1(31,32,32)=(−34,−32,34).∥w2∥=916+4+16=936=2.u2=(−32,−31,32).Step 3: ⟨v3,u1⟩=30+0+2=32. ⟨v3,u2⟩=0+0+32=32.w3=(0,0,1)−32(31,32,32)−32(−32,−31,32)=(0,0,1)−(92,94,94)−(−94,−92,94)=(0−92+94,0−94+92,1−94−94)=(92,−92,91).∥w3∥=814+4+1=819=31.u3=(32,−32,31).Orthonormal basis: {(31,32,32),(−32,−31,32),(32,−32,31)}.
3. QR Decomposition
>A=QR>Definition (QR Decomposition). For an m×n matrix A with linearly independent columns, the QR decomposition is:
where Q is m×n with orthonormal columns and R is n×n upper triangular. How to get R: When Gram-Schmidt produces qj from aj, the entries of R are:
- rii=∥wi∥ (norm before normalisation)
- rji=⟨ai,qj⟩ for j<i
- rji=0 for j>i
>A=[3122]>Example 3: QR of a 2×2 matrix
>Q=[103101−101103]>.From Example 1: q1=(103,101), q2=(−101,103).So
>R=[100108104]>.R: r11=∥v1∥=10. r12=⟨v2,q1⟩=108. r22=∥w2∥=104.
>QR=[103101−101103][100108104]=[311024−104108+1012]=[3122]=A>.Check:
✓
4. Modified Gram-Schmidt (Numerical Stability)
For computer implementation, the modified Gram-Schmidt is preferred:
- For each k, subtract projections one at a time
- This reduces rounding errors But the classical Gram-Schmidt (described above) is what you'll use on exams.
5. Edge Cases & Gotchas
| Situation | What Happens |
|---|---|
| Vector is already orthogonal | wk=vk (no subtraction needed) |
| ** $\ | \mathbf{w}_k\ |
| Starting basis not ordered | Different orders produce different orthonormal bases |
6. Common Pitfalls
❌ Pitfall 1: Arithmetic errors in subtraction
Keep fractions or use exact arithmetic. Rounding early leads to wrong results.
❌ Pitfall 2: Forgetting to subtract ALL previous projections
For w3, you must subtract projections onto u1 and u2, not just u2.
❌ Pitfall 3: Normalising too early
First compute the orthogonal wk, then normalise to get uk.
7. Formula Summary Table
| Step | Formula |
|---|---|
| First orthonormal vector | $\mathbf{u}_1 = \mathbf{v}_1 / \ |
| Orthogonal component | wk=vk−∑i=1k−1⟨vk,ui⟩ui |
| Normalise | $\mathbf{u}_k = \mathbf{w}_k / \ |
| **QR: Q ** | Columns are ui |
| **QR: R ** | $r_{ii} = \ |
8. 📝 Practice Questions
>A=101011>Q1: Gram-Schmidt in ℝ²Orthonormalise {(1,2),(3,4)}.Solution: u1=5(1,2)=(51,52).⟨(3,4),u1⟩=53+8=511. w2=(3,4)−511(51,52)=(3,4)−511(1,2)=(515−11,520−22)=(54,−52).∥w2∥=2516+4=2520=52. u2=2/5(4/5,−2/5)=(52,−51). Q2: Gram-Schmidt in ℝ³Orthonormalise {(1,0,1),(1,1,0),(0,1,1)}.Solution: u1=2(1,0,1)=(21,0,21).⟨(1,1,0),u1⟩=21+0+0=21. w2=(1,1,0)−21(1,0,1)=(21,1,−21). ∥w2∥=41+1+41=23=26. u2=6/2(1/2,1,−1/2)=(61,62,−61).⟨(0,1,1),u1⟩=20+0+1=21. ⟨(0,1,1),u2⟩=60+2−1=61.w3=(0,1,1)−21(1,0,1)−61(1,2,−1). =(0,1,1)−(21,0,21)−(61,31,−61). =(0−21−61,1−0−31,1−21+61)=(−32,32,32).∥w3∥=94+4+4=912=32. u3=(−31,31,31). Q3: QR decompositionFind the QR decomposition of
>Q=21021−616261>.Solution: v1=(1,0,1), u1=2(1,0,1). v2=(0,1,1), ⟨v2,u1⟩=20+0+1=21. w2=(0,1,1)−21(1,0,1)=(−21,1,21). ∥w2∥=41+1+41=23. u2=(−61,62,61).
>R=[202123]>,
>A=[3122]>. Q4: Gram-Schmidt on dependent setWhat happens if you apply Gram-Schmidt to {(1,2),(2,4)}?Solution: u1=5(1,2). ⟨(2,4),u1⟩=52+8=510. w2=(2,4)−510(1,2)=(2,4)−(2,4)=(0,0). ∥w2∥=0 — the algorithm fails because the set is dependent. Q5: Gram-Schmidt with different inner productOrthonormalise {1,x} in P1 with ⟨p,q⟩=∫01p(x)q(x)dx.Solution: ∥1∥2=∫011dx=1, so u1=1.⟨x,1⟩=∫01xdx=21. w2=x−21⋅1=x−21. ∥x−21∥2=∫01(x−21)2dx=∫01(x2−x+41)dx=31−21+41=124−6+3=121. ∥x−21∥=231.u2=1/(23)x−1/2=23(x−1/2)=23x−3.Orthonormal basis: {1,23x−3}. Q6: QR for solving Ax=bUsing QR, solve Ax=b where
>b=[53]>,
>Q=[3/101/10−1/103/10]>.Solution: From Example 3, A=QR with
>R=[1008/104/10]>,
>QTb=[3/10−1/101/103/10][53]=[1015+310−5+9]=[1018104]>.
>QTb=[1/2−1/602/61/21/6]123=[21+36−1+4+3]=[2466]=[226]>.Solve Rx=QTb: 10x1+108x2=1018 104x2=104⇒x2=1 10x1+108=1018⇒10x1=1010⇒x1=1. Solution: x=(1,1). Q7: Orthogonal basis for a planeFind an orthonormal basis for W={(x,y,z)∣x+y+z=0}.Solution: Find any basis. Let x=1,y=0⇒z=−1: v1=(1,0,−1). Let x=0,y=1⇒z=−1: v2=(0,1,−1).Apply Gram-Schmidt: u1=2(1,0,−1).⟨(0,1,−1),u1⟩=20+0+1=21. w2=(0,1,−1)−21(1,0,−1)=(−21,1,−21). ∥w2∥=41+1+41=23. u2=(−61,62,−61). Q8: Projection using QRProject b=(1,2,3) onto the column space of A from Q3 using QR.Solution: From Q3, proj=QQTb.
>proj=Q(22,6)T=221/201/2+6−1/62/61/6=202+−121=123>.
>A=101011>.Interesting — b is already in C(A)! Check: A(1,1)=(1,0,1)+(0,1,1)=(1,1,2). Wait, that doesn't give (1,2,3). Let me recheck...Actually (1,2,3) is in C(A):
. Solve Ax=(1,2,3): x=1, y=2, 1+2=3 ✓. So b is indeed in C(A) and projection = itself. Q9: Orthogonal polynomialsApply Gram-Schmidt to {1,x,x2} on [−1,1] with ⟨f,g⟩=∫−11f(x)g(x)dx.Solution: ∥1∥2=∫−111dx=2, u1=21.⟨x,1⟩=∫−11xdx=0 (odd). w2=x−0=x. ∥x∥2=∫−11x2dx=32, u2=2/3x=23x.⟨x2,1⟩=∫−11x2dx=32. ⟨x2,x⟩=∫−11x3dx=0. w3=x2−22/3⋅1−0=x2−31. ∥x2−31∥2=∫−11(x4−32x2+91)dx=52−94+92=52−92=4518−10=458. u3=8/45x2−1/3=845(x2−31).These are (up to scaling) the Legendre polynomials: P0=1, P1=x, P2=21(3x2−1). Q10: QR and least squaresWhy is QR useful for least squares?Solution: The least squares solution to Ax≈b (when A is full column rank) solves ATAx=ATb. With A=QR where Q has orthonormal columns: ATA=RTQTQR=RTR (since QTQ=I). ATb=RTQTb. So RTRx=RTQTb⇒Rx=QTb. This triangular system is easy to solve by back substitution — no need to form ATA!
🔗 Cross-References
- Next topic: Orthogonal Transformations
- Week 12 (Eigenvalues): QR algorithm for computing eigenvalues
- BSCS2004 (ML Foundations): QR for linear regression, numerical stability Join Discord Previous8.1 OrthogonalityNext8.3 Orthogonal Transformations