A728 of 56
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A7 · Lesson 28 · Gram-Schmidt and QR

Straightening a basis

Orthonormal bases make everything easy: coordinates are dot products, projections are QQT, 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, A=QR, it's how good software solves least-squares problems.

  • 𝐚1 is fixed; turn and stretch 𝐚2.
  • Gram-Schmidt keeps the direction of 𝐚1 as 𝐪1. From 𝐚2 it removes the part along 𝐪1 (the dashed arrow), leaving a perpendicular piece that becomes 𝐪2 after scaling to length 1.
  • The readouts are the entries of R. What happens to r22 as 𝐚2 lines up with 𝐚1?

Predict

After Gram-Schmidt, A=QR. What shape is R?

Name it

The process

Given independent 𝐚1,…,𝐚n:

  1. 𝐪1=𝐚1/|𝐚1|.
  2. For each next 𝐚j, subtract its components along the 𝐪's found so far, then normalize:
𝐯j=𝐚j−∑i<j(𝐪i⋅𝐚j)𝐪i𝐪j=𝐯j|𝐯j|

The 𝐪's span the same space as the 𝐚's, step by step.

As a factorization

Record the numbers: rij=𝐪i⋅𝐚j for i<j, and rjj=|𝐯j|. Then

A=QR

with Q having orthonormal columns and R upper triangular with positive diagonal.

Least squares with QR

The normal equations ATA𝐱^=AT𝐛 become R𝐱^=QT𝐛, one back substitution. That's better than forming ATA, 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 𝐚1=(3,4), 𝐚2=(1,0).

Show the solution
  1. Normalize 𝐚1. 𝐪1=(0.6,0.8), with r11=5.
  2. Project 𝐚2 onto it. r12=𝐪1⋅𝐚2=0.6
  3. Subtract the projection. 𝐯2=(1,0)−0.6(0.6,0.8)=(0.64,−0.48)
  4. Normalize. Its length is r22=0.8, so 𝐪2=(0.8,−0.6), and R=[50.600.8]
  5. Check. 𝐪1⋅𝐪2=0.48−0.48=0

Answer 𝐪1=(0.6,0.8) and 𝐪2=(0.8,−0.6), with R=[50.600.8].

Worked example

Apply Gram-Schmidt to 𝐚1=(1,1,0), 𝐚2=(1,0,1).

Show the solution
  1. Normalize 𝐚1. 𝐪1=12(1,1,0)
  2. Project 𝐚2 onto it. 𝐪1⋅𝐚2=12
  3. Subtract the projection. 𝐯2=(1,0,1)−12(1,1,0)=(0.5,−0.5,1)
  4. Normalize. Its length is 1.5=1.225, so 𝐪2=(0.408,−0.408,0.816)

Answer 𝐪1=12(1,1,0) and 𝐪2=(0.408,−0.408,0.816).

Worked example

Run Gram-Schmidt on the columns of [111ε000ε000ε] with ε=10−8, in double precision.

Show the solution
  1. In exact arithmetic. The three 𝐪's are perpendicular.
  2. Classical Gram-Schmidt in floating point. 1+ε2 rounds to 1, and the classical process, which computes every projection coefficient from the original column, returns 𝐪2⋅𝐪3=0.5 so the vectors are 60° apart instead of 90°.
  3. Modified Gram-Schmidt. It subtracts each projection from the running remainder as soon as each 𝐪 is found. On the same matrix it gives 𝐪2⋅𝐪3≈10−16
  4. Library QR. It uses Householder reflections, which are better still.

Answer the classical process leaves 𝐪2 and 𝐪3 60° apart, with 𝐪2⋅𝐪3=0.5; the modified process brings that down to about 10−16.

Check

Gram-Schmidt starts on 𝐚1=(5,4). What is r11?

Check

With 𝐚1=(3,4) and 𝐚2=(2,5), what is r12=𝐪1⋅𝐚2?

Check

With 𝐚1=(3,4) and 𝐚2=(2,5), what is r22, the length of the part of 𝐚2 perpendicular to 𝐚1?

Check

Q is a 2 × 2 orthogonal matrix and 𝐱=(5,4). How long is Q𝐱?

Check

Why solve least squares with R𝐱^=QT𝐛 instead of the normal equations ATA𝐱^=AT𝐛?

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 𝐚1=(3,4) and 𝐚2=(1,0).

(a)

What is |𝐚1|, the first diagonal entry of R?

(b)

What is 𝐪1⋅𝐚2, the top-right entry of R?

Check

Gram-Schmidt on 𝐚1=(1,1), 𝐚2=(x,0).

Step 1

What is 𝐪1⋅𝐚2?

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°.

Explain why

Why does Gram-Schmidt produce vectors that span the same space as the originals?

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.