The chapter on orthogonality closes with orthonormal bases and the matrices Q that hold them. When the columns are orthonormal, Q transpose Q is the identity, projection matrices collapse to Q Q transpose, and the normal equations solve themselves. Gram-Schmidt is the recipe that turns any independent columns into orthonormal ones, and the matrix record of that process is the factorization A = QR.
This is the last lecture in the chapter on orthogonality — the chapter that met orthogonal vectors, then orthogonal subspaces like the row space and null space. Today it meets an orthogonal basis and an orthogonal matrix. In Strang’s words, “this chapter cleans up orthogonality.”
The right word is really orthonormal. Strang writes his vectors as q1,q2,…,qn — the letter q is a standing reminder that these are not just any vectors but orthogonal ones.
It’s a natural idea — a basis headed off at ninety-degree angles, all inner products zero. And it’s the basis numerical linear algebra is built around: computations with orthonormal vectors “never get out of hand, they never overflow or underflow.” The lecture has two halves. First: how does having an orthonormal basis make everything nice? Second: if your columns are not orthonormal, how do you make them so? The two names attached to that second, simple idea are Gram and Schmidt.
The matrix Q, and why Q transpose Q equals I
Put the orthonormal vectors into the columns of a matrix Q: first column q1, last column qn. The whole chapter has been looking at ATA, so it’s natural to look at QTQ. The beauty is it comes out perfectly. The rows of QT are q1T,…,qnT, and multiplying rows times columns just asks for all the dot products: first row times first column is 1; first row times second column is 0 — that’s the orthogonality. Ones on the diagonal, zeroes everywhere else:
QTQ=q1T⋮qnT[q1⋯qn]=I
Note that Q does not have to be square here — two orthonormal columns with four components each work fine.
A gallery of examples
Permutations. Any permutation matrix qualifies, say
Q=010001100
Unit vectors in the columns, certainly perpendicular to each other. Its transpose is another orthogonal matrix, and QTQ=I. Easy examples — getting orthogonal columns by putting ones in different places is “like too easy.”
Rotations. Take a unit vector (cosθ,sinθ); to make the inner product with the second column zero, put a minus sign in:
Q=[cosθsinθ−sinθcosθ]
Fixing up lengths. Is [111−1] an orthogonal matrix? Orthogonal columns, yes — but the length squared of each column is 2, so divide by 2:
Q=21[111−1]
That’s the rotation matrix at (about) θ=π/4.
Hadamard. Repeat that pattern in blocks — the block again, and minus it in the corner:
Q=2111111−11−111−1−11−1−11
One of the world’s favorite orthogonal matrices: every entry 1 or −1, every pair of columns balancing pluses against minuses, and the length of (1,1,1,1) is 4=2, so divide by 2. This construction is named after Hadamard, and it works for sizes 2,4,16,64,… — but nobody knows exactly which sizes allow an orthogonal matrix of ones and minus ones. Some sizes are known, some are impossible (no 5×5), and for some sizes it’s still an open question.
A rectangular Q. The columns 31(1,2,2) and 31(−2,−1,2) are orthonormal (each length: 1+4+4=3, hence the division). Two orthonormal columns in R3: an orthonormal basis for the two-dimensional plane they span. And orthonormal vectors are automatically independent — headed off at ninety degrees, no combination can give zero. To extend to a third column you could run Gram-Schmidt on any independent third vector, or be inspired: the pattern 31(2,−2,1) fixes up the signs. That’s about the last example without square roots — the punishing thing in Gram-Schmidt, “maybe we better know that in advance,” is that making unit vectors means dividing by lengths, and lengths are square roots.
What a Q is good for: projections without inversion
Suppose Q has orthonormal columns and I want to project onto its column space. The big projection formula from this chapter had four A‘s in a row; with Q‘s it reads P=Q(QTQ)−1QT. But QTQ is the identity — no inversion to do:
All the messy equations of this chapter become trivial. The most important one was the normal equation ATAx^=ATb. With A=Q it becomes QTQx^=QTb, and the matrix on the left is the identity. Normally it’s a matrix of inner products — “you’ve got to compute all those dopey inner products and solve the system.” Here it’s gone:
x^=QTb,component by component:x^i=qiTb
Gram-Schmidt: making vectors orthonormal
Now the second half. We start with merely independent vectors a and b — maybe in two dimensions, maybe in twelve — and want orthonormal q1,q2. This calculation isn’t quite like elimination: there the goal was a triangular matrix, here the goal is orthonormal columns. There’s really only one idea, and Strang jokes that “if Gram had the idea, I don’t know what Schmidt did” — Schmidt’s contribution, it turns out, is dividing by the lengths at the end.
The plan: first produce orthogonal vectors A and B (that’s Gram’s part), then normalize, q1=A/∥A∥ and q2=B/∥B∥ (that’s Schmidt’s).
The first direction is fine as it is: A=a, no complaint. The trouble is the second direction — b isn’t orthogonal to A. Where in this chapter did a vector show up that was orthogonal? In the very first calculation: projection split b into a piece along a and an error piece e at ninety degrees. Before, we wanted the projection; now the projection is what we throw away and the error is what we keep:
How is the column space of Q related to the column space of the original A with columns (1,1,1) and (1,0,2)? They’re one and the same plane. B is a combination of b and a, so we never left the space — we just put ninety-degree angles into it. The original columns were a perfectly good basis; this one is better, because with it every projection and calculation you’d ever want is a cinch.
A = QR
One final point, and it’s just like elimination. We learned elimination’s steps, and then captured the whole process in matrix language: A=LU. Gram-Schmidt deserves the same treatment. Nobody working in linear algebra writes out the subtraction formulas each time; they write the connection between the matrix A you start with and the matrix Q you end with:
A=QR
— “the magic formula,” the expression of Gram-Schmidt. For two columns:
The entries of R are the inner products of the original columns with the q‘s. And the main point: R is upper triangular. The entry below the diagonal is a1Tq2, and that’s zero — because the whole point of Gram-Schmidt was to construct the later q‘s perpendicular to all the earlier vectors. The result is extremely satisfactory: a matrix with independent columns factors into a matrix with orthonormal columns times a triangular matrix.
Problems
Work these before revealing the solutions — the lecture’s formulas are all you need.
Problem 17.1Completing an orthogonal matrix
Verify that q1=31(1,2,2) and q2=31(2,1,−2) are orthonormal, then find a third column q3 that makes
Q=[q1q2q3]
a square orthogonal matrix.
Show solution
Lengths: 12+22+22=9 and 22+12+(−2)2=9, so dividing by 3 makes each a unit vector. Orthogonality: 91(1⋅2+2⋅1+2⋅(−2))=91(2+2−4)=0.
For q3 we need a unit vector perpendicular to both. Try the same pattern of 1s and 2s with signs fixed up: q3=31(−2,2,−1). Check: (1)(−2)+(2)(2)+(2)(−1)=−2+4−2=0 against q1, and (2)(−2)+(1)(2)+(−2)(−1)=−4+2+2=0 against q2; its length squared is 4+4+1=9. So
Q=3112221−2−22−1
is orthogonal, and QT=Q−1.
Problem 17.2Gram-Schmidt by hand
Apply Gram-Schmidt to
a=110,b=101
to produce orthonormal vectors q1 and q2.
Show solution
A=a. Then ATb=1 and ATA=2, so
B=101−21110=1/2−1/21
Check: ATB=21−21+0=0. Normalize: ∥A∥=2 and ∥B∥=41+41+1=23, so
q1=21110,q2=611−12
(the square roots showing up, as promised).
Problem 17.3Projection made trivial
Let Q have the orthonormal columns q1=31(1,2,2) and q2=31(−2,−1,2). Project b=(3,3,3) onto the column space of Q using p=QQTb, and verify that the error e=b−p is perpendicular to both columns.
Show solution
With an orthonormal basis, each coefficient is a dot product:
q1Tb=33+6+6=5,q2Tb=3−6−3+6=−1
So
p=5q1−1q2=35122−31−2−12=317118
Then e=b−p=31(2,−2,1), and
eTq1=92−4+2=0,eTq2=9−4+2+2=0
No matrix was inverted anywhere — that’s the whole point of P=QQT.
Problem 17.4Finding R in A = QR
For the vectors of Problem 17.2, write A=[ab] and find the matrix R in A=QR. Confirm that R is upper triangular and that QR reproduces A.
Show solution
Since QTQ=I, multiplying A=QR by QT gives R=QTA — inner products of the original columns with the q‘s:
R=[q1Taq2Taq1Tbq2Tb]=[201/26/2]
The zero appears because q2 was constructed perpendicular to a — that’s why R is always upper triangular. Check the second column of QR:
21q1+26q2=1/21/20+1/2−1/21=101=b✓
(and the first column is 2q1=a).
Problem 17.5Orthonormal implies independent
The lecture claims orthonormal vectors “have got to be independent.” Prove it: if q1,…,qn are orthonormal and c1q1+⋯+cnqn=0, show every ci is zero.
Show solution
Take the dot product of the combination with qi:
qiT(c1q1+⋯+cnqn)=c1qiTq1+⋯+cnqiTqn
Every term dies except the i-th, because qiTqj=0 for j=i, while qiTqi=1. So the left side is qiT0=0 and the right side is ci: hence ci=0 for every i. Only the trivial combination gives zero — the vectors are independent.