Unit 6, lesson 32

Gram-Schmidt and QR Factorization

How Gram-Schmidt turns independent vectors into orthonormal ones with the same span, how its unit vectors and coefficients form a reduced QR factorization with an upper triangular factor, and what a zero remainder reveals.

  • Video 32
  • Orthonormal Bases and QR

The vectors (1,0,1)(1, 0, 1) and (0,1,1)(0, 1, 1) span a plane in R3\mathbb{R}^3, but they meet at an angle of 60∘60^{\circ}. Can the same plane be described by two perpendicular unit vectors? Rescaling each vector to length one keeps the 60∘60^{\circ} angle, so something else is needed. The Gram-Schmidt process removes from each vector the part already explained by the earlier directions and normalizes what is left. The new unit vectors and the coefficients of the process then give the factorization A=QRA = QR. Throughout, a tuple such as (1,0,1)(1, 0, 1) stands for a column vector, and all vectors are real with the ordinary dot product.

Key terms

Gram-Schmidt process

Definition Let a1,…,an\mathbf{a}_1, \dots, \mathbf{a}_n be linearly independent vectors in Rm\mathbb{R}^m, so that n≤mn \le m. The Gram-Schmidt process builds vectors q1,…,qn\mathbf{q}_1, \dots, \mathbf{q}_n in order. For j=1,…,nj = 1, \dots, n,

uj=aj−∑i=1j−1(qi⋅aj) qi,qj=uj∥uj∥,\begin{aligned} \mathbf{u}_j &= \mathbf{a}_j - \sum_{i=1}^{j-1} (\mathbf{q}_i \cdot \mathbf{a}_j)\,\mathbf{q}_i, \\[0.4em] \mathbf{q}_j &= \frac{\mathbf{u}_j}{\lVert \mathbf{u}_j \rVert}, \end{aligned}

where the sum is empty for j=1j = 1, so u1=a1\mathbf{u}_1 = \mathbf{a}_1. Every uj\mathbf{u}_j is nonzero, the list q1,…,qn\mathbf{q}_1, \dots, \mathbf{q}_n is orthonormal, and for every jj

span⁡{q1,…,qj}=span⁡{a1,…,aj}.\begin{aligned} &\operatorname{span}\{\mathbf{q}_1, \dots, \mathbf{q}_j\} \\[0.4em] &\quad = \operatorname{span}\{\mathbf{a}_1, \dots, \mathbf{a}_j\}. \end{aligned}

The proof is by induction on jj. In the base case j=1j = 1, the vector a1\mathbf{a}_1 is nonzero because the list is independent, so q1\mathbf{q}_1 is a unit vector spanning the same line. For the step, assume that q1,…,qk\mathbf{q}_1, \dots, \mathbf{q}_k are orthonormal and span the same space as a1,…,ak\mathbf{a}_1, \dots, \mathbf{a}_k. The next remainder uk+1\mathbf{u}_{k+1} is nonzero and perpendicular to each earlier qi\mathbf{q}_i, as the next card shows, so qk+1\mathbf{q}_{k+1} extends the orthonormal list. The formula puts qk+1\mathbf{q}_{k+1} in the span of a1,…,ak+1\mathbf{a}_1, \dots, \mathbf{a}_{k+1}, since each earlier qi\mathbf{q}_i lies in the span of a1,…,ak\mathbf{a}_1, \dots, \mathbf{a}_k, and rearranged it writes ak+1\mathbf{a}_{k+1} as a combination of q1,…,qk+1\mathbf{q}_1, \dots, \mathbf{q}_{k+1}, so the two spans agree at step k+1k + 1.

In plain words Take the vectors in order. From each one, subtract the parts that point along the directions you already have, then rescale what remains to length one. The results are perpendicular unit vectors, and after each step they span the same space as the original vectors used so far.

Example Let a1=(1,0,1)\mathbf{a}_1 = (1, 0, 1) and a2=(0,1,1)\mathbf{a}_2 = (0, 1, 1). The first vector has length 2\sqrt{2}, so q1=12(1,0,1)\mathbf{q}_1 = \frac{1}{\sqrt{2}}(1, 0, 1). Next, q1⋅a2=1/2\mathbf{q}_1 \cdot \mathbf{a}_2 = 1/\sqrt{2}, so the projection of a2\mathbf{a}_2 onto q1\mathbf{q}_1 is 12(1,0,1)\tfrac12(1, 0, 1) and

