Math Core

Lesson 6.4 · Orthogonality and Least Squares

The Gram-Schmidt process

Orthogonal bases make coordinates and projections easy, but the bases you meet in practice (columns of a matrix, solutions of a system) are rarely orthogonal. The Gram-Schmidt process fixes that: it turns any basis of a subspace into an orthogonal basis of the same subspace, one vector at a time, using nothing but projections.

The idea with two vectors

Suppose W=Span⁡{x1,x2}W = \operatorname{Span}\{\mathbf{x}_1, \mathbf{x}_2\} with x1,x2\mathbf{x}_1, \mathbf{x}_2 independent but not orthogonal. Keep the first vector: v1=x1\mathbf{v}_1 = \mathbf{x}_1. From x2\mathbf{x}_2, subtract its projection onto v1\mathbf{v}_1:

v2=x2−x2⋅v1v1⋅v1v1.\mathbf{v}_2 = \mathbf{x}_2 - \frac{\mathbf{x}_2 \cdot \mathbf{v}_1}{\mathbf{v}_1 \cdot \mathbf{v}_1}\mathbf{v}_1.

This is the component of x2\mathbf{x}_2 orthogonal to v1\mathbf{v}_1, so v1⋅v2=0\mathbf{v}_1 \cdot \mathbf{v}_2 = 0. It is still in WW, being a combination of x2\mathbf{x}_2 and x1\mathbf{x}_1. And it isn't 0\mathbf{0}, because x2\mathbf{x}_2 is not a multiple of x1\mathbf{x}_1. So {v1,v2}\{\mathbf{v}_1, \mathbf{v}_2\} is an orthogonal basis for WW.

Worked example: Two vectors in R³

Find an orthogonal basis for W=Span⁡{x1,x2}W = \operatorname{Span}\{\mathbf{x}_1, \mathbf{x}_2\}, where x1=(1,2,2)\mathbf{x}_1 = (1, 2, 2) and x2=(4,5,2)\mathbf{x}_2 = (4, 5, 2).

Let v1=x1\mathbf{v}_1 = \mathbf{x}_1. Then x2⋅v1=4+10+4=18\mathbf{x}_2 \cdot \mathbf{v}_1 = 4 + 10 + 4 = 18 and v1⋅v1=9\mathbf{v}_1 \cdot \mathbf{v}_1 = 9, so

v2=(4,5,2)−189(1,2,2)=(4,5,2)−(2,4,4)=(2,1,−2).\mathbf{v}_2 = (4, 5, 2) - \frac{18}{9}(1, 2, 2) = (4, 5, 2) - (2, 4, 4) = (2, 1, -2).

Check: v1⋅v2=2+2−4=0\mathbf{v}_1 \cdot \mathbf{v}_2 = 2 + 2 - 4 = 0. So {(1,2,2),(2,1,−2)}\{(1, 2, 2), (2, 1, -2)\} is an orthogonal basis for WW.

The general process

For more vectors, keep going: each new xk\mathbf{x}_k has its projection onto the span of all the earlier v\mathbf{v}'s removed. Because those earlier v\mathbf{v}'s are already orthogonal, that projection is just a sum of one-line projections.

The Gram-Schmidt process

Given a basis {x1,…,xp}\{\mathbf{x}_1, \ldots, \mathbf{x}_p\} of a subspace WW of Rn\mathbb{R}^n, define

v1=x1v2=x2−x2⋅v1v1⋅v1v1v3=x3−x3⋅v1v1⋅v1v1−x3⋅v2v2⋅v2v2  ⋮vp=xp−xp⋅v1v1⋅v1v1−⋯−xp⋅vp−1vp−1⋅vp−1vp−1.\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 \\ &\ \ \vdots \\ \mathbf{v}_p &= \mathbf{x}_p - \frac{\mathbf{x}_p \cdot \mathbf{v}_1}{\mathbf{v}_1 \cdot \mathbf{v}_1}\mathbf{v}_1 - \cdots - \frac{\mathbf{x}_p \cdot \mathbf{v}_{p-1}}{\mathbf{v}_{p-1} \cdot \mathbf{v}_{p-1}}\mathbf{v}_{p-1}. \end{aligned}

