A7 · Lesson 28 · Gram-Schmidt and QR
Straightening a basis
Orthonormal bases make everything easy: coordinates are dot products, projections are , and nothing is lost to rounding. But the bases problems hand you, the columns of a measurement matrix or a set of signal templates, are rarely perpendicular. Gram-Schmidt fixes that: it takes any independent vectors and straightens them, one at a time, into an orthonormal basis for the same space. Written as a factorization, , it's how good software solves least-squares problems.
- is fixed; turn and stretch .
- Gram-Schmidt keeps the direction of as . From it removes the part along (the dashed arrow), leaving a perpendicular piece that becomes after scaling to length 1.
- The readouts are the entries of . What happens to as lines up with ?
Predict
After Gram-Schmidt, . What shape is ?
Upper triangular. is built from alone, from and , and so on, so column of is a combination of only. The entries below the diagonal of are zero.
Name it
The process
Given independent :
- .
- For each next , subtract its components along the 's found so far, then normalize:
The 's span the same space as the 's, step by step.
As a factorization
Record the numbers: for , and . Then
with having orthonormal columns and upper triangular with positive diagonal.
Least squares with QR
The normal equations become , one back substitution. That's better than forming , which squares the matrix's sensitivity to rounding (lesson 52). np.linalg.lstsq and np.linalg.qr work this way.
In practice
Done exactly as written, rounding errors make the computed 's drift away from perpendicular when the 's are nearly dependent. Modified Gram-Schmidt, which subtracts each projection from the running vector one at a time, is much steadier; libraries use Householder reflections, which are steadier still.
Worked example
Apply Gram-Schmidt to , .
Show the solution
- Normalize . , with .
- Project onto it.
- Subtract the projection.
- Normalize. Its length is , so , and
- Check.
Answer and , with .
Worked example
Apply Gram-Schmidt to , .
Show the solution
- Normalize .
- Project onto it.
- Subtract the projection.
- Normalize. Its length is , so
Answer and .
Worked example
Run Gram-Schmidt on the columns of with , in double precision.
Show the solution
- In exact arithmetic. The three 's are perpendicular.
- Classical Gram-Schmidt in floating point. rounds to 1, and the classical process, which computes every projection coefficient from the original column, returns so the vectors are 60° apart instead of 90°.
- Modified Gram-Schmidt. It subtracts each projection from the running remainder as soon as each is found. On the same matrix it gives
- Library QR. It uses Householder reflections, which are better still.
Answer the classical process leaves and 60° apart, with ; the modified process brings that down to about .
Check
Gram-Schmidt starts on . What is ?
Check
With and , what is ?
Check
With and , what is , the length of the part of perpendicular to ?
Check
is a 2 × 2 orthogonal matrix and . How long is ?
Check
Why solve least squares with instead of the normal equations ?
Check
Write gram_schmidt(A) returning (Q, R) for a NumPy array with independent columns, using modified Gram-Schmidt: for each column, subtract its projections onto the earlier q's one at a time from the running vector, then normalize.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Gram-Schmidt starts with and .
(a)
What is , the first diagonal entry of ?
(b)
What is , the top-right entry of ?
Check
Gram-Schmidt on , .
Step 1
What is ?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
Subtract that much of from . What is the first entry of what's left?
Explain why
Why does Gram-Schmidt produce vectors that span the same space as the originals?
Each step only subtracts multiples of the 's already found, which are themselves combinations of the earlier 's, and then rescales. So is a combination of , and running the steps backward, is a combination of with coefficients from . Since each set can be built from the other, they span the same space, and independence guarantees no is ever zero.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.