Linear Algebra, Live
Strang §4.4 · The Gram-Schmidt process

Gram-Schmidt: making vectors orthonormal

Slide 6 showed how easy everything gets with orthonormal vectors. But real vectors rarely come that way, so we need a recipe that makes them. Picture two sticks pushed into the ground, the second one leaning toward the first. Keep the first stick as it is. From the second, take away the part that leans along the first: its shadow on the first stick, from slide 2. What is left stands at a right angle to the first stick. Do the same with each next vector, removing its shadows on all the earlier ones, then divide every vector by its length. That recipe is Gram-Schmidt. Writing the old vectors in terms of the new ones gives A = QR, where R holds the dot products qᵢᵀ(column j) and its diagonal holds the lengths (see below).

Lesson 5 · slide 7Gram-Schmidt
u₂ = b − (aᵀb / aᵀa) a    q₁ = a / ‖a‖,   q₂ = u₂ / ‖u₂‖    A = QR
Key ideas
  1. The first vector a is kept as it is.
  2. From b, subtract its projection onto a. The remainder u₂ is perpendicular to a.
  3. From a third vector c, subtract its projections onto both a and u₂.
  4. Finally divide each by its length: q₁ = a/‖a‖, q₂ = u₂/‖u₂‖, and so on.
  5. With a, b, c as the columns of A, each column is a combination of the q's: A = QR, with R upper triangular.

The idea

From now on a, b, c name three vectors, the columns of A (not the numbers in a 2 by 2 matrix). Gram-Schmidt turns independent vectors a, b, c into perpendicular ones, one at a time. From each new vector, remove its projection onto the ones before it (slide 2).

The steps

Keep the first direction as it is: u₁ = a (below we just write a).   u₂ = b − (aᵀb / aᵀa) a
Check: aᵀu₂ = aᵀb − (aᵀb / aᵀa) aᵀa = 0 ✓
q₁ = a / ‖a‖,   q₂ = u₂ / ‖u₂‖

u₂ is the error e from slide 2: the part of b off the line through a.

A worked example

a = 11,   b = 20
aᵀb = 2,   aᵀa = 2:   u₂ = 20 − 1·11 = 1−1
a · u₂ = 1 − 1 = 0 ✓
q₁ = 11/√2,   q₂ = 1−1/√2
b = 20 drops onto the line through a = 11. Its shadow is p = 11; the leftover e = 1−1 is the new perpendicular vector u₂.

A second pair, with less friendly numbers:

a = 31,   b = 13:   aᵀb = 6,   aᵀa = 10,   shadow 0.6·31 = 1.80.6
u₂ = 13 − 1.80.6 = −0.82.4,   a · u₂ = −2.4 + 2.4 = 0 ✓
b = 13 leans toward a = 31. Remove its shadow p = 1.80.6 and the leftover u₂ = −0.82.4 stands at a right angle to a.

Three vectors and more

For c, subtract its projection onto each earlier direction:

u₃ = c − (aᵀc / aᵀa) a − (u₂ᵀc / u₂ᵀu₂) u₂

Each fraction is a number. Since aᵀu₂ = 0, the two subtractions don't get in each other's way. A useful trick: stretching a vector doesn't change its direction, so we may multiply any u by a number to clear fractions before going on. The picture below does this for a = 110, b = 101, c = 011, with the vectors as the columns:

u₂ = b − ½a = ½−½1; doubled: 1−12
with the doubled u₂ = 1−12: u₂ᵀc = 0 − 1 + 2 = 1 and u₂ᵀu₂ = 1 + 1 + 4 = 6, so the fraction is ⅙
u₃ = c − ½a − ⅙1−12 = −⅔⅔⅔; times 1.5: −111
checks: 110 · 1−12 = 0,   110 · −111 = 0,   1−12 · −111 = −1 − 1 + 2 = 0 ✓
Gram-Schmidt one column at a time. Each new column loses its shadows on the columns before it, so all three end up at right angles. The last step turns them into q₁, q₂, q₃.

Done without the stretching (one of the examples below), the same three vectors give the same directions.

A bigger example: the columns of a line fit

Fitting a line to readings at t = 0, 1, 2, 3 (slide 5) uses the columns a = 1111 and b = 0123.

aᵀb = 0 + 1 + 2 + 3 = 6,   aᵀa = 4,   shadow 1.5·a = 1.51.51.51.5
u₂ = b − 1.5a = −1.5−0.50.51.5,   a · u₂ = −1.5 − 0.5 + 0.5 + 1.5 = 0 ✓
‖a‖ = 2,   ‖u₂‖² = 2.25 + 0.25 + 0.25 + 2.25 = 5:   q₁ = ½1111,   q₂ = u₂ / √5

Gram-Schmidt just subtracted the average time, 1.5. The new times add to zero, which is the trick from the accelerometer example on slide 5. With a thousand readings it is the same: subtract the average time.

Keep reading with the full course

Finish lesson 5 and you will be able to: fit the best line to noisy sensor data.

Still locked here:

  • The rest of this explanation
  • 4 more examples with step-by-step solutions
  • Five extra members-only problems with worked solutions

$20 once, lifetime access. Lessons 1 to 3 stay free.

Examples

Example 1 · Two vectors in the plane

a = 10, b = 34.

Show solutionHide solution
aᵀb = 3, aᵀa = 1:   u₂ = 34 − 3·10 = 04
q₁ = 10,   q₂ = 01

The result is the standard basis, and R = 1304.

Exercise Make u2 perpendicular to a by removing from b its projection onto a. Edit the code and press Run.
slide_7.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

Questions and comments

Stuck on something, or found a mistake? Ask here and I'll answer. You sign in with GitHub.

The full course

From Ax = b to PCA, by running it

Lessons 1 to 3 are free. Finish all eight and you will be able to:

You get: all 8 lessons with Python labs · every worked example and solution · 5 extra problems per lesson · your progress tracked · answers to your questions · every future update.

$20 once · lifetime access
Continue to checkout →

Already a member? Log in to the course