u2=[011]−[1/201/2]=[−1/211/2].\mathbf{u}_2 = \begin{bmatrix} 0 \\ 1 \\ 1 \end{bmatrix} - \begin{bmatrix} 1/2 \\ 0 \\ 1/2 \end{bmatrix} = \begin{bmatrix} -1/2 \\ 1 \\ 1/2 \end{bmatrix}.

Its length is 3/2=6/2\sqrt{3/2} = \sqrt{6}/2, and dividing gives q2=16(−1,2,1)\mathbf{q}_2 = \frac{1}{\sqrt{6}}(-1, 2, 1). Both new vectors satisfy z=x+yz = x + y, so they lie in the original plane, and the numerators of q1⋅q2\mathbf{q}_1 \cdot \mathbf{q}_2 give −1+0+1=0-1 + 0 + 1 = 0.

Orthogonal remainder

Definition At step jj of the Gram-Schmidt process the orthogonal remainder is the vector uj\mathbf{u}_j defined above, the part of aj\mathbf{a}_j left after subtracting its projections onto all earlier directions q1,…,qj−1\mathbf{q}_1, \dots, \mathbf{q}_{j-1}. The subtracted sum is the orthogonal projection of aj\mathbf{a}_j onto span⁡{q1,…,qj−1}\operatorname{span}\{\mathbf{q}_1, \dots, \mathbf{q}_{j-1}\}, so uj\mathbf{u}_j is perpendicular to that whole span. Directly, since qk⋅qi\mathbf{q}_k \cdot \mathbf{q}_i is 11 for i=ki = k and 00 for i≠ki \ne k, every k<jk < j gives

qk⋅uj=qk⋅aj−qk⋅aj=0.\mathbf{q}_k \cdot \mathbf{u}_j = \mathbf{q}_k \cdot \mathbf{a}_j - \mathbf{q}_k \cdot \mathbf{a}_j = 0.

For independent inputs the remainder is never zero, because uj=0\mathbf{u}_j = \mathbf{0} would put aj\mathbf{a}_j in the span of the earlier qi\mathbf{q}_i, which is the span of a1,…,aj−1\mathbf{a}_1, \dots, \mathbf{a}_{j-1}.

In plain words The remainder is what is genuinely new in the next vector. Every earlier direction has to be removed, not only the most recent one.

Example Add a3=(1,1,0)\mathbf{a}_3 = (1, 1, 0) to the two vectors above. Its dot products with q1\mathbf{q}_1 and q2\mathbf{q}_2 are 1/21/\sqrt{2} and 1/61/\sqrt{6}, so the two projections are (12,0,12)(\tfrac12, 0, \tfrac12) and (−16,13,16)(-\tfrac16, \tfrac13, \tfrac16), with sum (13,13,23)(\tfrac13, \tfrac13, \tfrac23). Then

u3=[110]−[1/31/32/3]=[2/32/3−2/3],\mathbf{u}_3 = \begin{bmatrix} 1 \\ 1 \\ 0 \end{bmatrix} - \begin{bmatrix} 1/3 \\ 1/3 \\ 2/3 \end{bmatrix} = \begin{bmatrix} 2/3 \\ 2/3 \\ -2/3 \end{bmatrix},

which has zero dot product with both (1,0,1)(1, 0, 1) and (−1,2,1)(-1, 2, 1). Subtracting only the first projection would leave (12,1,−12)(\tfrac12, 1, -\tfrac12), whose dot product with (−1,2,1)(-1, 2, 1) is 11, so a component along q2\mathbf{q}_2 would remain.

Normalization

Definition Normalizing a nonzero vector u\mathbf{u} means dividing it by its length,

q=u∥u∥.\mathbf{q} = \frac{\mathbf{u}}{\lVert \mathbf{u} \rVert}.

