MathsLinear Algebra

Gram-Schmidt
calculator.

Turn a list of vectors into perpendicular ones with the same span, then into unit length.

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.

u1\displaystyle u_1=v1\displaystyle {}= v_1
u2\displaystyle u_2=v2−v2⋅u1u1⋅u1u1\displaystyle {}= v_2 - \frac{v_2 \cdot u_1}{u_1 \cdot u_1}\, u_1
u3\displaystyle u_3=v3\displaystyle {}= v_3−v3⋅u1u1⋅u1u1\displaystyle {}- \frac{v_3 \cdot u_1}{u_1 \cdot u_1}\, u_1−v3⋅u2u2⋅u2u2\displaystyle {}- \frac{v_3 \cdot u_2}{u_2 \cdot u_2}\, u_2

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.

ek\displaystyle e_k=uk‖uk‖\displaystyle {}= \frac{u_k}{\lVert u_k \rVert}

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 22\frac{2}{2}, which is 1, so she takes that many u₁ away from v₂.

u2\displaystyle u_2=(2,0,−2)−(1,−1,0)\displaystyle {}= (2, 0, -2) - (1, -1, 0)=(1,1,−2)\displaystyle {}= (1, 1, -2)

For u₃ the fractions are 62\frac{6}{2} and −66\frac{-6}{6}, which leaves u₃ = (1, 1, 1). The lengths are √2, √6, √3, so e₁ is u₁ over √2, and so on.

Open this example in the tool

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 1545\frac{15}{45}, which reduces to 13\frac{1}{3}.

u2\displaystyle u_2=(1,2,2)−1545(3,6,0)\displaystyle {}= (1, 2, 2) - \frac{15}{45}\,(3, 6, 0)=(0,0,2)\displaystyle {}= (0, 0, 2)

Now (3, 6, 0) and (0, 0, 2) span the same plane at right angles. Their lengths are 3√5 and 2.

Open this example in the tool

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.

Open this example in the tool

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.

Questions people ask

Yes. Any order gives a valid orthonormal basis for the same space, just a different one. To match a textbook answer, use the order the book uses.

Sources