Householder Reflections
Gram-Schmidt builds 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 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 , the matrix reflects across the hyperplane orthogonal to . It is symmetric and orthogonal (). Given a vector , the choice
gives : every entry but the first becomes zero. Adding (rather than subtracting) the norm avoids cancellation when already points along .
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 applies a reflector to rows that zeroes column below the diagonal. After steps:
is never formed as a matrix: applying it to a block costs only a matrix-vector product and a rank-one update, .
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
is orthogonal to machine precision even though is in the millions; compare the Gram-Schmidt results in The QR Factorization. The signs of the rows of 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:
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.
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
geqrfstores the Householder vectors in the lower triangle of 's array plus a vector of scalars;scipy.linalg.qr(mode="raw")exposes this compact form, andorgqr/ormqrapply without forming it. scipy.linalg.qr_insert,qr_delete,qr_updateupdate factorizations with Givens rotations.
Exercises
Exercise 1: Reflect a vector
Find and for .
Answer
, .
Solution
, . The reflection sends to .
Exercise 2: Properties
Show that with satisfies .
Answer
.
Solution
Expand and use . Since is also symmetric, : it is orthogonal.
Exercise 3: QR by Givens (code)
Triangularize with Givens rotations (zero entries , , in that order) and compare with NumPy.
Answer
and , matching NumPy.
Solution
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 is…
- triangular
- symmetric and orthogonal
- singular
- diagonal
Why is the sign in chosen this way?
- To make symmetric
- To avoid cancellation in the first entry of
- It does not matter
- To make positive
Givens rotations are especially useful for…
- dense matrices
- zeroing selected entries of sparse or structured matrices
- computing determinants
- inverting matrices