The result has length 11, spans the same line as u\mathbf{u}, and gives back u=∥u∥ q\mathbf{u} = \lVert \mathbf{u} \rVert\,\mathbf{q}. Normalization is defined only for u≠0\mathbf{u} \ne \mathbf{0}. Scaling by a positive number keeps every zero dot product zero, so a normalized remainder is still perpendicular to the earlier directions. Normalization changes lengths only and never angles, so it cannot make two vectors perpendicular when they were not.

In plain words Subtracting projections makes the new vector perpendicular, and normalizing then makes it a unit vector. These are two separate jobs, done in that order.

Example The remainder u3=(23,23,−23)\mathbf{u}_3 = (\tfrac23, \tfrac23, -\tfrac23) has ∥u3∥2=43\lVert \mathbf{u}_3 \rVert^2 = \tfrac43, so ∥u3∥=2/3\lVert \mathbf{u}_3 \rVert = 2/\sqrt{3} and q3=13(1,1,−1)\mathbf{q}_3 = \frac{1}{\sqrt{3}}(1, 1, -1). Normalizing the original vectors directly does not help. The unit vectors 12(1,0,1)\frac{1}{\sqrt{2}}(1, 0, 1) and 12(0,1,1)\frac{1}{\sqrt{2}}(0, 1, 1) have dot product 12\tfrac12, the cosine of the same 60∘60^{\circ} angle.

Matrix with orthonormal columns

Definition A real m×nm \times n matrix QQ has orthonormal columns if its columns q1,…,qn\mathbf{q}_1, \dots, \mathbf{q}_n satisfy qi⋅qj=1\mathbf{q}_i \cdot \mathbf{q}_j = 1 for i=ji = j and qi⋅qj=0\mathbf{q}_i \cdot \mathbf{q}_j = 0 for i≠ji \ne j. Entry (i,j)(i, j) of Q⊤QQ^{\top}Q is qi⋅qj\mathbf{q}_i \cdot \mathbf{q}_j, so the condition is

Q⊤Q=In.Q^{\top}Q = I_n.

Such a matrix has n≤mn \le m and preserves dot products and lengths, since (Qx)⋅(Qy)=x⊤Q⊤Qy=x⋅y(Q\mathbf{x}) \cdot (Q\mathbf{y}) = \mathbf{x}^{\top}Q^{\top}Q\mathbf{y} = \mathbf{x} \cdot \mathbf{y}. The other product QQ⊤QQ^{\top} is the m×mm \times m matrix of the orthogonal projection onto Col⁡Q\operatorname{Col} Q. It equals ImI_m only when m=nm = n, and then QQ is an orthogonal matrix with Q−1=Q⊤Q^{-1} = Q^{\top}.

In plain words The columns act as perpendicular unit rulers, and Q⊤Q^{\top} measures a vector along each of them. When there are fewer columns than rows, the rulers cover only a subspace, so QQ⊤QQ^{\top} keeps the part of a vector in that subspace and discards the rest.

Example The vectors found above form

Q=[1/2−1/602/61/21/6].Q = \begin{bmatrix} 1/\sqrt{2} & -1/\sqrt{6} \\ 0 & 2/\sqrt{6} \\ 1/\sqrt{2} & 1/\sqrt{6} \end{bmatrix}.

Its columns have squared lengths 1+0+12=1\tfrac{1 + 0 + 1}{2} = 1 and 1+4+16=1\tfrac{1 + 4 + 1}{6} = 1 and dot product 00, so Q⊤Q=I2Q^{\top}Q = I_2. But QQ⊤≠I3QQ^{\top} \ne I_3. The vector (−1,−1,1)(-1, -1, 1) is perpendicular to both columns, so Q⊤Q^{\top} sends it to 0\mathbf{0}, and so does QQ⊤QQ^{\top}. In fact

QQ⊤=13[2−11−121112],QQ^{\top} = \frac{1}{3}\begin{bmatrix} 2 & -1 & 1 \\ -1 & 2 & 1 \\ 1 & 1 & 2 \end{bmatrix},

the projection matrix onto the plane z=x+yz = x + y.

Reduced QR factorization

