Lecture 47 · Week 12· 19:12

Gram–Schmidt and QR

Orthonormal bases, QR factorisation.

Play: Lecture 47: Gram–Schmidt and QR
Lecture · 19:12 · Open on YouTube · Report a problem with this video
TranscriptThe full text of the lecture. Click any sentence to play the video from there.

Today: Gram–Schmidt and QR

Welcome back. Last lecture, we projected vectors onto a subspace U. When U has an orthogonal basis, w one up to w k, the projection of v is a sum of k simple terms, one for each basis vector. Each term is the inner product of v with w j, divided by the inner product of w j with itself, times w j. There is no system of equations to solve. But this needs an orthogonal basis. So today's questions are these. Does every finite dimensional inner product space have one? And if so, how do we find it?

The answer is an algorithm, the Gram Schmidt process. It turns any basis into an orthogonal one, one vector at a time. Written with matrices, it becomes a factorisation: A equals Q R. Here is the plan. First, orthonormal bases, and why they are so convenient. Then the process, first in the plane, then in general, with its proof. Then an example in R three. And finally the Q R factorisation, and what it is good for.

Orthonormal bases

Recall from last lecture: a list of vectors is orthonormal if every vector in it has norm one, and any two different vectors in it are orthogonal. In symbols, the inner product of q j with q k is one when j equals k, and zero when j is not k. An orthonormal list that is also a basis of V is called an orthonormal basis of V. The standard basis of R n, with the dot product, is the first example: the dot product of e j with e k is the j th entry of e k. Here is a less obvious one in the plane: q one is the vector (three fifths, four fifths), and q two is the vector (minus four fifths, three fifths).

Each has norm one: for both of them, the squared norm is nine twenty fifths plus sixteen twenty fifths, which is one. And their dot product is minus twelve twenty fifths, plus twelve twenty fifths, which is zero. Why are such bases convenient? Because coordinates become inner products. Take v equal to the vector (two, one). With q one, the dot product gives six fifths plus four fifths, which is two. With q two, it gives minus eight fifths plus three fifths, which is minus one.

So the coordinates of v in this basis are two and minus one: v is two q one, minus q two. Check it one coordinate at a time. Six fifths plus four fifths is two, and eight fifths minus three fifths is one. That is v, and we never solved a system of equations. Here is the general statement. Let q one up to q n be an orthonormal basis of an inner product space V, and let v be in V. Then v is the sum of the inner products of v with q j, times q j. And the norm of v, squared, is the sum of the squared absolute values of these inner products. The proof is short. Since the q's form a basis, v is c one q one, plus up to c n q n. Take the inner product with q k. By linearity in the first slot, every term with j different from k is zero, and the term with j equal to k leaves c k times one. So c k is the inner product of v with q k.

For the norm, expand the inner product of v with itself in both slots. Only the terms with j equal to k survive, and each gives c j times its complex conjugate, which is the absolute value of c j, squared. The same computation shows that an orthonormal list is always linearly independent. If a combination of q one up to q m is zero, its inner product with q k gives c k equals zero. And a list of independent vectors, as many as the dimension of V, is a basis. So in the plane, q one and q two form an orthonormal basis.

The idea in the plane

Now, how do we build such a basis? Start in the plane with two vectors that are independent, but not orthogonal. v one is the vector (three, one), and v two is the vector (one, two). Their dot product is three plus two, which is five, not zero. Keep the first vector: w one is v one. Then split v two into two parts: its projection onto the line of w one, and what is left over. The projection is the inner product of v two with w one, divided by the inner product of w one with itself, times w one. Here that is five over ten, which is one half, times w one. That is the vector (three halves, one half).

What is left over is w two: v two, minus the projection. In the first coordinate, one minus three halves is minus one half. In the second, two minus one half is three halves. So w two is the vector (minus one half, three halves). Check: the dot product of w two with w one gives minus three halves, plus three halves, which is zero. So w two is orthogonal to w one. It is the part of v two that points away from the line of w one. And w one and w two span the same plane as v one and v two, because w two is v two minus one half of v one. Nothing is lost: we only straightened the second vector.

Finally, divide each vector by its norm. w one has norm root ten, and w two has norm root ten, over two. So q one is the vector (three, one), divided by root ten, and q two is the vector (minus one, three), divided by root ten. They form an orthonormal basis of the plane.

The Gram–Schmidt process

In general, we repeat this step. Each new vector loses its projections onto all the vectors built so far. Here is the theorem. Let v one up to v n be a linearly independent list in an inner product space V. Put w one equal to v one. Then, for k from two to n, let w k be v k, minus the sum, over j less than k, of the inner product of v k with w j, divided by the inner product of w j with itself, times w j. Then w one up to w n are nonzero and pairwise orthogonal. And for every k, the first k of them span the same subspace as v one up to v k.

By last lecture's formula, the sum that we subtract is the projection of v k onto the span of the earlier w's. So w k is the part of v k that is orthogonal to everything built so far. This proof is the heart of the lecture, and it is demanding, so take it slowly, and pause whenever you need to. We argue by induction on k. For k equal to one, w one is v one, which is not zero, because a list that contains the zero vector is dependent. And both lists span the same line.

