TExcelTAD
Home › Numerical Methods › Computing QR with Householder Reflections

Householder Reflections

Gram-Schmidt builds QQ column by column and loses orthogonality on ill-conditioned matrices. Householder's method goes the other way: it applies a sequence of orthogonal reflections to AA until it is upper triangular. Because each reflection is exactly orthogonal, the method is backward stable, and it is how LAPACK computes QR.

A reflector

For a unit vector v\mathbf{v}, the matrix H=I−2vvTH = I - 2\mathbf{v}\mathbf{v}^T reflects across the hyperplane orthogonal to v\mathbf{v}. It is symmetric and orthogonal (H2=IH^2 = I). Given a vector z\mathbf{z}, the choice

w=z+sign⁡(z1)∥z∥e1,v=w∥w∥\mathbf{w} = \mathbf{z} + \operatorname{sign}(z_1)\lVert\mathbf{z}\rVert\mathbf{e}_1, \qquad \mathbf{v} = \frac{\mathbf{w}}{\lVert\mathbf{w}\rVert}

gives Hz=−sign⁡(z1)∥z∥ e1H\mathbf{z} = -\operatorname{sign}(z_1)\lVert\mathbf{z}\rVert\,\mathbf{e}_1: every entry but the first becomes zero. Adding (rather than subtracting) the norm avoids cancellation when z\mathbf{z} already points along e1\mathbf{e}_1.

python · try it yourself
import numpy as np

z = np.array([3.0, 1.0, 5.0, 1.0])
w = z.copy()
w[0] += np.sign(z[0]) * np.linalg.norm(z)
v = w / np.linalg.norm(w)
H = np.eye(4) - 2 * np.outer(v, v)
print("H z =", np.round(H @ z, 12) + 0)
print("||z|| =", np.linalg.norm(z), "| H orthogonal:", np.allclose(H.T @ H, np.eye(4)))
H z = [-6.  0.  0.  0.]
||z|| = 6.0 | H orthogonal: True

QR by reflections

Step kk applies a reflector to rows k,…,mk, \ldots, m that zeroes column kk below the diagonal. After nn steps:

Hn⋯H2H1A=R,Q=H1H2⋯Hn.H_n\cdots H_2H_1A = R, \qquad Q = H_1H_2\cdots H_n.

HH is never formed as a matrix: applying it to a block costs only a matrix-vector product and a rank-one update, B−=2v(vTB)B \mathrel{-}= 2\mathbf{v}(\mathbf{v}^TB).

python · try it yourself
import numpy as np

def householder_qr(A):
    m, n = A.shape
    R = A.astype(float).copy()
    vs = []
    for k in range(n):
        z = R[k:, k]
        w = z.copy()
        w[0] += (np.sign(z[0]) or 1.0) * np.linalg.norm(z)
        v = w / np.linalg.norm(w)
        R[k:, k:] -= 2 * np.outer(v, v @ R[k:, k:])
        vs.append(v)
    Q = np.eye(m)
    for k in reversed(range(n)):                 # Q = H_1 H_2 ... H_n applied to I
        Q[k:, :] -= 2 * np.outer(vs[k], vs[k] @ Q[k:, :])
    return Q, np.triu(R)

t = np.linspace(0, 1, 60)
A = np.vander(t, 10, increasing=True)
Q, R = householder_qr(A)
print(f"||Q^T Q - I|| = {np.linalg.norm(Q.T @ Q - np.eye(60)):.1e}")
print(f"||QR - A||    = {np.linalg.norm(Q @ R - A):.1e}")
print("|diag R| matches NumPy:", np.allclose(np.abs(np.diag(R)), np.abs(np.diag(np.linalg.qr(A)[1]))))
||Q^T Q - I|| = 4.9e-15
||QR - A||    = 1.3e-14
|diag R| matches NumPy: True

QQ is orthogonal to machine precision even though κ(A)\kappa(A) is in the millions; compare the Gram-Schmidt results in The QR Factorization. The signs of the rows of RR may differ from NumPy's, since QR is unique only up to signs.

Givens rotations

A Givens rotation zeroes a single entry by rotating two rows:

