Gram-Schmidt
calculator.
Turn a list of vectors into perpendicular ones with the same span, then into unit length.
- Dimension of the span
- 3
- Vectors dropped
- 0
- Orthonormal check
- QᵀQ = I
The 3 vectors are independent and fill all of R³, so e₁ to e₃ are an orthonormal basis for the whole space.
Inputs and the orthonormal vectors
Gray arrows are the inputs v₁ = (1, −1, 0), v₂ = (2, 0, −2), v₃ = (3, −3, 3). Colored arrows are the orthonormal vectors e₁ = (0.707, −0.707, 0), e₂ = (0.408, 0.408, −0.816), e₃ = (0.577, 0.577, 0.577), each of length 1 and at right angles to the others, in an oblique 3D view.
Orthonormal vectors e = u ÷ ‖u‖
| Vector | Exact | Decimals |
|---|---|---|
| e₁u₁ ÷ √2 | u₁ ÷ √2 | (0.707107, −0.707107, 0) |
| e₂u₂ ÷ √6 | u₂ ÷ √6 | (0.408248, 0.408248, −0.816497) |
| e₃u₃ ÷ √3 | u₃ ÷ √3 | (0.57735, 0.57735, 0.57735) |
The columns of Q are the e vectors. Column j of R holds the amounts of each e that rebuild v₁ to v₃, so A = QR with the inputs as the columns of A.
Working
- 1u₁ = v₁u₁ = (1, −1, 0)
- 2u₂ = v₂ − projectionsu₂ = (2, 0, −2) − (22)·u₁ = (1, 1, −2)
- 3u₃ = v₃ − projectionsu₃ = (3, −3, 3) − (62)·u₁ − (−66)·u₂ = (1, 1, 1)
- 4Normalize u₁‖u₁‖ = √2, e₁ = u₁ ÷ ‖u₁‖ = (0.707107, −0.707107, 0)
- 5Normalize u₂‖u₂‖ = √6, e₂ = u₂ ÷ ‖u₂‖ = (0.408248, 0.408248, −0.816497)
- 6Normalize u₃‖u₃‖ = √3, e₃ = u₃ ÷ ‖u₃‖ = (0.57735, 0.57735, 0.57735)
- 7Check: QᵀQ is the identity, so the e vectors are at right angles and of length 1QᵀQ =
- 8Check: Q (columns e₁, e₂, e₃) times R gives back A, the inputs as columnsQR =
How it's worked out
The Gram-Schmidt process takes vectors v₁, v₂ and so on, and builds vectors u₁, u₂ that are at right angles to each other. It keeps the first vector as it is. Every later vector has its shadow along each earlier u taken away. That shadow is called the projection.
Each fraction is a dot product over a dot product, so with whole-number inputs the u vectors stay as fractions. At every stage u₁ to uₖ span the same space as v₁ to vₖ.
Dividing each u by its length gives the orthonormal vectors e, which have length 1. The lengths are square roots, so the e vectors are usually irrational even when the u vectors are not.
QR factorization. Put the inputs as the columns of a matrix A. The e vectors as columns form Q, and R = QᵀA is upper triangular with the lengths of the u vectors on its diagonal, so A = QR. The tool subtracts each projection from the partly reduced vector, the modified order, which gives the same answer in exact arithmetic and keeps rounding errors smaller.
Worked examples
Nadia orthogonalizes three vectors from her textbook
Nadia starts with v₁ = (1, −1, 0), v₂ = (2, 0, −2) and v₃ = (3, −3, 3). She keeps u₁ = v₁. For u₂ the projection fraction is , which is 1, so she takes that many u₁ away from v₂.
For u₃ the fractions are and , which leaves u₃ = (1, 1, 1). The lengths are √2, √6, √3, so e₁ is u₁ over √2, and so on.
Theo finds a right-angled basis for a plane
Theo's plane in 3D space is spanned by (3, 6, 0) and (1, 2, 2). The dot products are 15 and 45, so the fraction is , which reduces to .
Now (3, 6, 0) and (0, 0, 2) span the same plane at right angles. Their lengths are 3√5 and 2.
Iris spots a vector that adds nothing
Iris enters (1, 2, 3), (2, 4, 6) and (0, 1, 0). The second is twice the first, so removing its projection leaves (0, 0, 0). The tool flags it, drops it and carries on.
What is left is (1, 2, 3) and (−1/7, 5/7, −3/7), so the three vectors only span a plane: the dimension is 2. That number is also the rank of the matrix with these vectors as rows.
Reading your result
Order matters. u₁ is always v₁, so a new order gives a new basis for the same space. Listing Theo's vectors the other way round gives u₂ = (4/3, 8/3, −10/3) instead.
Reading Q and R. In the lecture example the inputs are (1, 0, 0, 0), (1, 1, 0, 1), (0, 2, 2, 0), and the u vectors come out as (1, 0, 0, 0), (0, 1, 0, 1), (0, 1, 2, −1). The diagonal of R holds their lengths, 1, 1.4142, 2.4495. Column j of R says how much of each e rebuilds vⱼ, and the zeros below the diagonal say that v₁ uses only e₁.
A zero u means dependence. The tool treats a u as zero when it is shorter than 10⁻¹⁰ times the length of the vector it came from. The test is relative, so very small or very large inputs are judged fairly.
More vectors than components. In a space with n components, at most n vectors can be independent, so the extra ones are always dropped.
Common mistakes
- Projecting onto the v vectors. Each projection must use the new u vectors, not the original v vectors. Using v gives vectors that are not at right angles.
- Normalizing too early. If you divide by the length at each step, square roots enter every later fraction. Keep the u vectors exact and normalize at the end.
- Forgetting a projection. u₃ needs both the u₁ and the u₂ parts taken away. Check by dotting u₃ with each earlier u.
- Rounding the e vectors and checking QᵀQ. Six decimal places give a product that is close to the identity, not exactly it. Use the exact u vectors to check right angles.