Hello! Welcome to the next lesson in our exploration of inner product spaces.
In our last session, we defined orthogonality and saw the convenience of orthonormal sets. We concluded with a question: what if we have a basis that's not orthonormal? Today, we answer that question by learning a powerful algorithmic procedure that does exactly this conversion.
This lesson covers the Gram-Schmidt process, a step-by-step method to transform any set of linearly independent vectors (a basis) into an orthonormal basis. This algorithm is not just a theoretical tool; it's the engine behind fundamental numerical computations in both deep learning and physics, making it a crucial concept for your goals.
1. The Core Idea: Purification by Projection
Imagine you have a basis of vectors, but they're "messy"—skewed at various angles. An orthonormal basis is "clean"—all vectors are at 90-degree angles to each other and have a length of 1. The Gram-Schmidt process is an algorithm that "purifies" a messy basis into a clean one, vector by vector.
The key insight is to use orthogonal projection, a concept we've encountered before. For each vector in our original basis, we'll subtract the parts of it that are "contaminated" by the directions of the new, clean vectors we've already built. What remains is the "pure" part, a new vector that is orthogonal to all the previous ones.
Professor Gilbert Strang provides a wonderful intuitive introduction to this idea.
17. Orthogonal Matrices and Gram-Schmidt
In this segment from his MIT OpenCourseWare lecture, Professor Strang introduces the Gram-Schmidt process by focusing on the core idea for just two vectors: making the second vector orthogonal to the first by subtracting its projection.
Watch from the beginning of the Gram-Schmidt discussion at 25:13 to 32:43. Focus on the geometric intuition of how he constructs an orthogonal vector B from an initial vector b by removing the component parallel to a.
As Professor Strang explained, the process for two vectors, , works as follows:
- First vector: We have nothing to make it orthogonal to yet, so we just accept the first vector as is. Let our first new orthogonal vector be .
- Second vector: We "purify" by subtracting its projection onto . The result, , will be orthogonal to .
2. The General Algorithm
We can generalize this procedure to any number of vectors. To find the -th orthogonal vector , we take the original -th vector and subtract its projections onto all the previously constructed orthogonal vectors .
The image below illustrates this for finding the second and third orthogonal vectors.

This leads to the general formula for constructing an orthogonal basis from an initial basis :
- ...
From Orthogonal to Orthonormal
Once we have our orthogonal basis , creating an orthonormal basis is simple. We just normalize each vector by dividing it by its norm (length). This was "Schmidt's contribution" in Professor Strang's telling.
3. A Complete Worked Example
The algorithm can look intimidating, but it's a straightforward computation. The following video walks through a full example in and clearly lays out all the steps.
This video from Professor Dave Explains provides a clear, step-by-step application of the Gram-Schmidt process to a basis in \mathbb{R}^3, showing both the orthogonalization and normalization steps.
Watch from 01:23 to 08:37. This covers the general formula, a detailed worked example for three vectors, and the final normalization step.
Let's recap the complete process:
Given a basis :
- Orthogonalize: For , compute .
- Normalize: For , compute .
The resulting set is an orthonormal basis for the same space spanned by the original vectors.
Test your understanding!
Let and be a basis for . Use the Gram-Schmidt process to find a corresponding orthonormal basis .
Show answer
Step 1: Orthogonalization
-
Find :
-
Find :
First, compute the inner products (dot products):
Now, substitute these into the formula for :
To make calculations easier, we can scale by any factor. Let's multiply by 5 to get rid of the decimals: . (This is a valid trick because we will normalize it anyway). Our orthogonal basis is .
Step 2: Normalization
- Normalize :
- Normalize :
The orthonormal basis is .
4. Practical Implications for Computer Science
For a computer science student, it's crucial to know that while the "Classical" Gram-Schmidt (CGS) process we've just learned is mathematically sound, it suffers from a major drawback in practice: numerical instability. When implemented on a computer, floating-point rounding errors can accumulate, causing the resulting vectors to lose their orthogonality.
A slightly rearranged version, the Modified Gram-Schmidt (MGS) process, is mathematically equivalent but far more robust to these errors. In MGS, after a vector is produced, its components are immediately removed from all subsequent vectors . This "pay as you go" approach prevents errors from accumulating.
The following notes provide a more rigorous look at Gram-Schmidt, including a striking example that shows how CGS can fail in practice while MGS succeeds. This is a perfect illustration of why numerical linear algebra is a deep field.
Notes on Gram-Schmidt QR Factorization
These notes from the University of Texas at Austin are aimed at a CS audience. We'll focus on the part that demonstrates the practical difference between the classical algorithm and its numerically stable variant.
Read Section 3, titled "In Practice, MGS is More Accurate" (pages 7-10). You don't need to follow every single calculation. The goal is to appreciate the result shown in Figure 8 and the subsequent discussion: why does one algorithm produce nearly orthogonal vectors while the other fails badly, even though they are identical in theory?
The Gram-Schmidt process is also the theoretical foundation for QR factorization, a cornerstone of numerical computing. It states that any matrix with linearly independent columns can be factored into , where:
- Q is a matrix with orthonormal columns (the basis vectors we just made!).
- R is an upper-triangular matrix that stores the "recipe" of the projections.
This decomposition is used everywhere, from solving linear systems to the algorithms inside deep learning libraries. In practice, you would use a library function like np.linalg.qr in Python, which typically uses a stable variant of Gram-Schmidt or other, even more robust methods.
Conclusion
Today, we've added a powerful constructive tool to our linear algebra toolkit. The Gram-Schmidt process provides a concrete algorithm for turning any basis into a much more convenient orthonormal basis.
Key Takeaways:
- The Gram-Schmidt process builds an orthonormal basis by sequentially orthogonalizing and then normalizing vectors.
- The core step is projection: to make a vector orthogonal to the existing set , you subtract the projection of onto each of those vectors.
- The classical version of the algorithm is numerically unstable; the Modified Gram-Schmidt process is used in practice for better accuracy.
- This process is the theoretical basis for QR decomposition, a fundamental tool in computational science.
Preview of the Next Lesson:
The Gram-Schmidt process used projection as a building block to orthogonalize a set of vectors. In the next lesson, we will focus directly on the orthogonal projection of a single vector onto an entire subspace. This will generalize our work from today and give us the primary tool needed to solve "unsolvable" systems of linear equations, leading directly to the concept of least squares approximation—a fundamental technique in data analysis and machine learning.