G=(cs−sc),c=aa2+b2, s=ba2+b2,G(ab)=(a2+b20).G = \begin{pmatrix} c & s \\ -s & c\end{pmatrix}, \qquad c = \frac{a}{\sqrt{a^2 + b^2}},\ s = \frac{b}{\sqrt{a^2 + b^2}}, \qquad G\begin{pmatrix} a \\ b\end{pmatrix} = \begin{pmatrix} \sqrt{a^2 + b^2} \\ 0\end{pmatrix}.

Rotations cost more than reflections for dense matrices but are ideal for sparse or structured ones (zeroing one subdiagonal of a Hessenberg matrix) and for updating a QR factorization when a row is added.

python · try it yourself
import numpy as np

a, b = 3.0, 4.0
r = np.hypot(a, b)
G = np.array([[a / r, b / r], [-b / r, a / r]])
print(np.round(G @ np.array([a, b]), 12) + 0)
[5. 0.]

In software

  • LAPACK geqrf stores the Householder vectors in the lower triangle of RR's array plus a vector of scalars; scipy.linalg.qr(mode="raw") exposes this compact form, and orgqr/ormqr apply QQ without forming it.
  • scipy.linalg.qr_insert, qr_delete, qr_update update factorizations with Givens rotations.

Exercises

Exercise 1: Reflect a vector

Find w\mathbf{w} and HzH\mathbf{z} for z=(4,3)\mathbf{z} = (4, 3).

Answer

w=(9,3)\mathbf{w} = (9, 3), Hz=(−5,0)H\mathbf{z} = (-5, 0).

Solution

∥z∥=5\lVert\mathbf{z}\rVert = 5, w=(4+5,3)\mathbf{w} = (4 + 5, 3). The reflection sends z\mathbf{z} to −sign⁡(z1)∥z∥e1=(−5,0)-\operatorname{sign}(z_1)\lVert\mathbf{z}\rVert\mathbf{e}_1 = (-5, 0).

Exercise 2: Properties

Show that H=I−2vvTH = I - 2\mathbf{v}\mathbf{v}^T with ∥v∥=1\lVert\mathbf{v}\rVert = 1 satisfies H2=IH^2 = I.

Answer

H2=I−4vvT+4v(vTv)vT=IH^2 = I - 4\mathbf{v}\mathbf{v}^T + 4\mathbf{v}(\mathbf{v}^T\mathbf{v})\mathbf{v}^T = I.

Solution

Expand and use vTv=1\mathbf{v}^T\mathbf{v} = 1. Since HH is also symmetric, HTH=IH^TH = I: it is orthogonal.

Exercise 3: QR by Givens (code)

Triangularize A=(123456)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6\end{pmatrix} with Givens rotations (zero entries (3,1)(3,1), (2,1)(2,1), (3,2)(3,2) in that order) and compare ∣diag⁡R∣\lvert\operatorname{diag}R\rvert with NumPy.

Answer

∣r11∣≈5.916080\lvert r_{11}\rvert \approx 5.916080 and ∣r22∣≈0.828079\lvert r_{22}\rvert \approx 0.828079, matching NumPy.

Solution
python · try it yourself
import numpy as np
R = np.array([[1.0, 2], [3, 4], [5, 6]])
for (i, j, col) in [(1, 2, 0), (0, 1, 0), (1, 2, 1)]:
    a, b = R[i, col], R[j, col]
    r = np.hypot(a, b); c, s = a / r, b / r
    R[[i, j], :] = np.array([[c, s], [-s, c]]) @ R[[i, j], :]
print(np.round(np.abs(np.diag(R)), 6), np.round(np.abs(np.diag(np.linalg.qr(np.array([[1.0, 2], [3, 4], [5, 6]]))[1])), 6))
[5.91608  0.828079] [5.91608  0.828079]

Test yourself

A Householder reflector H=I−2vvTH = I - 2\mathbf{v}\mathbf{v}^T is…

  1. triangular
  2. symmetric and orthogonal
  3. singular
  4. diagonal

Why is the sign in w=z+sign⁡(z1)∥z∥e1\mathbf{w} = \mathbf{z} + \operatorname{sign}(z_1)\lVert\mathbf{z}\rVert\mathbf{e}_1 chosen this way?

  1. To make HH symmetric
  2. To avoid cancellation in the first entry of w\mathbf{w}
  3. It does not matter
  4. To make RR positive

Givens rotations are especially useful for…

  1. dense matrices
  2. zeroing selected entries of sparse or structured matrices
  3. computing determinants
  4. inverting matrices