Then {v1,…,vp}\{\mathbf{v}_1, \ldots, \mathbf{v}_p\} is an orthogonal basis for WW. Moreover, for each kk, Span⁡{v1,…,vk}=Span⁡{x1,…,xk}\operatorname{Span}\{\mathbf{v}_1, \ldots, \mathbf{v}_k\} = \operatorname{Span}\{\mathbf{x}_1, \ldots, \mathbf{x}_k\}.

In the language of the last lesson, vk=xk−proj⁡Wk−1xk\mathbf{v}_k = \mathbf{x}_k - \operatorname{proj}_{W_{k-1}}\mathbf{x}_k, where Wk−1=Span⁡{x1,…,xk−1}W_{k-1} = \operatorname{Span}\{\mathbf{x}_1, \ldots, \mathbf{x}_{k-1}\}. By the orthogonal decomposition theorem, vk\mathbf{v}_k is orthogonal to Wk−1W_{k-1}, hence to all earlier v\mathbf{v}'s.

Tip

You may rescale any vk\mathbf{v}_k before moving on, for example to clear fractions. Scaling doesn't change orthogonality or the span, and it keeps later arithmetic clean. Just be sure to use the rescaled vector consistently in every later step.

Worked example: Three vectors in R⁴

Apply Gram-Schmidt to x1=(1,0,1,0)\mathbf{x}_1 = (1, 0, 1, 0), x2=(1,1,1,1)\mathbf{x}_2 = (1, 1, 1, 1), x3=(0,1,2,1)\mathbf{x}_3 = (0, 1, 2, 1).

Step 1. v1=(1,0,1,0)\mathbf{v}_1 = (1, 0, 1, 0), with v1⋅v1=2\mathbf{v}_1 \cdot \mathbf{v}_1 = 2.

Step 2. x2⋅v1=2\mathbf{x}_2 \cdot \mathbf{v}_1 = 2, so

v2=(1,1,1,1)−22(1,0,1,0)=(0,1,0,1),v2⋅v2=2.\mathbf{v}_2 = (1, 1, 1, 1) - \frac{2}{2}(1, 0, 1, 0) = (0, 1, 0, 1), \qquad \mathbf{v}_2 \cdot \mathbf{v}_2 = 2.

Step 3. x3⋅v1=0+2=2\mathbf{x}_3 \cdot \mathbf{v}_1 = 0 + 2 = 2 and x3⋅v2=1+1=2\mathbf{x}_3 \cdot \mathbf{v}_2 = 1 + 1 = 2, so

v3=(0,1,2,1)−22(1,0,1,0)−22(0,1,0,1)=(−1,0,1,0).\mathbf{v}_3 = (0, 1, 2, 1) - \frac{2}{2}(1, 0, 1, 0) - \frac{2}{2}(0, 1, 0, 1) = (-1, 0, 1, 0).

Check all pairs: v1⋅v2=0\mathbf{v}_1 \cdot \mathbf{v}_2 = 0, v1⋅v3=−1+1=0\mathbf{v}_1 \cdot \mathbf{v}_3 = -1 + 1 = 0, v2⋅v3=0\mathbf{v}_2 \cdot \mathbf{v}_3 = 0. The orthogonal basis is {(1,0,1,0),(0,1,0,1),(−1,0,1,0)}\{(1, 0, 1, 0), (0, 1, 0, 1), (-1, 0, 1, 0)\}.

Common mistake

In step kk, project xk\mathbf{x}_k onto the new vectors v1,…,vk−1\mathbf{v}_1, \ldots, \mathbf{v}_{k-1}, not onto the original x1,…,xk−1\mathbf{x}_1, \ldots, \mathbf{x}_{k-1}. The one-line projections only add up correctly because the v\mathbf{v}'s are orthogonal; the x\mathbf{x}'s are not.

Orthonormal bases

To get an orthonormal basis, run Gram-Schmidt and then normalize each vk\mathbf{v}_k. Normalize at the end, not along the way: dividing by square roots mid-process fills every later step with radicals.

For the first example, ∥v1∥=∥v2∥=3\|\mathbf{v}_1\| = \|\mathbf{v}_2\| = 3, so an orthonormal basis for WW is

q1=13(1,2,2),q2=13(2,1,−2).\mathbf{q}_1 = \tfrac{1}{3}(1, 2, 2), \qquad \mathbf{q}_2 = \tfrac{1}{3}(2, 1, -2).

If the input vectors are dependent, Gram-Schmidt reveals it: some vk\mathbf{v}_k comes out as 0\mathbf{0}, meaning xk\mathbf{x}_k was already in the span of the earlier vectors. Drop it and continue.

