Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

2026-09-25 Householder QR

Last time

  • Stability and ill conditioning

  • Intro to performance modeling

  • Classical vs Modified Gram-Schmidt

Today

  • Performance strategies

  • Right vs left-looking algorithms

  • Elementary reflectors

  • Householder QR

using LinearAlgebra
using Plots
using Polynomials
default(linewidth=4, legendfontsize=12)

function vander(x, k=nothing)
    if isnothing(k)
        k = length(x)
    end
    m = length(x)
    V = ones(m, k)
    for j in 2:k
        V[:, j] = V[:, j-1] .* x
    end
    V
end

function gram_schmidt_classical(A)
    m, n = size(A)
    Q = zeros(m, n)
    R = zeros(n, n)
    for j in 1:n
        v = A[:,j]
        R[1:j-1,j] = Q[:,1:j-1]' * v
        v -= Q[:,1:j-1] * R[1:j-1,j]
        R[j,j] = norm(v)
        Q[:,j] = v / R[j,j]
    end
    Q, R
end
gram_schmidt_classical (generic function with 1 method)

Counting flops is a bad model

  • We load a single entry (8 bytes) and do 2 flops (add + multiply). That’s an arithmetic intensity of 0.25 flops/byte.

  • Current hardware can do about 10 flops per byte, so our best algorithms will run at about 2% efficiency.

  • Need to focus on memory bandwidth, not flops.

Dense matrix-matrix mulitply

Inherent data dependencies

[q1∣q2∣q3∣q4∣q5][r11r12r13r14r15r22r23r24r25r33r34r35r44r45r55]\Bigg[ q_1 \Bigg| q_2 \Bigg| q_3 \Bigg| q_4 \Bigg| q_5 \Bigg] \begin{bmatrix} r_{11} & r_{12} & r_{13} & r_{14} & r_{15} \\ & r_{22} & r_{23} & r_{24} & r_{25} \\ & & r_{33} & r_{34} & r_{35} \\ &&& r_{44} & r_{45} \\ &&& & r_{55} \end{bmatrix}

Right-looking modified Gram-Schmidt

function gram_schmidt_modified(A)
    m, n = size(A)
    Q = copy(A)
    R = zeros(n, n)
    for j in 1:n
        R[j,j] = norm(Q[:,j])
        Q[:,j] /= R[j,j]
        R[j,j+1:end] = Q[:,j]'*Q[:,j+1:end]
        Q[:,j+1:end] -= Q[:,j]*R[j,j+1:end]'
    end
    Q, R
