Last time¶
Right vs left-looking algorithms
Elementary reflectors
Householder QR
Today¶
Householder fix
Comparison of interfaces
Profiling
Cholesky QR
using LinearAlgebra
using Plots
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
endgram_schmidt_classical (generic function with 1 method)Householder QR¶
Gram-Schmidt constructed a triangular matrix to orthogonalize into . Each step was a projector, which is a rank-deficient operation. Householder uses orthogonal transformations (reflectors) to triangularize.
The structure of the algorithm is
Constructing the ¶
Each of our will have the form
where is a “reflection” that achieves
where is the column of from the diagonal down. This transformation is a reflection across a plane with normal .

The reflection, as depicted above by Trefethen and Bau (1999) can be written .
Adventures in reflection¶
A = rand(4, 4); A += A'
v = copy(A[:,1])
v[1] -= norm(v)
v = normalize(v)
F = I - 2 * v * v'
B = F * A4×4 Matrix{Float64}:
2.68473 2.24213 1.49483 2.48211
9.79724e-17 -0.270069 0.196389 0.915463
1.19184e-16 -0.319677 -0.317452 0.238797
-2.06218e-16 -0.591514 -0.194604 -0.585453v = 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]
B4×4 Matrix{Float64}:
2.68473 2.24213 1.49483 2.48211
9.79724e-17 0.724583 0.225722 0.0313666
1.19184e-16 5.55112e-17 -0.308024 -0.0453481
-2.06218e-16 5.55112e-17 -0.177159 -1.11122An 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
endqr_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.397523How to interpret as ?¶
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
endreflectors_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; 0 1]
A = [0 2; 1e-2 1.]
V, R = qr_householder_naive(A)
#Q, R = qr(A)
R2×2 Matrix{Float64}:
0.01 1.0
-1.73472e-18 NaNWe had the lines
v = copy(R[j:end, j])
v[1] -= norm(v)
v = normalize(v)What happens when R is already upper triangular?

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[1] += (v[1] > 0 ? 1 : -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
endqr_householder (generic function with 1 method)A = [2 -1; -1 2] * 1e-10
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
Rtau = [1.894427190999916, 2.0]
V1 = [[1.0, -0.2360679774997897], [1.0]]
2×2 Matrix{Float64}:
-2.23607e-10 1.78885e-10
-1.29247e-26 -1.34164e-10Householder 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)(Any[[1.0, 0.0], [1.0]], [-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
Q = reflectors_to_dense(qr_householder(A)[1])
scatter(abs.(Q[:,1:end-1]' * v), yscale=:log10, title="Householder QR")Composition of reflectors¶
This turns applying reflectors from a sequence of vector operations to a sequence of (smallish) matrix operations. It’s the key to high performance and the native format (QRCompactWY) returned by Julia qr().
Q, R = qr(A[:, 1:5])LinearAlgebra.QRCompactWY{Float64, Matrix{Float64}, Matrix{Float64}}
Q factor: 20×20 LinearAlgebra.QRCompactWYQ{Float64, Matrix{Float64}, Matrix{Float64}}
R factor:
5×5 Matrix{Float64}:
-4.47214 0.0 -1.64763 0.0 -1.08899
0.0 2.71448 1.11022e-16 1.79412 -2.22045e-16
0.0 0.0 -1.46813 5.55112e-17 -1.37924
0.0 0.0 0.0 -0.774796 1.66533e-16
0.0 0.0 0.0 0.0 0.402707This works even for very nonsquare matrices¶
A = rand(1000000, 5)
Q, R = qr(A)
@show size(Q)
@show norm(Q*R - A)
Rsize(Q) = (1000000, 1000000)
norm(Q * R - A) = 4.103993514007667e-12
5×5 Matrix{Float64}:
-577.782 -433.374 -433.314 -432.929 -432.852
0.0 381.589 163.282 163.051 163.235
0.0 0.0 -345.047 -103.392 -103.448
0.0 0.0 0.0 329.173 76.3907
0.0 0.0 0.0 0.0 -320.405This is known as a “full” (or “complete”) QR factorization, in contrast to a reduced QR factorization in which has the same shape as .
How much memory does use?
Compare to numpy.linalg.qr¶
Need to decide up-front whether you want full or reduced QR.
Full QR is expensive to represent.
function qr_chol(A)
R = cholesky(A' * A).U
Q = A / R
Q, R
end
A = rand(10,4)
Q, R = qr_chol(A)
@show norm(Q' * Q - I)
@show norm(Q * R - A)norm(Q' * Q - I) = 1.4610813375536948e-15
norm(Q * R - A) = 2.237726045655905e-16
2.237726045655905e-16x = LinRange(-1, 1, 20)
A = vander(x)
Q, R = qr_chol(A)
@show norm(Q' * Q - I)
@show norm(Q * R - A);norm(Q' * Q - I) = 0.1749736012761826
norm(Q * R - A) = 6.520306397146206e-16
Can we fix this?¶
Note that the product of two triangular matrices is triangular.
R = triu(rand(5,5))
R * R5×5 Matrix{Float64}:
0.453167 0.972976 1.01752 1.26301 1.10259
0.0 0.993084 0.919986 1.32044 1.08722
0.0 0.0 0.276985 0.796827 0.437453
0.0 0.0 0.0 0.902163 0.751084
0.0 0.0 0.0 0.0 0.77306function qr_chol2(A)
Q, R = qr_chol(A)
Q, R1 = qr_chol(Q)
Q, R1 * R
end
x = LinRange(-1, 1, 20)
A = vander(x)
Q, R = qr_chol2(A)
@show norm(Q' * Q - I)
@show norm(Q * R - A);norm(Q' * Q - I) = 1.3276319786294278e-15
norm(Q * R - A) = 1.0894860257129427e-15
How fast are these methods?¶
m, n = 5000, 1000
A = randn(m, n)
@time qr(A); 0.169613 seconds (15 allocations: 38.698 MiB, 0.81% gc time)
A = randn(m, n)
@time qr_chol2(A); 0.199483 seconds (22 allocations: 114.444 MiB, 15.33% gc time)
Profiling¶
using ProfileSVG
@profview qr(A)
#@profview qr_chol(A)Krylov subspaces¶
Suppose we wish to solve
but only have the ability to apply the action of the (we do not have access to its entries). A general approach is to work with approximations in a Krylov subspace, which has the form
This matrix is horribly ill-conditioned and cannot stably be computed as written. Instead, we seek an orthogonal basis that spans the same space as . We could write this as a factorization
where the first column . The is unnecessary and hopelessly ill-conditioned, so a slightly different procedure is used.
Arnoldi iteration¶
The Arnoldi iteration applies orthogonal similarity transformations to reduce to Hessenberg form, starting from a vector ,
Let’s multiply on the right by and examine the first columns,
where is an Hessenberg matrix.
GMRES (Generalized Minimum Residual) minimizes
over the subspace . I.e., for some . By the Arnoldi recurrence, this is equivalent to
which can be solved by minimizing
Since , the least squares problem is to minimize
The solution of this least squares problem is achieved by incrementally updating a factorization of .
Notes¶
The solution constructed by GMRES at iteration is not explicitly available. If a solution is needed, it must be constructed by solving the least squares problem and forming the solution as a linear combination of the vectors . The leading cost is where .
The residual vector is not explicitly available in GMRES. To compute it, first build the solution .
GMRES minimizes the 2-norm of the residual which is equivalent to the norm of the error .
More notes on GMRES¶
GMRES needs to store the full , which is unaffordable for large (many iterations). The standard solution is to choose a “restart” and to discard and start over with an initial guess after each iterations. This algorithm is called GMRES(k). PETSc’s default solver is GMRES(30) and the restart can be controlled using the run-time option
-ksp_gmres_restart.Most implementations of GMRES use classical Gram-Schmidt because it is much faster in parallel (one reduction per iteration instead of reductions per iteration). The PETSc option
-ksp_gmres_modifiedgramschmidtcan be used when you suspect that classical Gram-Schmidt may be causing instability.There is a very similar (and older) algorithm called GCR that maintains and . This is useful, for example, if a convergence tolerance needs to inspect individual entries. GCR requires vectors instead of vectors, and can tolerate a nonlinear preconditioner. FGMRES is a newer algorithm with similar properties to GCR.