Linear Algebra · Undergraduate

The Gram–Schmidt Process and QR Factorization

Quick answer

The Gram–Schmidt process converts a basis x₁, …, x_p of a subspace into an orthogonal basis of the same subspace. Keep the first vector; from each later vector subtract its projections onto the vectors already built, leaving only the part orthogonal to them. Dividing each result by its length gives an orthonormal basis. Recording the process in matrix form writes A = QR, with orthonormal columns in Q and an upper triangular R, a factorization that computers use to solve least-squares problems.

What you'll learn

  • Apply the Gram–Schmidt process to a basis
  • Normalize an orthogonal basis to an orthonormal one
  • Explain why each step keeps the same span
  • Form the QR factorization of a matrix with independent columns

Building an orthogonal basis

Orthogonal bases turn coordinates and projections into dot products, but the bases that come out of row reduction are rarely orthogonal. The Gram–Schmidt process fixes that. Given a basis x1,…,xp\mathbf{x}_1, \ldots, \mathbf{x}_p of a subspace WW:

v1=x1v2=x2−x2⋅v1v1⋅v1 v1v3=x3−x3⋅v1v1⋅v1 v1−x3⋅v2v2⋅v2 v2\begin{aligned} \mathbf{v}_1 &= \mathbf{x}_1\\ \mathbf{v}_2 &= \mathbf{x}_2 - \frac{\mathbf{x}_2 \cdot \mathbf{v}_1}{\mathbf{v}_1 \cdot \mathbf{v}_1}\,\mathbf{v}_1\\ \mathbf{v}_3 &= \mathbf{x}_3 - \frac{\mathbf{x}_3 \cdot \mathbf{v}_1}{\mathbf{v}_1 \cdot \mathbf{v}_1}\,\mathbf{v}_1 - \frac{\mathbf{x}_3 \cdot \mathbf{v}_2}{\mathbf{v}_2 \cdot \mathbf{v}_2}\,\mathbf{v}_2 \end{aligned}

and so on: each new vector minus its projections onto every vector already built. The result v1,…,vp\mathbf{v}_1, \ldots, \mathbf{v}_p is an orthogonal basis of WW. Dividing each by its length gives an orthonormal basis.

One Gram–Schmidt step in the plane Arrows from the origin for x₁ = (3, 1) and x₂ = (2, 2). The projection of x₂ onto x₁ lands at (2.4, 0.8), marked on x₁. A dashed segment from there to the tip of x₂ is the part of x₂ orthogonal to x₁, and the arrow v₂ = (−0.4, 1.2) from the origin is that same piece moved to start at the origin. x₁ x₂ -112312xy proj v₂
One Gram–Schmidt step in the plane

Why each step keeps the span and adds orthogonality

At step kk, the vectors v1,…,vk−1\mathbf{v}_1, \ldots, \mathbf{v}_{k-1} are already orthogonal and span the same subspace as x1,…,xk−1\mathbf{x}_1, \ldots, \mathbf{x}_{k-1}. The subtracted terms are exactly the projection of xk\mathbf{x}_k onto that subspace. So vk\mathbf{v}_k is the error of that projection, and the error is orthogonal to the subspace. It is not zero, since xk\mathbf{x}_k is not in the span of the earlier vectors. And vk\mathbf{v}_k differs from xk\mathbf{x}_k by a combination of earlier vectors, so the span is unchanged. Each step strips away the part of the new vector that the earlier ones already cover, and what remains is orthogonal to all of them.

Worked examples

Common mistakes

