Skip to article frontmatterSkip to article content

Computing QR factorizations

It is possible to compute a thin QR factorization using the outer product formula (2.4.4), as we did with LU. However, to stably compute the factorization, a better strategy is to introduce zeros into the lower triangle, one column at a time, using orthogonal matrices. Thanks to Theorem 3.3.2, the product of orthogonal matrices will also be orthogonal.

3.4.1Householder reflections

We will use a particular type of orthogonal matrix.

In Exercise 2 you are asked to show that such a P\mathbf{P} is necessarily orthogonal. Note that for any vector x\mathbf{x} of appropriate dimension,

Px=x2v(vTx). \mathbf{P}\mathbf{x} = \mathbf{x} - 2 \mathbf{v} (\mathbf{v}^T\mathbf{x}).

The reason P\mathbf{P} is called a reflector is sketched in Figure 3.4.1.

A Householder reflector. Because \mathbf{v} is a unit vector, \mathbf{v}^T\mathbf{x} is the component of \mathbf{x} in the direction of \mathbf{v}. Hence subtracting (\mathbf{v}^T\mathbf{x})\mathbf{v} projects \mathbf{x} into a hyperplane orthogonal to \mathbf{v}. By subtracting off twice as much, we get the reflection of \mathbf{x} through the hyperplane instead.

Figure 3.4.1:A Householder reflector. Because v\mathbf{v} is a unit vector, vTx\mathbf{v}^T\mathbf{x} is the component of x\mathbf{x} in the direction of v\mathbf{v}. Hence subtracting (vTx)v(\mathbf{v}^T\mathbf{x})\mathbf{v} projects x\mathbf{x} into a hyperplane orthogonal to v\mathbf{v}. By subtracting off twice as much, we get the reflection of x\mathbf{x} through the hyperplane instead.

Given a vector z\mathbf{z}, we can choose v\mathbf{v} so that P\mathbf{P} reflects z\mathbf{z} onto the x1x_1-axis—i.e., so that Pz\mathbf{P}\mathbf{z} is nonzero only in the first element. Because orthogonal matrices preserve the 2-norm, we must have

Pz=[±z00]=±ze1.\mathbf{P}\mathbf{z} = \begin{bmatrix} \pm \| \mathbf{z} \|\\0 \\ \vdots \\ 0 \end{bmatrix} = \pm \| \mathbf{z} \| \mathbf{e}_1.

(Recall that ek\mathbf{e}_k is the kkth column of the identity matrix.) We choose the positive sign above for our discussion, but see Function 3.4.1 and Exercise 4 for important computational details. Let

w=ze1z,v=ww. \mathbf{w} = \| \mathbf{z} \| \mathbf{e}_1-\mathbf{z}, \quad \mathbf{v} = \frac{\mathbf{w}}{\|\mathbf{w}\|}.

If it turns out that w=0\mathbf{w}=\boldsymbol{0}, then z\mathbf{z} is already in the target form and we can take P=I\mathbf{P}=\mathbf{I}. Otherwise, we have the following.

3.4.2Factorization algorithm

The QR factorization is computed by using successive Householder reflections to introduce zeros in one column at a time. We first show the process for a small numerical example in Demo 3.4.1.

You may be wondering what happened to Q\mathbf{Q} in Demo 3.4.1. Each Householder reflector is orthogonal but not full-size. We have to pad it out to represent algebraically the fact that a block of the first rows is left alone. Given a reflector Pk\mathbf{P}_k that is of square size mk+1m-k+1, we define

Qk=[Ik100Pk].\mathbf{Q}_k = \begin{bmatrix} \mathbf{I}_{k-1} & \boldsymbol{0} \\ \boldsymbol{0} & \mathbf{P}_k \end{bmatrix}.

It is easy to show that Qk\mathbf{Q}_k is also orthogonal. Then the algorithm produces

QnQn1Q1A=R. \mathbf{Q}_n \mathbf{Q}_{n-1}\cdots \mathbf{Q}_1 \mathbf{A} = \mathbf{R}.

But QnQn1Q1\mathbf{Q}_n \mathbf{Q}_{n-1}\cdots \mathbf{Q}_1 is orthogonal too, and we multiply on the left by its transpose to get A=QR\mathbf{A}=\mathbf{Q}\mathbf{R}, where Q=(QnQn1Q1)T\mathbf{Q} = (\mathbf{Q}_n \mathbf{Q}_{n-1}\cdots \mathbf{Q}_1)^T. We don’t even need to form these matrices explicitly. Writing

QT=QnQn1Q1=Qn(Qn1((Q1I))),\mathbf{Q}^T = \mathbf{Q}_n \mathbf{Q}_{n-1}\cdots \mathbf{Q}_1 = \mathbf{Q}_n \Bigl( \mathbf{Q}_{n-1}\bigl(\cdots (\mathbf{Q}_1\mathbf{I})\cdots\bigr)\Bigr),

we can build QT\mathbf{Q}^T iteratively by starting with the identity and doing the same row operations as on A\mathbf{A}. That process uses much less memory than building the Qk\mathbf{Q}_k matrices explicitly.

