Orthogonal bases, Gram–Schmidt and the QR decomposition
Ten problems on orthogonality worked by hand: dot products, lengths and angles, coordinates in an orthonormal basis by dot products, a Householder reflection and the properties of an orthogonal matrix, Gram–Schmidt in the plane and in three dimensions, the QR decomposition of a 3×2 and a 3×3 matrix with det A from R, least squares by solving Rx = Qᵀb, the projection QQᵀ and the orthogonal complement, a dependent column that Gram–Schmidt turns into zero, and why nearly collinear features break the normal equations but not QR, with worked solutions and the mistakes of skipping the division by a·a, projecting onto a column instead of q, transposing R and taking QQᵀ to be I.
Before you start
An orthonormal basis turns linear algebra into dot products. Coordinates come from a dot product instead of a linear system, a projection is a sum of dot products, and the inverse of a matrix with orthonormal columns is its transpose. Gram–Schmidt builds such a basis from any independent set of vectors by subtracting, from each new vector, its projections onto the directions already found, and the bookkeeping of that process is the factorization .A=QR. The mistakes are in the projections: a coefficient not divided by ,a⊤a, a projection onto an original column instead of a finished ,q, and a tall Q treated as if it were square. These ten problems build one orthonormal basis of R3 and use it for a reflection, a QR decomposition, a determinant and a least-squares fit, and end with the reason libraries solve least squares with QR rather than the normal equations.
Dot products.,a⊤b=a1b1+⋯+anbn,,∥a∥=a⊤a, and the angle θ between nonzero vectors has .cosθ=∥a∥∥b∥a⊤b.a and b are orthogonal when .a⊤b=0.
Orthonormal. Vectors q1,…,qk are orthonormal when qi⊤qj=0 for i=j and .∥qi∥=1. Stacked as the columns of ,Q, that says .Q⊤Q=Ik.
Orthogonal matrix. A square Q with .Q⊤Q=I. Then ,Q−1=Q⊤,QQ⊤=I as well, ∥Qx∥=∥x∥ for every ,x, and .detQ=±1. A tall Q (n×k with )k<n) still has ,Q⊤Q=Ik, but QQ⊤ is not .In.
Projection onto a line. The projection of b onto the line through a=0 is .a⊤aa⊤ba. For a unit vector q it is .(q⊤b)q.
Gram–Schmidt.;u1=a1; then uj=aj−∑i<j(qi⊤aj)qi and .qj=uj/∥uj∥. Each uj is aj minus its projections onto the q's already built. If some ,uj=0, then aj is a combination of the earlier columns.
QR. For A with independent columns, ,A=QR, with Q the Gram–Schmidt vectors as columns and R=Q⊤A upper triangular: rij=qi⊤aj for i<j and .rjj=∥uj∥>0.
Least squares. The x^ that minimizes ∥Ax−b∥ solves ;A⊤Ax^=A⊤b; with A=QR this becomes ,Rx^=Q⊤b, and the projection of b onto col(A) is .QQ⊤b.
Condition number.,κ(A)=σmax/σmin, the ratio of the largest and smallest singular values. Roughly, solving a system with condition number κ in double precision loses log10κ of its 16 significant digits.
Problems
·
For ,a=(1,2,2)⊤,b=(3,0,4)⊤ and ,c=(2,1,−2)⊤, find ,a⊤b,,∥a∥,∥b∥ and the angle between a and ,b, and show that a and c are orthogonal.
·
q1=51(3,4)⊤ and q2=51(−4,3)⊤ form an orthonormal basis of .R2. Write x=(2,11)⊤ as c1q1+c2q2 without solving a system.
··
Let x=(1,2,2)⊤ and .v=x−3e1=(−2,2,2)⊤. Compute the Householder reflection ,H=I−v⊤v2vv⊤, and show that ,Hx=3e1,,H⊤H=I,H−1=H and .detH=−1.
··
Apply Gram–Schmidt to a1=(3,4)⊤ and .a2=(2,11)⊤.
··
Find the QR decomposition of .A=122452.
···
Extend Problem 5 with a third column: find the QR decomposition of ,A=122452511, and use it to find .detA.
··
Use the QR decomposition of Problem 5 to find the least-squares solution of Ax=b for A=122452 and ,b=(3,3,6)⊤, and the residual.
··
For the tall Q of Problem 5, find P=QQ⊤ and show it is the projection onto the plane spanned by the columns of .A. Find the orthogonal complement of that plane, and show QQ⊤=I although .Q⊤Q=I.
··
Run Gram–Schmidt on ,a1=(1,2,2)⊤,,a2=(4,5,2)⊤,.a3=(6,9,6)⊤. What happens at the third vector, and what does it say about ?A=[a1a2a3]?
···
Two features are almost collinear:
A=1ε010ε,ε=10−8.
Show that κ(A⊤A)=κ(A)2 in general, find both for this ,A, and explain why forming A⊤A in double precision fails while QR on A does not.
,x^=(3,−31)⊤,,Ax^=31(5,13,16)⊤, residual r=32(2,−2,1)⊤ with ∥r∥=2
;P=9154−2452−228; complement spanned by ;(2,−2,1)⊤;QQ⊤ has trace ,2, so it is not I3
:u3=0:,a3=2a1+a2, the columns are dependent, A has rank 2 and detA=0
,κ(A)≈1.41×108,:κ(A⊤A)≈2×1016: the normal equations lose every digit, while QR works on A itself, with condition number κ(R)=κ(A)
Worked solutions
Problem 1
For ,a=(1,2,2)⊤,b=(3,0,4)⊤ and ,c=(2,1,−2)⊤, find ,a⊤b,,∥a∥,∥b∥ and the angle between a and ,b, and show that a and c are orthogonal.
a⊤b=1⋅3+2⋅0+2⋅4=11Multiply matching entries and add.
∥a∥=1+4+4=3 and ∥b∥=9+0+16=5The length is the square root of a vector's dot product with itself.
cosθ=3⋅511=1511Divide the dot product by both lengths.
a⊤c=2+2−4=0A zero dot product is orthogonality.
,a⊤b=11,,∥a∥=3,,∥b∥=5,;θ=arccos1511≈42.8∘;a⊤c=0The cosine is between −1 and ,1, as Cauchy–Schwarz, ,∣a⊤b∣≤∥a∥∥b∥, guarantees.
Problem 2
q1=51(3,4)⊤ and q2=51(−4,3)⊤ form an orthonormal basis of .R2. Write x=(2,11)⊤ as c1q1+c2q2 without solving a system.
,q1⊤q1=259+16=1,,q2⊤q2=1,q1⊤q2=25−12+12=0Check the basis is orthonormal before relying on it.
q1⊤x=c1q1⊤q1+c2q1⊤q2=c1Dot both sides of x=c1q1+c2q2 with :q1: the cross term is 0 and .q1⊤q1=1.
c1=q1⊤x=56+44=10The coordinate is the dot product.
c2=q2⊤x=5−8+33=5The same with .q2.
x=10q1+5q2Check: .10q1+5q2=(6,8)⊤+(−4,3)⊤=(2,11)⊤. In a basis that is not orthogonal, the coordinates need a 2×2 solve; here they are two dot products.
Problem 3
Let x=(1,2,2)⊤ and .v=x−3e1=(−2,2,2)⊤. Compute the Householder reflection ,H=I−v⊤v2vv⊤, and show that ,Hx=3e1,,H⊤H=I,H−1=H and .detH=−1.
v⊤v=4+4+4=12 and vv⊤=4−4−4−444−444Entry (i,j) of vv⊤ is .vivj.
H=I−61vv⊤=3112221−22−211−64=31 on the diagonal, 64=32 off it, with the signs of .−vv⊤.
Hx=31(1+4+4,2+2−4,2−4+2)⊤=(3,0,0)⊤,∥x∥=3, so x is sent to the same length along :e1: this is how QR algorithms zero out a column.
The columns ,31(1,2,2),,31(2,1,−2),31(2,−2,1) have length 1 and pairwise dot products 91(2+2−4)=91(2−4+2)=91(4−2−2)=0So .H⊤H=I.H is also symmetric, so :H−1=H⊤=H: reflecting twice puts every vector back.
,H=3112221−22−21,,Hx=3e1,,H⊤H=I,,H−1=H,detH=−1Hv=v−2v=−v and Hw=w for every ,w⊥v, so the eigenvalues are −1,1,1 and :detH=−1: a reflection, not a rotation. Lengths are kept: ,H(1,1,1)⊤=31(5,1,1)⊤, and both have length .3.
Problem 4
Apply Gram–Schmidt to a1=(3,4)⊤ and .a2=(2,11)⊤.
,∥a1∥=5, so q1=51(3,4)⊤The first vector only needs normalizing.
q1⊤a2=56+44=10The length of a2 along .q1.
u2=a2−10q1=(2,11)⊤−(6,8)⊤=(−4,3)⊤Remove that component. Check: .a1⊤u2=−12+12=0.
,∥u2∥=5, so q2=51(−4,3)⊤Normalize what is left.
,q1=51(3,4)⊤,q2=51(−4,3)⊤The basis of Problem 2, and its coordinates of a2 there, 10 and ,5, are exactly the q1⊤a2 and ∥u2∥ found here: .R=[50105].
Problem 5
Find the QR decomposition of .A=122452.
r11=∥a1∥=1+4+4=3 and q1=31(1,2,2)⊤The first column of Q is the first column of ,A, normalized.
r12=q1⊤a2=34+10+4=6The entry above the diagonal in column .2.
r22=∥u2∥=3 and q2=31(2,1,−2)⊤The diagonal entries of R are the lengths of the u's, so they are positive.
,Q=3112221−2,R=[3063]Check :QR: column 1 is 3q1=(1,2,2)⊤ and column 2 is .6q1+3q2=(2,4,4)⊤+(2,1,−2)⊤=(4,5,2)⊤. And ,R=Q⊤A, as it must.
Problem 6
Extend Problem 5 with a third column: find the QR decomposition of ,A=122452511, and use it to find .detA.
,q1=31(1,2,2)⊤,,q2=31(2,1,−2)⊤,,r11=3,,r12=6,r22=3The first two columns are Problem 5 unchanged: Gram–Schmidt never revisits earlier columns.
r13=q1⊤a3=35+2+2=3 and r23=q2⊤a3=310+1−2=3Project a3=(5,1,1)⊤ onto each finished .q.
u3=a3−3q1−3q2=(5,1,1)⊤−(1,2,2)⊤−(2,1,−2)⊤=(2,−2,1)⊤Check: q1⊤u3=31(2−4+2)=0 and .q2⊤u3=31(4−2−2)=0.
,r33=3,,q3=31(2,−2,1)⊤, so Q=3112221−22−21The same matrix as the reflection H of Problem 3, so .detQ=−1.
R=300630333 and detA=detQdetR=(−1)(27)=−27R is triangular, so its determinant is .3⋅3⋅3. The sign comes from :Q:∣detA∣ is always the product of the ,rjj, the volume of the box the columns span.
Problem 7
Use the QR decomposition of Problem 5 to find the least-squares solution of Ax=b for A=122452 and ,b=(3,3,6)⊤, and the residual.
A⊤Ax^=A⊤b becomes ,R⊤Q⊤QRx^=R⊤Q⊤b, so Rx^=Q⊤b,Q⊤Q=I, and R⊤ is invertible because R's diagonal is positive.
Q⊤b=(33+6+12,36+3−12)⊤=(7,−1)⊤One dot product with each .q.
3x^2=−1 and 3x^1+6x^2=7R is upper triangular: back substitution, no elimination needed.
x^2=−31 and x^1=37+2=3From the bottom row up.
,x^=(3,−31)⊤,,Ax^=31(5,13,16)⊤, residual r=32(2,−2,1)⊤ with ∥r∥=2.Ax^=7q1−q2. The residual is a multiple of q3 from Problem 6, orthogonal to both columns: a1⊤r=32(2−4+2)=0 and .a2⊤r=32(8−10+2)=0.
Problem 8
For the tall Q of Problem 5, find P=QQ⊤ and show it is the projection onto the plane spanned by the columns of .A. Find the orthogonal complement of that plane, and show QQ⊤=I although .Q⊤Q=I.
Q⊤Q=91[9009]=I2The columns are orthonormal.
P=q1q1⊤+q2q2⊤=9154−2452−228QQ⊤ is the sum of the projections onto each ;q; for example, its top-left entry is .91+4.
P2=Q(Q⊤Q)Q⊤=QQ⊤=P and ,Pa1=a1,Pa2=a2Projecting twice is projecting once, and vectors in the plane are left alone.
The complement is spanned by :(2,−2,1)⊤: it is orthogonal to (1,2,2) and ,(4,5,2), and P(2,−2,1)⊤=0Gram–Schmidt on a third vector gave it in Problem 6. So ,I−P=q3q3⊤, the projection onto the complement.
;P=9154−2452−228; complement spanned by ;(2,−2,1)⊤;QQ⊤ has trace ,2, so it is not I3The trace of a projection is the dimension it projects onto: ,95+5+8=2, not .3. Only a square Q has .QQ⊤=I.
Problem 9
Run Gram–Schmidt on ,a1=(1,2,2)⊤,,a2=(4,5,2)⊤,.a3=(6,9,6)⊤. What happens at the third vector, and what does it say about ?A=[a1a2a3]?
q1=31(1,2,2)⊤ and q2=31(2,1,−2)⊤As in Problem 5.
q1⊤a3=36+18+12=12 and q2⊤a3=312+9−12=3The two projection coefficients.
u3=(6,9,6)⊤−(4,8,8)⊤−(2,1,−2)⊤=(0,0,0)⊤Nothing is left after removing the components along q1 and :q2:a3 lies in their plane.
a3=12q1+3q2=4a1+(a2−2a1)=2a1+a2Rewrite the q's in terms of the a's with Problem 5: 3q1=a1 and .3q2=a2−2a1. Check: .2(1,2,2)+(4,5,2)=(6,9,6).
:u3=0:,a3=2a1+a2, the columns are dependent, A has rank 2 and detA=0There is no q3 to make, and r33 would be .0. In a data matrix this is a feature that is a combination of two others.
Problem 10
Two features are almost collinear:
A=1ε010ε,ε=10−8.
Show that κ(A⊤A)=κ(A)2 in general, find both for this ,A, and explain why forming A⊤A in double precision fails while QR on A does not.
A=UΣV⊤ gives ,A⊤A=VΣ2V⊤, so the singular values of A⊤A are σi2U⊤U=I cancels in the middle.
κ(A⊤A)=σmax2/σmin2=κ(A)2Forming A⊤A squares the condition number before any solving starts.
A⊤A=[1+ε2111+ε2] with eigenvalues 2+ε2 and ε2Eigenvectors (1,1)⊤ and .(1,−1)⊤. So σ1(A)=2+ε2 and .σ2(A)=ε.
κ(A)=2+ε2/ε≈1.41×108 and κ(A⊤A)≈2×10161+ε2=1+10−16 rounds to 1 in double precision (the spacing of doubles near 1 is about ),2.2×10−16), so the computed A⊤A is exactly ,[1111], which is singular.
,κ(A)≈1.41×108,:κ(A⊤A)≈2×1016: the normal equations lose every digit, while QR works on A itself, with condition number κ(R)=κ(A)For ,b=A(2,1)⊤, QR returns (2,1) to at least eight digits, and r22≈2ε is small but not lost. This is why least-squares solvers use QR or the SVD, and why nearly collinear features make the normal equations unreliable long before they are exactly singular.
Where this goes wrong
1. Projection coefficient not divided by a·a
The projection onto a1 is remembered as "dot product times a1", and a1 is not a unit vector.
,a1=(3,4)⊤,,a2=(2,11)⊤,a1⊤a2=50Right so far: Problem 4.
“Subtract the dot product times .a1.”The step that causes the mistake: (a1⊤a2)a1 is the projection only when .∥a1∥=1.
u2=(2,11)⊤−50(3,4)⊤=(−148,−189)⊤Check: ,a1⊤u2=−444−756=−1200, not .0. The coefficient is ,a1⊤a1a1⊤a2=2550=2, giving .u2=(2,11)⊤−(6,8)⊤=(−4,3)⊤. Dividing by ∥a1∥=5 instead of ∥a1∥2 fails too: ,(2,11)⊤−10(3,4)⊤=(−28,−29)⊤, with .a1⊤u2=−200.
2. Projecting onto a column of A instead of q
The third step subtracts projections onto the earlier vectors, and the earlier columns of A are the nearest to hand.
,a1=(1,2,2)⊤,,a2=(4,5,2)⊤,a3=(5,1,1)⊤Right so far: Problem 6.
“Subtract the projections of a3 onto a1 and onto .a2.”The step that causes the mistake: a1 and a2 are not orthogonal (),a1⊤a2=18), so their projections overlap.
u3=a3−99a1−4527a2=(58,−4,−511)⊤Check: ,a1⊤u3=58−8−522=−554, not .0. The part of a3 along the shared direction is removed twice. Project onto q1 and ,q2, which are orthogonal: .u3=a3−3q1−3q2=(2,−2,1)⊤.
3. R computed as AᵀQ
R is built from dot products of q's with a's, and the order of the transpose is easy to swap.
Q=3112221−2 for A=122452Right so far: Problem 5.
“R holds the dot products of the columns, so .R=A⊤Q.”The step that causes the mistake: A⊤Q=(Q⊤A)⊤ is R transposed.
R=[3603]It is lower triangular, which is already the warning. Multiplying back, the first column of QR is ,3q1+6q2=(5,4,−2)⊤, not .a1=(1,2,2)⊤. The right order is :R=Q⊤A=[3063]: row i is qi dotted with each column of .A.
4. QQᵀ taken to be I for a tall Q
,Q⊤Q=I, and the identity is expected to work in the other order too.
Q⊤Q=I2 for the 3×2 matrix Q of Problem 5Right so far.
“So ,Q−1=Q⊤, and .QQ⊤=I.”The step that causes the mistake: Q is not square, so it has no inverse, and Q⊤ is only a left inverse.
,Ax^=QQ⊤b=b, so the least-squares fit of Problem 7 is exact and the residual is 0,QQ⊤b=7q1−q2=31(5,13,16)⊤, not :b=(3,3,6)⊤:QQ⊤ projects onto a plane, with trace .2. The residual is ,32(2,−2,1)⊤, of length .2.
5. Normalizing a zero vector
Every Gram–Schmidt step ends with a division by a length, and it is done without looking.
u3=a3−12q1−3q2 for a3=(6,9,6)⊤Right so far: Problem 9.
With measured data or inexact arithmetic, a dependent column leaves a u3 of size around 10−15 rather than exactly 0Rounding leaves crumbs; these small integers happen to cancel exactly.
q3=u3/∥u3∥ is a unit vector, so the columns are independent and A has rank 3The direction of u3 is rounding noise, and dividing by its tiny length blows the noise up to length .1. Compare ∥u3∥ with ∥a3∥=153≈12.4 before normalizing: a ratio near machine precision means a3 is a combination of the earlier columns, here ,a3=2a1+a2, and the rank is .2.
Print this set: gram-schmidt-and-qr.pdf (problems, answers, and worked solutions on separate pages).