Now let k be at least two, and suppose that w one up to w k minus one are nonzero, pairwise orthogonal, and span the same subspace as v one up to v k minus one. Call that subspace U. Since each of these w j is nonzero, the divisions in the formula make sense. First, w k is not zero. The sum that we subtract lies in U. So if w k were zero, v k would equal that sum, and lie in U, the span of v one up to v k minus one. That contradicts the independence of the list. Next, w k is orthogonal to each earlier w l. Take the inner product of w k with w l. By linearity in the first slot, it is the inner product of v k with w l, minus the sum of each coefficient times the inner product of w j with w l.

In that sum, only the term with j equal to l survives, because the earlier w's are pairwise orthogonal. In that term, the inner product of w l with itself cancels against the denominator, and what is left is the inner product of v k with w l. So the result is zero. Finally, the spans. The formula says that w k is v k minus a vector in U. Read backwards, v k is w k plus a vector in U. So adding w k to U gives the same span as adding v k. That completes the induction. Two consequences. First, every finite dimensional inner product space, other than the zero space, has an orthonormal basis. Apply the process to any basis, then divide each w k by its norm. The result is orthonormal, with as many vectors as the dimension, so it is a basis.

Second, every orthonormal list can be extended to an orthonormal basis. It is independent, so extend it to a basis, and apply the process. On the first vectors every coefficient is zero, so the process leaves them as they are, already of norm one. One remark. Normalising does not have to wait until the end. You can divide each w k by its norm as soon as it is built, and continue with the unit vectors. Then the formula becomes: w k equals v k, minus the sum of the inner products of v k with q j, times q j. Each term is the same projection as before, because q j is w j divided by its norm. So the orthonormal basis is exactly the same.

An example in R3

Let's run the process in R three, with the dot product. Take v one equal to the vector (one, two, two), v two equal to the vector (three, three, zero), and v three equal to the vector (one, minus one, five). They are independent. The matrix with these columns has determinant one times fifteen, minus three times twelve, plus one times minus six, which is minus twenty seven, not zero. Step one: w one is v one. Its inner product with itself is one plus four plus four, which is nine.

Step two. The dot product of v two with w one gives three plus six plus zero, which is nine. So the coefficient is nine over nine, which is one, and w two is v two minus w one. In the first coordinate, three minus one is two. In the second, three minus two is one. In the third, zero minus two is minus two. So w two is the vector (two, one, minus two), and its inner product with itself is four plus one plus four, which is nine. Step three. The dot product of v three with w one gives one minus two plus ten, which is nine. With w two, it gives two minus one minus ten, which is minus nine. So the coefficients are nine over nine, which is one, and minus nine over nine, which is minus one.

So we subtract one times w one, and minus one times w two. That makes w three equal to v three, minus w one, plus w two. In the first coordinate, one minus one plus two is two. In the second, minus one minus two plus one is minus two. In the third, five minus two minus two is one. So w three is the vector (two, minus two, one). Check: the dot product of w three with w one gives two minus four plus two, which is zero. With w two, it gives four minus two minus two, which is zero too. And w two with w one gives two plus two minus four, which is zero.

In a picture: w one and w two span a plane U. What we subtracted from v three, w one minus w two, is its projection onto that plane. What is left, w three, sticks straight out of the plane. All three vectors have inner product nine with themselves, so each has norm three. Dividing each of them by three gives the vectors q one, q two and q three, an orthonormal basis of R three.

The QR factorisation

Now let's write the process with matrices. From here on, matrices are real, and R m has the dot product. First, a test for orthonormal columns. Let Q be a real m by n matrix with columns q one up to q n. The columns are orthonormal exactly when Q transpose Q is the identity matrix. Here is why. The entry in row j and column k of Q transpose Q is row j of Q transpose, times column k of Q. That is q j transpose times q k, the dot product of q j with q k. These dot products are one on the diagonal and zero off it exactly when the columns are orthonormal.

Now the theorem. Let A be a real m by n matrix whose columns, a one up to a n, are linearly independent. Then A equals Q R, where Q is an m by n matrix with orthonormal columns, and R is an n by n upper triangular matrix with positive diagonal entries. Here is the proof. Apply the Gram Schmidt process to the columns of A, and normalise, to get q one up to q n. These are the columns of Q. Fix k. The column a k lies in the span of a one up to a k, which is the span of q one up to q k. So, by the coordinate formula in that span, a k is the sum, over j up to k, of the inner product of a k with q j, times q j.

So let r j k be the inner product of a k with q j when j is at most k, and zero when j is greater than k. Then R is upper triangular, and column k of Q R is the sum of r j k times q j, which is a k. So A equals Q R. The diagonal entries are positive. In the formula for w k, a k is w k plus a combination of earlier w's, which are orthogonal to q k. So r k k, the inner product of a k with q k, equals the inner product of w k with q k, which is the norm of w k. And since Q transpose Q is the identity, multiplying A equals Q R on the left by Q transpose gives R directly: R equals Q transpose A.

