Understanding the Gram–Schmidt calculator — concept and principle
This calculator turns a set of vectors into an orthonormal basis for the same space with the Gram–Schmidt process. It shows the orthogonal vectors before scaling, their lengths, the final unit vectors and which inputs were dependent. Use it for linear algebra homework, QR decomposition and least-squares problems.
How it is calculated
f₁ = v₁
f_k = v_k − Σ_(j<k) (v_k · f_j ÷ ‖f_j‖²) f_j
q_k = f_k ÷ ‖f_k‖
- If some f_k is the zero vector, v_k depended on the earlier vectors; it is skipped, and the number of vectors kept is the dimension of the span.
- Each projection step is repeated once for numerical accuracy. The input order matters.
The principle
Projecting v_k onto f_j finds the part of v_k that points along f_j. Subtracting all such parts leaves only what is new, perpendicular to everything before. Doing this vector by vector builds perpendicular directions spanning the same space, and dividing by their lengths makes them unit vectors. Orthonormal bases are convenient because coordinates become simple dot products and lengths and angles are preserved.
Worked example
Default input: v₁ = (1, 1, 0), v₂ = (1, 0, 1), v₃ = (0, 1, 1).
- f₁ = (1, 1, 0), length √2 = 1.41421.
- v₂ · f₁ = 1, so f₂ = (1, 0, 1) − ½(1, 1, 0) = (1/2, −1/2, 1), length 1.22474.
- v₃ · f₁ = 1 and v₃ · f₂ = 1/2, so f₃ = (0, 1, 1) − ½(1, 1, 0) − (1/3)(1/2, −1/2, 1) = (−2/3, 2/3, 2/3), length 1.1547.
- Lengths: 1.41421, 1.22474, 1.1547; none skipped (none); dimension 3.
- Orthonormal vectors: (0.707107, 0.707107, 0), (0.408248, −0.408248, 0.816497), (−0.57735, 0.57735, 0.57735).
How to read the result
Check two properties: every q has length 1, and the dot product of any two different q's is 0. For example q₁ · q₂ = 0.288675 − 0.288675 + 0 = 0. Starting from a different order of the input vectors gives a different but equally valid orthonormal basis.
Common mistakes
- Projecting onto the original v's instead of the new f's.
- Forgetting to divide by ‖f_j‖² in the projection.
- Normalising too early and then using the wrong formula.
- Not noticing a dependent vector, which gives a zero vector.
Frequently asked questions
What does the Gram–Schmidt process do?
It turns independent vectors into orthonormal vectors spanning the same space.
What if the vectors are dependent?
A zero vector appears; that input is skipped and the span has lower dimension.
How is it related to QR decomposition?
The q vectors form Q, and the projection coefficients form R.
Sources: D. Margalit & J. Rabinoff, Interactive Linear Algebra, §6.4 Orthogonal Sets (Gram–Schmidt process)