QR factorization

Gram-Schmidt, written in matrix form, is a factorization.

QR factorization

If AA is an m×nm \times n matrix with linearly independent columns, then A=QRA = QR, where QQ is m×nm \times n with orthonormal columns forming a basis of Col⁡A\operatorname{Col} A, and RR is an n×nn \times n upper triangular invertible matrix with positive diagonal entries.

To build it, apply Gram-Schmidt to the columns of AA and normalize to get the columns of QQ. Then, since QTQ=IQ^TQ = I, multiplying A=QRA = QR on the left by QTQ^T gives

R=QTA.R = Q^TA.

RR is upper triangular because, by the span property, column kk of AA is a combination of q1,…,qk\mathbf{q}_1, \ldots, \mathbf{q}_k only, so its inner products with qk+1,qk+2,…\mathbf{q}_{k+1}, \mathbf{q}_{k+2}, \ldots are all 00.

Worked example: A QR factorization

Find a QR factorization of A=[142522]A = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 2 & 2 \end{bmatrix}.

The columns are x1\mathbf{x}_1 and x2\mathbf{x}_2 from the first example, so

Q=13[12212−2].Q = \frac{1}{3}\begin{bmatrix} 1 & 2 \\ 2 & 1 \\ 2 & -2 \end{bmatrix}.

Then

R=QTA=13[12221−2][142522]=13[91809]=[3603].R = Q^TA = \frac{1}{3}\begin{bmatrix} 1 & 2 & 2 \\ 2 & 1 & -2 \end{bmatrix}\begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 2 & 2 \end{bmatrix} = \frac{1}{3}\begin{bmatrix} 9 & 18 \\ 0 & 9 \end{bmatrix} = \begin{bmatrix} 3 & 6 \\ 0 & 3 \end{bmatrix}.

Check: column 2 of QRQR is 6q1+3q2=(2,4,4)+(2,1,−2)=(4,5,2)6\mathbf{q}_1 + 3\mathbf{q}_2 = (2, 4, 4) + (2, 1, -2) = (4, 5, 2), which is column 2 of AA.

The diagonal entries of RR have a meaning: rkk=∥vk∥r_{kk} = \|\mathbf{v}_k\|, the length of the new direction found at step kk. Computers use QR factorizations to solve least-squares problems stably, as you'll see in the next lesson.

Practice

Practice 1

Apply Gram-Schmidt to x1=(2,1,2)\mathbf{x}_1 = (2, 1, 2) and x2=(4,−1,1)\mathbf{x}_2 = (4, -1, 1). Find v2\mathbf{v}_2.

Enter a point like (2, -3)

Practice 2

Apply Gram-Schmidt to x1=(1,−1,1)\mathbf{x}_1 = (1, -1, 1) and x2=(4,0,2)\mathbf{x}_2 = (4, 0, 2), then normalize. Find the second orthonormal vector q2\mathbf{q}_2. Give exact values (you may type sqrt).

Enter a point like (2, -3)

Practice 3

Apply Gram-Schmidt to x1=(3,4,0)\mathbf{x}_1 = (3, 4, 0) and x2=(1,1,1)\mathbf{x}_2 = (1, 1, 1). Rescale v2\mathbf{v}_2 to clear fractions: enter the multiple of v2\mathbf{v}_2 whose third entry is 2525.

Enter a point like (2, -3)

Practice 4

Apply Gram-Schmidt to x1=(1,1,1,1)\mathbf{x}_1 = (1, 1, 1, 1), x2=(2,0,2,0)\mathbf{x}_2 = (2, 0, 2, 0), x3=(4,2,2,0)\mathbf{x}_3 = (4, 2, 2, 0). Find v3\mathbf{v}_3.

Enter a point like (2, -3)

Practice 5

Let A=[11252−1]A = \begin{bmatrix} 1 & 1 \\ 2 & 5 \\ 2 & -1 \end{bmatrix} and A=QRA = QR be its QR factorization. Find the entry r22r_{22} of RR as an exact value.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

You apply Gram-Schmidt to x1,x2,x3\mathbf{x}_1, \mathbf{x}_2, \mathbf{x}_3 and find v3=0\mathbf{v}_3 = \mathbf{0}. What does that tell you?

Practice 7

AA is a 5×35 \times 3 matrix with linearly independent columns and A=QRA = QR. Which statement is true?