MatrixStep

Guides8 min

Gram-Schmidt Process: Formula, Example and QR Decomposition

The Gram-Schmidt process turns a set of vectors that lean on each other into a set of mutually perpendicular vectors. The formulas look busy, but they do one thing: subtract the component along each vector built so far. This guide computes a 3D example all the way through and shows what happens when the vectors are linearly dependent.

Why orthogonal bases matter

When the basis vectors are perpendicular to each other, finding coordinates takes a single dot product instead of solving a linear system. Projections, least squares, QR decomposition and orthogonal diagonalization all rest on this property. That is why, given an arbitrary basis, the first move is to convert it into an orthogonal one.

The Gram-Schmidt formula

If v₁, v₂, …, vₙ are linearly independent, build u₁, u₂, …, uₙ as follows.

u₁ = v₁u₂ = v₂ − proj(v₂ → u₁)u₃ = v₃ − proj(v₃ → u₁) − proj(v₃ → u₂)…

Here proj(v → u) = (v·u)/(u·u) u is the projection of v onto the direction of u. In other words, subtract every component along the u's already built, and what remains is perpendicular to all of them.

Written as one general formula for the k-th vector:

uₖ = vₖ − Σⱼ₌₁ᵏ⁻¹ (vₖ·uⱼ)/(uⱼ·uⱼ) uⱼ

and if an orthonormal basis is wanted, finish with eₖ = uₖ / ‖uₖ‖. The coefficient (vₖ·uⱼ)/(uⱼ·uⱼ) is a plain number, so each step is a few dot products and a vector subtraction.

The algorithm in four lines

  1. Take the first vector as it is: u₁ = v₁.
  2. For each next vector vₖ, compute its projection onto every u already built and subtract all of them.
  3. If the result is the zero vector, vₖ was dependent on the earlier vectors: discard it and continue.
  4. Optionally normalize each uₖ at the very end.

Example: three vectors in ℝ³

v₁ =110v₂ =101v₃ =011

Step 1: u₁ = v₁

u₁ =110

Step 2: subtract the u₁ component from v₂

Since v₂·u₁ = 1 and u₁·u₁ = 2, the coefficient is 1/2.

u₂ = v₂ − 1/2u₁ =101−1/2110=12−121

Check: u₁·u₂ = 0. They are perpendicular.

Step 3: subtract the u₁ and u₂ components from v₃

v₃·u₁ = 1, u₁·u₁ = 2 → coefficient 1/2. v₃·u₂ = 1/2, u₂·u₂ = 3/2 → coefficient 1/3.

u₃ = v₃ − 1/2u₁ − 1/3u₂ =−232323

Check: u₁·u₃ = 0, u₂·u₃ = 0. The three vectors are mutually perpendicular: an orthogonal basis.

u₁ =110u₂ =12−121u₃ =−232323

Careful: in step 3, the projection of v₃ must be taken onto u₂. Using v₂ instead gives a wrong answer. Using the orthogonal vectors already built is the whole point of the process.

Making it an orthonormal basis

Divide each uₖ by its own length ‖uₖ‖ = √(uₖ·uₖ) to get an orthonormal basis of unit vectors. The squared lengths are u₁·u₁ = 2, u₂·u₂ = 3/2, u₃·u₃ = 4/3.

e₁ = u₁/√2e₂ = u₂/√(3/2)e₃ = u₃/√(4/3)

Because square roots appear, this is awkward to write by hand, so exams usually ask only for the u's before normalization or let you leave factors like "1/√2" as they are. For the same reason the Gram-Schmidt calculator shows u and ‖u‖² exactly as fractions.

If the vectors are dependent: you get zero

What happens with three vectors where w₃ = w₁ + w₂?

w₁ =120w₂ =011w₃ = w₁ + w₂ =131

Everything goes as before up to u₁ = w₁ and, with coefficient 2/5, u₂ = −2/5, 1/5, 1. But when you subtract the u₁ and u₂ components from w₃,

u₃ =000

you get the zero vector. Since w₃ is already built from the first two vectors, there is no new perpendicular direction. In that case drop u₃; the orthogonal basis ends with the two vectors u₁, u₂. Put the other way round, a zero vector appearing during Gram-Schmidt means "that vector is linearly dependent on the previous ones".

QR decomposition from the same work

Put v₁, v₂, v₃ as the columns of a matrix A. The matrix whose columns are the orthonormal vectors e₁, e₂, e₃ is Q, and R = QᵀA is upper triangular, so A = QR. Nothing new has to be computed: the diagonal of R holds the lengths ‖uₖ‖, and the entries above the diagonal are the dot products eⱼ·vₖ, which are the projection coefficients found during Gram-Schmidt times ‖uⱼ‖.

For the example above, with ‖u₁‖ = √2, ‖u₂‖ = √(3/2) = √6/2 and ‖u₃‖ = √(4/3) = 2/√3:

Q =1/√21/√6−1/√31/√2−1/√61/√302/√61/√3R =√21/√21/√20√6/21/√6002/√3

Check one entry: r₁₂ = e₁·v₂ = (v₂·u₁)/‖u₁‖ = 1/√2, and r₂₃ = e₂·v₃ = (v₃·u₂)/‖u₂‖ = (1/2)/(√6/2) = 1/√6. Multiplying QR gives back A. This is the standard way to get a QR decomposition by hand; computers use Householder reflections instead because they are numerically more stable, but the result is the same.

Common mistakes

FAQ

Does changing the order of the vectors change the result?

Yes, it does. u₁ always points along the first vector, so a different order gives a different orthogonal basis. The space they span is the same, though.

Does the same method work in four or more dimensions?

Exactly the same. The number of steps grows with the number of vectors, not the dimension. The calculator accepts up to 6×6.

What if the dot products are not rational?

If the components are rational, all the dot products and coefficients are rational too, so everything is computed exactly. Square roots appear only when normalizing (dividing by the length).

Related guides