Definition Let AA be a real m×nm \times n matrix with linearly independent columns a1,…,an\mathbf{a}_1, \dots, \mathbf{a}_n. A reduced QR factorization of AA is a product A=QRA = QR in which QQ is an m×nm \times n matrix with orthonormal columns and the upper triangular factor RR is an n×nn \times n matrix with rij=0r_{ij} = 0 for i>ji > j. Here the diagonal entries of RR are also required to be positive, as Gram-Schmidt makes them. Gram-Schmidt on the columns of AA produces such a factorization. The columns of QQ are q1,…,qn\mathbf{q}_1, \dots, \mathbf{q}_n, and

rij=qi⋅ajfor i<j,rjj=∥uj∥>0,rij=0for i>j.\begin{aligned} r_{ij} &= \mathbf{q}_i \cdot \mathbf{a}_j \quad \text{for } i < j, \\[0.4em] r_{jj} &= \lVert \mathbf{u}_j \rVert > 0, \\[0.4em] r_{ij} &= 0 \quad \text{for } i > j. \end{aligned}

Rearranging the Gram-Schmidt formula gives aj=r1jq1+⋯+rjjqj\mathbf{a}_j = r_{1j}\mathbf{q}_1 + \cdots + r_{jj}\mathbf{q}_j, which is column jj of QRQR. The zeros below the diagonal say that aj\mathbf{a}_j needs only the first jj directions. Dotting this equation with qi\mathbf{q}_i gives rij=qi⋅ajr_{ij} = \mathbf{q}_i \cdot \mathbf{a}_j for every ii, including rjj=qj⋅ajr_{jj} = \mathbf{q}_j \cdot \mathbf{a}_j and the zeros, so all entries can be collected as R=Q⊤AR = Q^{\top}A. With positive diagonal entries, QQ and RR are unique. Without that convention, changing the sign of a column of QQ together with the matching row of RR gives another factorization. The word reduced means that QQ has only the nn columns needed for Col⁡A\operatorname{Col} A, even when m>nm > n.

In plain words The matrix QQ holds the new perpendicular unit directions, and RR records how to rebuild each original column from them. The triangle of zeros reflects the order of the process, since a column never needs a direction that was built after it.

Example For the matrix with columns (1,0,1)(1, 0, 1) and (0,1,1)(0, 1, 1), the Gram-Schmidt example gives r11=2r_{11} = \sqrt{2}, r12=q1⋅a2=1/2r_{12} = \mathbf{q}_1 \cdot \mathbf{a}_2 = 1/\sqrt{2} and r22=∥u2∥=6/2r_{22} = \lVert \mathbf{u}_2 \rVert = \sqrt{6}/2. With QQ from the previous card,

[100111]=QR,R=[21/206/2].\begin{gathered} \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{bmatrix} = QR, \\[0.4em] R = \begin{bmatrix} \sqrt{2} & 1/\sqrt{2} \\ 0 & \sqrt{6}/2 \end{bmatrix}. \end{gathered}

The first column checks out because 2 q1=(1,0,1)\sqrt{2}\,\mathbf{q}_1 = (1, 0, 1), and the second because

12q1+62q2=[1/201/2]+[−1/211/2]=[011].\begin{aligned} &\tfrac{1}{\sqrt{2}}\mathbf{q}_1 + \tfrac{\sqrt{6}}{2}\mathbf{q}_2 \\[0.4em] &\quad = \begin{bmatrix} 1/2 \\ 0 \\ 1/2 \end{bmatrix} + \begin{bmatrix} -1/2 \\ 1 \\ 1/2 \end{bmatrix} = \begin{bmatrix} 0 \\ 1 \\ 1 \end{bmatrix}. \end{aligned}

Zero remainder and dependence