The algorithm we have described is encapsulated in Function 3.4.1. There is one more refinement in it, however. As indicated by (3.4.2), the application of a reflector P\mathbf{P} to a vector does not require the formation of the matrix P\mathbf{P} explicitly.

3.4.3Q-less QR and least squares

In Demo 3.3.1 it was seen that the Q\mathbf{Q} output of Julia’s qr function is not a standard matrix. The reason is that Equation (3.3.8) shows that in order to solve the linear least-squares problem, all we need from Q\mathbf{Q} is the computation of Q^Tb\hat{\mathbf{Q}}^T\mathbf{b}. Referring again to (3.4.10) and (3.4.2), the special structure of the reflectors is such that for this computation, we only need to apply code similar to lines 18 and 21 of Function 3.4.1 for each of the Householder vectors v\mathbf{v} that is constructed.

This observation leads to the idea of the Q-less QR factorization, in which the full or thin Q\mathbf{Q} is never computed explicitly. This is the variant used by Julia’s qr. The returned value Q used within Function 3.3.2 is of a special type that allows Julia to perform Q'*b efficiently for the least-squares solution.

In Exercise 8 you are asked to derive the following result about the Q-less factorization.

The flop count quoted in Theorem 3.4.2 dominates the running time for least-squares solution via QR. Compared to the count from Theorem 3.2.3 for solution by the normal equations, the flops are essentially identical when m=nm=n, but the QR solution is about twice the cost when mnm\gg n. The redeeming quality of the QR route is better stability, which we do not discuss here.

3.4.4Exercises

  1. ⌨ Find a Householder reflector P\mathbf{P} such that

    P[296]=[1100].\mathbf{P} \begin{bmatrix} 2 \\ 9 \\ -6 \end{bmatrix} = \begin{bmatrix} 11\\0\\0 \end{bmatrix}.
  2. ✍ Prove the unfinished items in Theorem 3.4.1, namely that a Householder reflector P\mathbf{P} is symmetric and orthogonal.

  3. ✍ Let P\mathbf{P} be a Householder reflector as in (3.4.1).

    (a) Find a vector u\mathbf{u} such that Pu=u\mathbf{P}\mathbf{u} = -\mathbf{u}. (Figure 3.4.1 may be of help.)

    (b) What algebraic condition is necessary and sufficient for a vector x\mathbf{x} to satisfy Px=x\mathbf{P}\mathbf{x}=\mathbf{x}? In nn dimensions, how many such linearly independent vectors are there?

  4. ✍ Under certain circumstances, computing the vector v\mathbf{v} in (3.4.4) could lead to subtractive cancellation, which is why line 12 of Function 3.4.1 reads as it does. Devise an example that causes subtractive cancellation if (3.4.4) is used.

  5. ✍ Suppose QR factorization is used to compute the solution of a square linear system, Ax=b\mathbf{A}\mathbf{x}=\mathbf{b}, i.e., let m=nm=n.

    (a) Find an asymptotic flop count for this procedure, and compare it to the LU factorization algorithm.

    (b) Show that κ2(A)=κ2(R)\kappa_2(\mathbf{A}) = \kappa_2(\mathbf{R}).

  6. ✍ Prove that κ2(A)=κ2(R)\kappa_2(\mathbf{A})=\kappa_2(\mathbf{R}) when A\mathbf{A} is not square. (Be careful! You can’t take an inverse of A\mathbf{A} or R\mathbf{R}.)

  7. Another algorithmic technique for orthogonally introducing zeros into a matrix is the Givens rotation. Given a 2-vector [α,β][\alpha,\, \beta], it defines an angle θ such that

    [cos(θ)sin(θ)sin(θ)cos(θ)][αβ]=[α2+β20].\begin{bmatrix} \cos(\theta) & \sin(\theta) \\ -\sin(\theta) & \cos(\theta) \end{bmatrix} \begin{bmatrix} \alpha \\ \beta \end{bmatrix} = \begin{bmatrix} \sqrt{\alpha^2 + \beta^2} \\ 0 \end{bmatrix}.

    (a) ✍ Given α and β, show how to compute cosθ\cos \theta and sinθ\sin \theta without evaluating any trig functions.

    (b) ⌨ Given the vector z=[1  2  3  4  5]T\mathbf{z}=[1\;2\;3\;4\;5]^T, use Julia to find a sequence of Givens rotations that transforms z\mathbf{z} into the vector ze1\| \mathbf{z} \|\mathbf{e}_1. (Hint: You can operate only on pairs of elements at a time, introducing a zero at the lower of the two positions.)

  8. ✍ Derive the result of Theorem 3.4.2 by analyzing Function 3.4.1 without lines 20–22.

  9. ✍ Suppose m=Knm=Kn for constant K1K \ge 1 as both mm and nn go to infinity. Show that the flop counts from Theorem 3.4.2 and Theorem 3.2.3 have a ratio of 1 when K=1K=1 and approaches 2 as KK\to \infty.