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
>u1โw2โw3โโ=โฅ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โ=(10โ3โ,10โ1โ).Step 2: โจv2โ,u1โโฉ=10โ6+2โ=10โ8โ.w2โ=(2,2)โ10โ8โ(10โ3โ,10โ1โ)=(2,2)โ108โ(3,1)=(2,2)โ(2.4,0.8)=(โ0.4,1.2).โฅw2โโฅ=0.16+1.44โ=1.6โ=10โ4โ.u2โ=4/10โ(โ0.4,1.2)โ=(โ10โ1โ,10โ3โ).Check: โจu1โ,u2โโฉ=10โ3โ(โ10โ1โ)+10โ1โ(10โ3โ)=โ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=[31โ22โ]>Example 3: QR of a 2ร2 matrix
>Q=[10โ3โ10โ1โโโ10โ1โ10โ3โโ]>.From Example 1: q1โ=(10โ3โ,10โ1โ), q2โ=(โ10โ1โ,10โ3โ).So
>R=[10โ0โ10โ8โ10โ4โโ]>.R: r11โ=โฅv1โโฅ=10โ. r12โ=โจv2โ,q1โโฉ=10โ8โ. r22โ=โฅw2โโฅ=10โ4โ.
>QR=[10โ3โ10โ1โโโ10โ1โ10โ3โโ][10โ0โ10โ8โ10โ4โโ]=[31โ1024โโ104โ108โ+1012โโ]=[31โ22โ]=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=โ101โ011โโ>Q1: Gram-Schmidt in โยฒOrthonormalise {(1,2),(3,4)}.Solution: u1โ=5โ(1,2)โ=(5โ1โ,5โ2โ).โจ(3,4),u1โโฉ=5โ3+8โ=5โ11โ. w2โ=(3,4)โ5โ11โ(5โ1โ,5โ2โ)=(3,4)โ511โ(1,2)=(515โ11โ,520โ22โ)=(54โ,โ52โ).โฅw2โโฅ=2516+4โโ=2520โโ=5โ2โ. u2โ=2/5โ(4/5,โ2/5)โ=(5โ2โ,โ5โ1โ). Q2: Gram-Schmidt in โยณOrthonormalise {(1,0,1),(1,1,0),(0,1,1)}.Solution: u1โ=2โ(1,0,1)โ=(2โ1โ,0,2โ1โ).โจ(1,1,0),u1โโฉ=2โ1+0+0โ=2โ1โ. 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)โ=(6โ1โ,6โ2โ,โ6โ1โ).โจ(0,1,1),u1โโฉ=2โ0+0+1โ=2โ1โ. โจ(0,1,1),u2โโฉ=6โ0+2โ1โ=6โ1โ.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โโ=3โ2โ. u3โ=(โ3โ1โ,3โ1โ,3โ1โ). Q3: QR decompositionFind the QR decomposition of
>Q=โ2โ1โ02โ1โโโ6โ1โ6โ2โ6โ1โโโ>.Solution: v1โ=(1,0,1), u1โ=2โ(1,0,1)โ. v2โ=(0,1,1), โจv2โ,u1โโฉ=2โ0+0+1โ=2โ1โ. w2โ=(0,1,1)โ21โ(1,0,1)=(โ21โ,1,21โ). โฅw2โโฅ=41โ+1+41โโ=23โโ. u2โ=(โ6โ1โ,6โ2โ,6โ1โ).
>R=[2โ0โ2โ1โ23โโโ]>,
>A=[31โ22โ]>. 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โโฉ=5โ2+8โ=5โ10โ. 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โฉ=โซ01โp(x)q(x)dx.Solution: โฅ1โฅ2=โซ01โ1dx=1, so u1โ=1.โจx,1โฉ=โซ01โxdx=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โโฅ=23โ1โ.u2โ=1/(23โ)xโ1/2โ=23โ(xโ1/2)=23โxโ3โ.Orthonormal basis: {1,23โxโ3โ}. Q6: QR for solving Ax=bUsing QR, solve Ax=b where
>b=[53โ]>,
>Q=[3/10โ1/10โโโ1/10โ3/10โโ]>.Solution: From Example 3, A=QR with
>R=[10โ0โ8/10โ4/10โโ]>,
>QTb=[3/10โโ1/10โโ1/10โ3/10โโ][53โ]=[10โ15+3โ10โโ5+9โโ]=[10โ18โ10โ4โโ]>.
>QTb=[1/2โโ1/6โโ02/6โโ1/2โ1/6โโ]โ123โโ=[2โ1+3โ6โโ1+4+3โโ]=[2โ4โ6โ6โโ]=[22โ6โโ]>.Solve Rx=QTb: 10โx1โ+10โ8โx2โ=10โ18โ 10โ4โx2โ=10โ4โโx2โ=1 10โx1โ+10โ8โ=10โ18โโ10โx1โ=10โ10โโ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โโฉ=2โ0+0+1โ=2โ1โ. w2โ=(0,1,โ1)โ21โ(1,0,โ1)=(โ21โ,1,โ21โ). โฅw2โโฅ=41โ+1+41โโ=23โโ. u2โ=(โ6โ1โ,6โ2โ,โ6โ1โ). 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=22โโ1/2โ01/2โโโ+6โโโ1/6โ2/6โ1/6โโโ=โ202โโ+โโ121โโ=โ123โโ>.
>A=โ101โ011โโ>.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โฉ=โซโ11โf(x)g(x)dx.Solution: โฅ1โฅ2=โซโ11โ1dx=2, u1โ=2โ1โ.โจx,1โฉ=โซโ11โxdx=0 (odd). w2โ=xโ0=x. โฅxโฅ2=โซโ11โx2dx=32โ, u2โ=2/3โxโ=23โโx.โจx2,1โฉ=โซโ11โx2dx=32โ. โจx2,xโฉ=โซโ11โx3dx=0. w3โ=x2โ22/3โโ 1โ0=x2โ31โ. โฅx2โ31โโฅ2=โซโ11โ(x4โ32โx2+91โ)dx=52โโ94โ+92โ=52โโ92โ=4518โ10โ=458โ. u3โ=8/45โx2โ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