For our example, A is the matrix with the columns v one, v two and v three. Its Q has the columns q one, q two and q three: one third, times the matrix with rows one, two, two, then two, one, minus two, and two, minus two, one. And R has the rows three, three, three, then zero, three, minus three, and zero, zero, three. Here is why. Read each step of the example backwards, and write each w as three times its q. Then v one is three q one. v two is w two plus w one, which is three q one plus three q two. And v three is w three, plus w one, minus w two, which is three q one, minus three q two, plus three q three. These coefficients are the columns of R. On the diagonal are the norms of the w's, and above it, three times the coefficients of the example. Check one entry. The first row of Q, times the third column of R, is one third, times one times three, plus two times minus three, plus two times three. That is one third of three, which is one: the top entry of the third column of A.

Try it yourself

You can try this yourself, on this lecture's page at Open Semester dot com. You drag the columns v one and v two of a two by two matrix A, and step through the process. Here is our example from the plane. Step one keeps v one as w one. Step two finds the projection of v two onto w one, which is half of w one. What is left is w two, at right angles to w one. Then both are divided by their norms. That gives q one and q two, the columns of Q. Beside the picture, R is upper triangular, with the two norms on its diagonal.

Go back to what is left, and drag v two towards the line of v one. The projection grows and w two gets shorter. When v two lies on the line, w two is zero, and the columns are dependent. Finally, start from v two instead. The process gives a different orthonormal basis, so the order of the vectors matters. Try your own columns, and watch Q and R change.

Solving with QR, and a summary

What is Q R good for? When A is square and invertible, Q is square too, and Q transpose Q is the identity, so Q transpose is the inverse of Q. Then A x equals b becomes R x equals Q transpose b, an upper triangular system. For b equal to the vector (two, one, seven), Q transpose b is one third of, two plus two plus fourteen, then four plus one minus fourteen, and four minus two plus seven. That is the vector (six, minus three, three). Now back substitution, from the bottom row up. The third row says that three x three equals three, so x three must be one. The second row says that three x two, minus three, equals minus three, so x two must be zero. And the first row says that three x one, plus zero, plus three, equals six, so x one must be one.

So x is the vector (one, zero, one). Check: A times this vector is v one plus v three, which is the vector (two, one, seven). That is b. When A is taller than it is wide, the system A x equals b usually has no solution. But R x equals Q transpose b still has exactly one, and it is the best approximate solution. That is the method of least squares, the subject of the next lecture. Let's sum up. In an orthonormal basis, coordinates are inner products. The Gram Schmidt process turns any independent list into an orthogonal one with the same spans, by subtracting from each vector its projections onto the earlier ones.

In matrix form, it is A equals Q R: orthonormal columns, times an upper triangular matrix with positive diagonal. Square matrices with orthonormal columns, the orthogonal matrices, return in lecture forty nine, with the spectral theorem. Before the next lecture, read section ten point three of the course book, try the problems on sheet twelve about Gram Schmidt and Q R, and do the lecture check on this lecture's page at Open Semester dot com. Next time, we fit lines to data, with least squares. See you there.

Try it

Gram–Schmidt in the plane

the columns of A in ℝ²
Examples
Order

Drag the tips of v1 and v2, the columns of A, and step through the process: w1 = v1, then v2 minus its projection onto w1 gives w2, and dividing by the norms gives q1 and q2. Beside the picture, Q has the columns q1, q2, and R = QTA is upper triangular with ‖w1‖ and ‖w2‖ on its diagonal. Swap the order to see that starting from v2 gives a different orthonormal basis.

Check yourself

3 questions, from warm-up to stretch

Work each one out on paper, type your answer and press Check. Stuck? Open a hint; the worked solution is there when you need it.

  1. Question 1 of 3 Warm-up

    Apply the Gram–Schmidt process to and in , with the dot product. Find .

    Hint 1 of 2

    Step 1 is . Step 2 subtracts from its projection onto : (Theorem 10.3.6).

    Hint 2 of 2

    and , so the coefficient is . Now subtract from , entry by entry.

    Worked solution
    1. , with .
    2. , so the coefficient is and .
    3. Check: , so is an orthogonal basis of .
  2. Question 2 of 3 Core

    Apply the Gram–Schmidt process to , and in , with the dot product. Find the coefficient in step 3, and .

    Hint 1 of 2

    Find first: , and , , so .

    Hint 2 of 2

    , with . In step 3, subtract from its projections onto both and : .

    Worked solution
    1. , with .
    2. , so the coefficient is and , with .
    3. and , so the coefficients are and .
    4. .
    5. Check: and .
  3. Question 3 of 3 Stretch

    Let , with as in Theorem 10.3.12. Find the entries and of . Then solve for with .

    Hint 1 of 2

    Gram–Schmidt on the columns: has norm , so and . Above the diagonal, .

    Hint 2 of 2

    , and . Then and . For the system, compute and solve from the bottom row up.

    Worked solution
    1. , , so and .
    2. , and , so and .
    3. So and . Check: the first row of times the second column of is , the entry of .
    4. . Row 2 of says , so ; row 1 says , so .
    5. Check: .

Found a mistake, or something not working on this page? Report it and we'll fix it. See what we've corrected.