end
gram_schmidt_modified (generic function with 1 method)
m = 20
x = LinRange(-1, 1, m)
A = vander(x, m)
@show cond(A)
Q, R = gram_schmidt_modified(A)
@show norm(Q' * Q - I)
@show norm(Q * R - A)
cond(A) = 2.7224082312417406e8
norm(Q' * Q - I) = 8.486718528276085e-9
norm(Q * R - A) = 8.709998074379606e-16
8.709998074379606e-16

Classical versus modified?

  • Classical

    • Really unstable, orthogonality error of size 1≫ϵmachine1 \gg \epsilon_{\text{machine}} for κ(A)≈108\kappa(A) \approx 10^8

    • Don’t need to know all the vectors in advance

  • Modified

    • Left-looking is inefficient

    • Needs to be right-looking for efficiency

    • Less unstable, but orthogonality error 10−9≫ϵmachine10^{-9} \gg \epsilon_{\text{machine}}

m = 20
x = LinRange(-1, 1, m)
A = vander(x, m)
Q, R = qr(A)
@show norm(Q' * Q - I)
@show norm(Q * R - A);
norm(Q' * Q - I) = 3.216006485393791e-15
norm(Q * R - A) = 4.00324754088652e-15

Householder QR

Gram-Schmidt constructed a triangular matrix RR to orthogonalize AA into QQ. Each step was a projector, which is a rank-deficient operation. Householder uses orthogonal transformations (reflectors) to triangularize.

Qn⋯Q1⏟QTA=R\underbrace{Q_{n} \dotsb Q_1}_{Q^T} A = R

The structure of the algorithm is

[∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗]⏟A→[∗∗∗0∗∗0∗∗0∗∗0∗∗]⏟Q1A→[∗∗∗0∗∗00∗00∗00∗]⏟Q2Q1A→[∗∗∗0∗∗00∗000000]⏟Q3Q2Q1A\underbrace{\begin{bmatrix} * & * & * \\ * & * & * \\ * & * & * \\ * & * & * \\ * & * & * \\ \end{bmatrix}}_{A} \to \underbrace{\begin{bmatrix} * & * & * \\ 0 & * & * \\ 0 & * & * \\ 0 & * & * \\ 0 & * & * \\ \end{bmatrix}}_{Q_1 A} \to \underbrace{\begin{bmatrix} * & * & * \\ 0 & * & * \\ 0 & 0 & * \\ 0 & 0 & * \\ 0 & 0 & * \\ \end{bmatrix}}_{Q_2 Q_1 A} \to \underbrace{\begin{bmatrix} * & * & * \\ 0 & * & * \\ 0 & 0 & * \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{bmatrix}}_{Q_3 Q_2 Q_1 A}

Constructing the QjQ_j

Qn⋯Q1⏟QTA=R\underbrace{Q_{n} \dotsb Q_1}_{Q^T} A = R

Each of our QjQ_j will have the form

Qj=[Ij00F]Q_j = \begin{bmatrix} I_j & 0 \\ 0 & F \end{bmatrix}

where FF is a “reflection” that achieves

Fx=[∥x∥00⋮]F x = \begin{bmatrix} \lVert x \rVert \\ 0 \\ 0 \\ \vdots \end{bmatrix}

where xx is the column of RR from the diagonal down. This transformation is a reflection across a plane with normal v=Fx−x=∥x∥e1−xv = Fx - x = \lVert x \rVert e_1 - x.

Householder Reflector (Trefethen and Bau, 1999)

The reflection, as depicted above by Trefethen and Bau (1999) can be written F=I−2vvTvTvF = I - 2 \frac{v v^T}{v^T v}.

Adventures in reflection

A = rand(4, 4); # A += A'
v = copy(A[:,1])
@show norm(v)
v[1] -= norm(v)
v = normalize(v)
F = I - 2 * v * v'
B = F * A
norm(v) = 1.336731815641707
4×4 Matrix{Float64}: 1.33673 0.8379 1.33584 0.875539 -3.09258e-16 -0.606621 0.0673465 -0.122927 -2.31024e-16 -0.00961638 0.0584903 0.3399 -3.16289e-17 0.379096 0.617029 0.839596
v = copy(B[2:end, 2])
v[1] -= norm(v); v = normalize(v)
F = I - 2 * v * v'
B[2:end, 2:end] = F * B[2:end, 2:end]
B
4×4 Matrix{Float64}: 1.33673 0.8379 1.33584 0.875539 -3.09258e-16 0.715399 0.269077 0.544577 -2.31024e-16 1.0842e-18 0.0599577 0.344756 -3.16289e-17 -5.55112e-17 0.559182 0.648186

An algorithm

function qr_householder_naive(A)
    m, n = size(A)
    R = copy(A)
    V = [] # list of reflectors
    for j in 1:n
        v = copy(R[j:end, j])
        v[1] -= norm(v)
        v = normalize(v)
        R[j:end,j:end] -= 2 * v * (v' * R[j:end,j:end])
        push!(V, v)
    end
    V, R
end
qr_householder_naive (generic function with 1 method)
m = 4
x = LinRange(-1, 1, m)
A = vander(x, m)
V, R = qr_householder_naive(A)
_, R_ = qr(A)
R_
4×4 Matrix{Float64}: -2.0 0.0 -1.11111 0.0 0.0 1.49071 1.38778e-17 1.3582 0.0 0.0 0.888889 9.71445e-17 0.0 0.0 0.0 0.397523

How to interpret VV as QQ?

function reflectors_mult(V, x)
    y = copy(x)
    for v in reverse(V)
        n = length(v) - 1
        y[end-n:end] -= 2 * v * (v' * y[end-n:end])
    end
    y
end

function reflectors_to_dense(V)
    m = length(V[1])
    Q = diagm(ones(m))
    for j in 1:m
        Q[:,j] = reflectors_mult(V, Q[:,j])
    end
    Q
end
reflectors_to_dense (generic function with 1 method)
m = 20
x = LinRange(-1, 1, m)
A = vander(x, m)
V, R = qr_householder_naive(A)
Q = reflectors_to_dense(V)
@show norm(Q' * Q - I)
@show norm(Q * R - A);
norm(Q' * Q - I) = 3.7994490775439526e-15
norm(Q * R - A) = 7.562760794606217e-15

Great, but we can still break it

A = [1 0; 0 1.]
#V, R = qr_householder_naive(A)
2×2 Matrix{Float64}: 1.0 0.0 0.0 1.0

We had the lines

    v = copy(R[j:end, j])
    v[1] -= norm(v)
    v = normalize(v)

What happens when R is already upper triangular?

Choosing the better of two Householder reflectors (Trefethen and Bau, 1999).

An improved algorithm

function qr_householder(A)
    m, n = size(A)
    R = copy(A)
    V = [] # list of reflectors
    for j in 1:n
        v = copy(R[j:end, j])
        v[1] += sign(v[1]) * norm(v) # <---
        v = normalize(v)
        R[j:end,j:end] -= 2 * v * v' * R[j:end,j:end]
        push!(V, v)
    end
    V, R
end
qr_householder (generic function with 1 method)
A = [2 -1; -1 2] * 1e-10
A = [1 0; 0 1]
V, R = qr_householder(A)
tau = [2*v[1]^2 for v in V]
@show tau
V1 = [v ./ v[1] for v in V]
@show V1
R
tau = [2.0, 2.0]
V1 = [[1.0, 0.0], [1.0]]
2×2 Matrix{Int64}: -1 0 0 -1

Householder is backward stable

m = 40
x = LinRange(-1, 1, m)
A = vander(x, m)
V, R = qr_householder(A)
Q = reflectors_to_dense(V)
@show norm(Q' * Q - I)
@show norm(Q * R - A);
norm(Q' * Q - I) = 5.949301496893686e-15
norm(Q * R - A) = 1.2090264267288813e-14
A = [1 0; 0 1.]
V, R = qr_householder(A)
qr(A)
LinearAlgebra.QRCompactWY{Float64, Matrix{Float64}, Matrix{Float64}} Q factor: 2×2 LinearAlgebra.QRCompactWYQ{Float64, Matrix{Float64}, Matrix{Float64}} R factor: 2×2 Matrix{Float64}: 1.0 0.0 0.0 1.0

Orthogonality is preserved

x = LinRange(-1, 1, 20)
A = vander(x)
Q, _ = gram_schmidt_classical(A)
v = A[:,end]
@show norm(v)
scatter(abs.(Q[:,1:end-1]' * v), yscale=:log10, title="Classical Gram-Schmidt")
norm(v) = 1.4245900685395503
Loading...
Q = reflectors_to_dense(qr_householder(A)[1])
scatter(abs.(Q[:,1:end-1]' * v), yscale=:log10, title="Householder QR")
Loading...