Definition Let q1,…,qj−1\mathbf{q}_1, \dots, \mathbf{q}_{j-1} be orthonormal with the same span as a1,…,aj−1\mathbf{a}_1, \dots, \mathbf{a}_{j-1}, and form the remainder uj\mathbf{u}_j as above. Then uj=0\mathbf{u}_j = \mathbf{0} exactly when aj\mathbf{a}_j lies in span⁡{a1,…,aj−1}\operatorname{span}\{\mathbf{a}_1, \dots, \mathbf{a}_{j-1}\}. If uj=0\mathbf{u}_j = \mathbf{0}, the formula writes aj\mathbf{a}_j as a combination of the earlier qi\mathbf{q}_i. Conversely, if aj\mathbf{a}_j lies in that span, it equals the sum of its components (qi⋅aj)qi(\mathbf{q}_i \cdot \mathbf{a}_j)\mathbf{q}_i, so nothing remains. In exact arithmetic Gram-Schmidt is therefore a test for dependence. A zero remainder cannot be normalized, and the reduced QR factorization above needs every diagonal entry rjj=∥uj∥r_{jj} = \lVert \mathbf{u}_j \rVert to be positive. To get an orthonormal basis for the span of a dependent list, skip each zero remainder and continue with the next vector. The number of directions kept equals the rank. In floating-point arithmetic, rounding makes a very small computed remainder hard to interpret, so a tiny value is not by itself proof of dependence.

In plain words A zero remainder means the vector brought no new direction, because the earlier vectors already explain it completely.

Example Replace a2\mathbf{a}_2 by 2a1=(2,0,2)2\mathbf{a}_1 = (2, 0, 2). Then q1⋅(2,0,2)=4/2=22\mathbf{q}_1 \cdot (2, 0, 2) = 4/\sqrt{2} = 2\sqrt{2}, and the projection 22 q1=(2,0,2)2\sqrt{2}\,\mathbf{q}_1 = (2, 0, 2) is the whole vector, so the remainder is 0\mathbf{0}. For the list (1,0,1)(1, 0, 1), (2,0,2)(2, 0, 2), (0,1,1)(0, 1, 1), skipping that zero remainder and continuing gives q1\mathbf{q}_1 and then the same q2=16(−1,2,1)\mathbf{q}_2 = \frac{1}{\sqrt{6}}(-1, 2, 1) as before, so the list has rank 22.

Check yourself

  1. Find the reduced QR factorization of the matrix below.
A=[3140]A = \begin{bmatrix} 3 & 1 \\ 4 & 0 \end{bmatrix}
Show the answer

The first column has length 55, so r11=5r_{11} = 5 and q1=(35,45)\mathbf{q}_1 = (\tfrac35, \tfrac45). Next r12=q1⋅(1,0)=35r_{12} = \mathbf{q}_1 \cdot (1, 0) = \tfrac35, and

u2=[10]−35[3/54/5]=[16/25−12/25].\mathbf{u}_2 = \begin{bmatrix} 1 \\ 0 \end{bmatrix} - \frac{3}{5}\begin{bmatrix} 3/5 \\ 4/5 \end{bmatrix} = \begin{bmatrix} 16/25 \\ -12/25 \end{bmatrix}.

Its length is 45\tfrac45, so r22=45r_{22} = \tfrac45 and q2=(45,−35)\mathbf{q}_2 = (\tfrac45, -\tfrac35). Therefore

Q=[3/54/54/5−3/5],R=[53/504/5].\begin{gathered} Q = \begin{bmatrix} 3/5 & 4/5 \\ 4/5 & -3/5 \end{bmatrix}, \\[0.4em] R = \begin{bmatrix} 5 & 3/5 \\ 0 & 4/5 \end{bmatrix}. \end{gathered}

The columns of QQ are unit vectors with dot product 1225−1225=0\tfrac{12}{25} - \tfrac{12}{25} = 0, and the second column of QRQR is 35q1+45q2=(925+1625,1225−1225)=(1,0)\tfrac35\mathbf{q}_1 + \tfrac45\mathbf{q}_2 = (\tfrac{9}{25} + \tfrac{16}{25}, \tfrac{12}{25} - \tfrac{12}{25}) = (1, 0). Here m=nm = n, so QQ is an orthogonal matrix.

  1. Apply Gram-Schmidt to a1=(1,2,2)\mathbf{a}_1 = (1, 2, 2) and a2=(1,0,4)\mathbf{a}_2 = (1, 0, 4). Write the reduced QR factorization of the matrix AA with these columns, and check both Q⊤Q=I2Q^{\top}Q = I_2 and QR=AQR = A.
Show the answer