Practice problems

  1. Apply Gram–Schmidt to (1,0)(1, 0) and (1,2)(1, 2).

    Answer

    (1,0)(1, 0) and (0,2)(0, 2)

    Full solution

    v2=(1,2)−11(1,0)=(0,2)\mathbf{v}_2 = (1, 2) - \tfrac{1}{1}(1, 0) = (0, 2).

  2. Apply Gram–Schmidt to (1,1)(1, 1) and (1,3)(1, 3).

    Answer

    (1,1)(1, 1) and (−1,1)(-1, 1)

    Full solution

    The coefficient is 1+32=2\tfrac{1 + 3}{2} = 2, so v2=(1,3)−(2,2)=(−1,1)\mathbf{v}_2 = (1, 3) - (2, 2) = (-1, 1).

  3. Normalize the answer to exercise 2.

    Answer

    12(1,1)\tfrac{1}{\sqrt{2}}(1, 1) and 12(−1,1)\tfrac{1}{\sqrt{2}}(-1, 1)

    Full solution

    Both vectors have length 2\sqrt{2}.

  4. Apply Gram–Schmidt to (1,0,1)(1, 0, 1) and (2,1,0)(2, 1, 0).

    Answer

    (1,0,1)(1, 0, 1) and (1,1,−1)(1, 1, -1)

    Full solution

    The coefficient is 2+0+02=1\tfrac{2 + 0 + 0}{2} = 1, so v2=(2,1,0)−(1,0,1)=(1,1,−1)\mathbf{v}_2 = (2, 1, 0) - (1, 0, 1) = (1, 1, -1). Check: 1+0−1=01 + 0 - 1 = 0.

  5. What does Gram–Schmidt produce from (1,2)(1, 2) and (2,4)(2, 4)?

    Answer

    (1,2)(1, 2) and the zero vector

    Full solution

    The second vector is twice the first, so its projection is itself and nothing is left. The span is only a line.

  6. Why may (12,−12,1)\left(\tfrac{1}{2}, -\tfrac{1}{2}, 1\right) be replaced by (1,−1,2)(1, -1, 2) in the middle of the process?

    Answer

    Scaling keeps orthogonality and keeps the span.

    Full solution

    If u⋅v=0\mathbf{u} \cdot \mathbf{v} = 0 then u⋅(cv)=0\mathbf{u} \cdot (c\mathbf{v}) = 0, and cvc\mathbf{v} spans the same line as v\mathbf{v} for c≠0c \ne 0. Later projections onto that line come out the same.

  7. Is {12(1,1,0), 16(1,−1,2), 13(−1,1,1)}\left\{\tfrac{1}{\sqrt{2}}(1, 1, 0),\ \tfrac{1}{\sqrt{6}}(1, -1, 2),\ \tfrac{1}{\sqrt{3}}(-1, 1, 1)\right\} a basis of ℝ³?

    Answer

    Yes, an orthonormal one

    Full solution

    Three orthogonal nonzero vectors are independent, and three independent vectors in ℝ³ form a basis.

  8. In Example 4, what is QTQQ^{\mathsf{T}}Q?

    Answer

    The identity matrix

    Full solution

    Its entries are the dot products of the columns of QQ: 11 on the diagonal because they are unit vectors, and 00 off it because they are orthogonal.

  9. Why is the factor RR in A=QRA = QR upper triangular?

    Answer

    Each column of QQ is orthogonal to every earlier column of AA.

    Full solution

    Entry (i,j)(i, j) of R=QTAR = Q^{\mathsf{T}}A is qi⋅xj\mathbf{q}_i \cdot \mathbf{x}_j. Gram–Schmidt makes qi\mathbf{q}_i orthogonal to x1,…,xi−1\mathbf{x}_1, \ldots, \mathbf{x}_{i-1}, so every entry below the diagonal is 00.

  10. A student computes v3\mathbf{v}_3 by subtracting from x3\mathbf{x}_3 its projections onto x1\mathbf{x}_1 and x2\mathbf{x}_2. What went wrong?

    Hint

    Is {x1,x2}\{\mathbf{x}_1, \mathbf{x}_2\} orthogonal?

    Answer

    The projections must be onto the orthogonal vectors v1\mathbf{v}_1 and v2\mathbf{v}_2.

    Full solution

    The sum of projections onto separate vectors equals the projection onto their span only when those vectors are orthogonal. With x1\mathbf{x}_1 and x2\mathbf{x}_2 the pieces overlap, and the result is not orthogonal to the span.

Frequently asked questions

What does the Gram–Schmidt process do?

It takes any basis of a subspace and produces an orthogonal basis of the same subspace, one vector at a time.

What is the formula for each step?

v_k = x_k minus the projections of x_k onto v₁, …, v_(k−1). Each projection is (x_k · v_j)/(v_j · v_j) v_j.

Can I rescale the vectors along the way?

Yes. Multiplying a vector by a nonzero number keeps it orthogonal to the others and keeps the span, so clearing fractions is safe.

What happens if the starting vectors are dependent?

At the step where a vector lies in the span of the earlier ones, subtracting its projections leaves the zero vector. That vector adds nothing and is dropped.

What is the QR factorization?

A = QR, where Q has orthonormal columns from Gram–Schmidt and R = QᵀA is upper triangular. It exists whenever A has independent columns.

What to learn next