The first vector has length 33, so r11=3r_{11} = 3 and q1=13(1,2,2)\mathbf{q}_1 = \tfrac13(1, 2, 2). Next r12=q1⋅a2=1+0+83=3r_{12} = \mathbf{q}_1 \cdot \mathbf{a}_2 = \tfrac{1 + 0 + 8}{3} = 3, so u2=(1,0,4)−(1,2,2)=(0,−2,2)\mathbf{u}_2 = (1, 0, 4) - (1, 2, 2) = (0, -2, 2). Its length is 222\sqrt{2}, so r22=22r_{22} = 2\sqrt{2} and q2=12(0,−1,1)\mathbf{q}_2 = \frac{1}{\sqrt{2}}(0, -1, 1). Therefore

Q=[1/302/3−1/22/31/2],R=[33022].\begin{gathered} Q = \begin{bmatrix} 1/3 & 0 \\ 2/3 & -1/\sqrt{2} \\ 2/3 & 1/\sqrt{2} \end{bmatrix}, \\[0.4em] R = \begin{bmatrix} 3 & 3 \\ 0 & 2\sqrt{2} \end{bmatrix}. \end{gathered}

The columns of QQ have squared lengths 19+49+49=1\tfrac19 + \tfrac49 + \tfrac49 = 1 and 0+12+12=10 + \tfrac12 + \tfrac12 = 1, and the numerators of their dot product give 0−2+2=00 - 2 + 2 = 0, so Q⊤Q=I2Q^{\top}Q = I_2. For QRQR, the first column is 3q1=(1,2,2)3\mathbf{q}_1 = (1, 2, 2) and the second is 3q1+22 q2=(1,2,2)+(0,−2,2)=(1,0,4)3\mathbf{q}_1 + 2\sqrt{2}\,\mathbf{q}_2 = (1, 2, 2) + (0, -2, 2) = (1, 0, 4). Normalizing a1\mathbf{a}_1 and a2\mathbf{a}_2 without the subtraction would give unit vectors with dot product 9/(317)=3/179/(3\sqrt{17}) = 3/\sqrt{17}, not 00.

  1. Keep a1\mathbf{a}_1 and a2\mathbf{a}_2 from the previous question and add a3=(1,1,k)\mathbf{a}_3 = (1, 1, k). For which kk is the third remainder zero, and what dependence does that reveal? For the other values of kk, find q3\mathbf{q}_3 and r33r_{33}.
Show the answer

The dot products are q1⋅a3=3+2k3\mathbf{q}_1 \cdot \mathbf{a}_3 = \tfrac{3 + 2k}{3} and q2⋅a3=k−12\mathbf{q}_2 \cdot \mathbf{a}_3 = \tfrac{k - 1}{\sqrt{2}}. Subtracting both projections gives

u3=a3−3+2k9[122]−k−12[0−11]=k−318[−411].\begin{aligned} \mathbf{u}_3 &= \mathbf{a}_3 - \frac{3 + 2k}{9}\begin{bmatrix} 1 \\ 2 \\ 2 \end{bmatrix} \\[0.4em] &\quad - \frac{k - 1}{2}\begin{bmatrix} 0 \\ -1 \\ 1 \end{bmatrix} \\[0.4em] &= \frac{k - 3}{18}\begin{bmatrix} -4 \\ 1 \\ 1 \end{bmatrix}. \end{aligned}

The remainder is zero exactly when k=3k = 3. Then a3=(1,1,3)=12(a1+a2)\mathbf{a}_3 = (1, 1, 3) = \tfrac12(\mathbf{a}_1 + \mathbf{a}_2), so the three vectors are dependent and span only a plane, and the list has rank 22. For k≠3k \ne 3 the three vectors are independent. Since ∥(−4,1,1)∥=18=32\lVert (-4, 1, 1) \rVert = \sqrt{18} = 3\sqrt{2}, the remainder has length r33=∣k−3∣/(32)r_{33} = \lvert k - 3 \rvert / (3\sqrt{2}), and q3=±132(−4,1,1)\mathbf{q}_3 = \pm\frac{1}{3\sqrt{2}}(-4, 1, 1), with the sign of k−3k - 3. The length is positive even when k<3k < 3, and the sign goes into q3\mathbf{q}_3.

How this lesson connects

Where this leads in Linear Algebra II