using PyPlot, Interact, LinearAlgebra, SparseArrays, Arpack
The basic problem with most of the linear algebra techniques we have learned so far is that they scale badly for large matrices. Ordinary Gaussian elimination (LU factorization), Gram–Schmidt and other QR factorization algorithms, and techniques that computes all the eigenvalues and eigenvectors, all require $\sim n^3$ operations and $\sim n^2$ storage for $n \times n$ matrices.
This all assumes that you explicitly store and compute with all of the entries of the matrix, regardless of their values. Hence, they are sometimes called dense matrix algorithms (the opposite of "sparse" matrices, discussed below).
So, doubling the size of the matrix asymptotically requires about 8× more time. For any finite $n$, it is not quite a factor of 8 because computers are complicated; e.g. for larger matrices, it can use multiple processors more efficiently:
A1 = randn(500,500)
A2 = randn(1000,1000)
lu(A1); # do it once to make sure it is compiled
@elapsed(lu(A2)) / @elapsed(lu(A1))
3.121091035332169
qr(A1); # do it once to make sure it is compiled
@elapsed(qr(A2)) / @elapsed(qr(A1))
5.9377766625154935
eigen(A1); # do it once to make sure it is compiled
@elapsed(eigen(A2)) / @elapsed(eigen(A1))
5.506338823080527
@time lu(A2)
@time eigen(A2);
0.051441 seconds (8 allocations: 7.637 MiB) 2.269618 seconds (63 allocations: 31.566 MiB, 3.53% gc time)
Still, if we take the $O(n^3)$ scaling as a rough guide, this would suggest that LU-factorizing (lufact
) a $10^6 \times 10^6$ matrix would take $0.02\mbox{sec} \times 1000^3 \sim \mbox{months}$ and finding the eigenvectors and eigenvalues (eigfact
) would take $2 \mbox{sec} \times 1000^3 \sim \mbox{decades}$.
In practice, we actually usually run out of space before we run out of time. If we have 16GB of memory, the biggest matrix we can store (each number requires 8 bytes) is $8n^2\mbox{ bytes} = 16\times 10^9 \implies 40000 \times 40000$.
The saving grace is that most really large matrices are sparse = mostly zeros (or have some other special structure with similar consequences). You only have to store the nonzero entries, and you can multiply matrix × vector quickly (you can skip the zeros).
In Julia, there are many functions to work with sparse matrices by only storing the nonzero elements. The simplest one is the sparse
function. Given a matrix $A$, the sparse(A)
function creates a special data structure that only stores the nonzero elements:
A = [ 2 -1 0 0 0 0
-1 2 -1 0 0 0
0 -1 2 -1 0 0
0 0 -1 2 -1 0
0 0 0 -1 2 -1
0 0 0 0 -1 2]
6×6 Array{Int64,2}: 2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1 0 0 0 0 -1 2
sparse(A)
6×6 SparseMatrixCSC{Int64,Int64} with 16 stored entries: [1, 1] = 2 [2, 1] = -1 [1, 2] = -1 [2, 2] = 2 [3, 2] = -1 [2, 3] = -1 [3, 3] = 2 [4, 3] = -1 [3, 4] = -1 [4, 4] = 2 [5, 4] = -1 [4, 5] = -1 [5, 5] = 2 [6, 5] = -1 [5, 6] = -1 [6, 6] = 2
(Of course, in practice you would want to create the sparse matrix directly, rather than first making the "dense" matrix A
and then converting it to a sparse data structure.)
We've actually seen this several times in graph/network-based problems, where we often get matrices of the form:
$$A = G^T D G$$where D is diagonal (very sparse!) and G is the incidence matrix. Since each graph node is typically only connected to a few other nodes, G is sparse and so is A.
If each node is connected to a bounded number of other nodes (say, ≤ 20), then A only has $\sim n$ (i.e. proportional to n, not equal to n) entries, and $Ax$ can be computed in $\sim n$ operations and $\sim n$ storage (unlike $\sim n^2$ for a general matrix).
So, a $10^6 \times 10^6$ sparse matrix might be stored in only a few megabytes and take only a few milliseconds to multiply by a vector.
Much of large-scale linear algebra is about devising techniques to exploit sparsity or any case where matrix × vector is faster than n².
In fact, we already learned one algorithm that works well for sparse matrices: the power method to compute eigenvalues and eigenvectors. If we just repeatedly multiply a random vector by $A$, it converges towards the eigenvector of the largest $|\lambda|$. And since this only involves matrix × vector operations, it can take advantage of sparse matrices.
Moreover, there are variants of this algorithm that work for the smallest eigenvalues as well, and it turns out that there are more sophisticated variants that converge even faster than power iterations. In Julia, these are provided by the eigs
function, which lets you compute a few of the biggest or smallest eigenvalues quickly even for huge sparse matrices.
As an example, let's consider the two-dimensional grid of masses and springs that I showed in an earlier lecture, whose eigenvectors are the vibrating modes.
This can be thought of as a discretized approximation of a vibrating drum, which is described by the partial differential equation $\nabla^2 h = \frac{\partial^2 h}{\partial t^2}$ where $h(x,y,t)$ is the height of the drum surface (= zero at the edges of the drum). (This is an example taken from the class 18.303: Linear Partial Differential Equations at MIT.)
For this class, don't worry too much about how the matrix is constructed.
function spdiagm_nonsquare(m, n, args...)
I, J, V = SparseArrays.spdiagm_internal(args...)
return sparse(I, J, V, m, n)
end
# compute the first-derivative finite-difference matrix
# for Dirichlet boundaries, given a grid x[:] of x points
# (including the endpoints where the function = 0!).
function sdiff1(x)
N = length(x) - 2
dx1 = Float64[1/(x[i+1] - x[i]) for i = 1:N]
dx2 = Float64[-1/(x[i+1] - x[i]) for i = 2:N+1]
return spdiagm_nonsquare(N+1,N, 0=>dx1, -1=>dx2)
end
# compute the -∇⋅ c ∇ operator for a function c(x,y)
# and arrays x[:] and y[:] of the x and y points,
# including the endpoints where functions are zero
# (i.e. Dirichlet boundary conditions).
function Laplacian(x, y, c = (x,y) -> 1.0)
Dx = sdiff1(x)
Nx = size(Dx,2)
Dy = sdiff1(y)
Ny = size(Dy,2)
# discrete Gradient operator:
G = [kron(sparse(I,Ny,Ny), Dx); kron(Dy, sparse(I,Nx,Nx))]
# grids for derivatives in x and y directions
x′ = [0.5*(x[i]+x[i+1]) for i = 1:length(x)-1]
y′ = [0.5*(y[i]+y[i+1]) for i = 1:length(y)-1]
# evaluate c(x)
C = spdiagm(0 => [ vec([c(X,Y) for X in x′, Y in y[2:end-1]]);
vec([c(X,Y) for X in x[2:end-1], Y in y′]) ])
return G' * C * G # -∇⋅ c ∇
end
Laplacian (generic function with 2 methods)
The above code defines the matrix for a box-shaped drum, but for fun I will change it to define a drum over an oddly shaped domain:
N = 400
x = range(-1,stop=1,length=N+2)[2:end-1]
y = x' # a row vector
r = sqrt.(x.^2 .+ y.^2) # use broadcasting (.+) to make Nx x Ny matrix of radii
θ = atan.(y, x) # and angles
φ = @. exp(-(r - θ*0.5/π - 0.5)^2 / 0.3^2) - 0.5
imshow(φ .> 0, extent=[-1,1,-1,1], cmap="binary")
title("the domain")
PyObject Text(0.5, 1.0, 'the domain')
This all eventually leads to the following matrix, whose eigenvalues $ \lambda = \omega^2$ are the squares of the frequencies and whose eigenvectors are the vibrating modes:
x0 = range(-1,1,length=N+2) # includes boundary points, unlike x
Abox = Laplacian(x0, x0, (x,y) -> 1.0);
i = findall(vec(φ) .> 0)
A = Abox[i,i]
size(A)
(59779, 59779)
This is a $60000\times60000$ matrix, which would too big to even store on my laptop if we stored every entry. Because it is sparse, however, almost all of the entries are zero and we only need to store those.
The nnz
function computes the number of nonzero entries, and we can use it to compute the fraction of nonzero entries in A:
nnz(A) / length(A)
8.313882809376213e-5
nnz(A) / size(A,1)
4.969956004617006
Less than 0.01% of the entries are nonzero! It really pays to take advantage of this.
Now we'll compute a few of the smallest-|λ| eigenvectors using eigs
. We'll use the Interact package to interactively decide which eigenvalue to plot.
u = zeros(N,N)
@time λ, X = eigs(A, nev=20, which=:SM);
f = figure(figsize=(10,4))
for which_eig2 in 1:2:20
display(withfig(f) do
for which_eig = which_eig2:which_eig2+1
subplot(1,2,which_eig+1-which_eig2)
u[i] = X[:,which_eig]
umax = maximum(abs, u)
imshow(u, extent=[-1,1,-1,1], vmin=-umax,vmax=+umax, cmap="RdBu")
title("eigenvalue n=$which_eig, \$\\lambda\$ = $(λ[which_eig])")
end
end)
end
5.248698 seconds (6.75 M allocations: 554.236 MiB, 5.64% gc time)
Notice that it took less than two seconds to solve for 20 eigenvectors and eigenvalues!
This is because eigs
is essentially using an algorithm like the power method, that only uses repeated multiplication by $A$.
Or, sometimes, particularly to find the smallest $|\lambda|$ eigenvectors, it might repeatedly divide by $A$, i.e. solve $Ax=b$ for $b = A^{-1} x$. But it can't actually compute the inverse matrix, and I said that LU factorization was $\sim n^3$ in general. So, what is happening?
Even if $A$ is a sparse matrix, $A^{-1}$ is generally not sparse. However, if you arrange things cleverly, often the $L$ and $U$ factors are still sparse!
This leads to something called sparse-direct solvers: they solve $Ax=b$ by ordinary Gaussian elimination to find $A = LU$, but they take advantage of sparse $A$ to avoid computing with zeros. Moreover, they first re-order the rows and columns of $A$ so that elimination won't introduce too many zeros — this is a tricky problem that mostly involves graph theory, so I won't try to explain it here.
Julia (and Matlab) both use sparse-direct algorithms automatically when you do A \ b
if $A$ is stored as a sparse matrix. When they work (i.e. when the L and U factors are sparse), these algorithms are great: fast, memory-efficient, reliable, and worry-free "black boxes".
The following code solves a scalar Helmholtz equation
$$ \left[ -\nabla^2 - \omega^2 \right] u = f(x,y) $$This equation describes the propagation of waves u from a source f at a frequency $\omega$ is the frequency. For example, imagine water waves travelling across a shallow pond, with $u(x,y)$ being the height of the wave, where $f$ represents an vibrating disturbance that creates the wave.
We discretize this into a matrix equation $Au=f$ by discretizing space $(x,y)$ into a grid and approximating derivatives $-\nabla^2$ by differences on the grid (this is an FDFD method).
Again, don't worry too much about the details of this construction (take 18.303 to find out more). The important thing is that we will have a grid (graph!) of many unknowns, but the problem is sparse because each grid point only "talks" to its 4 nearest neighbors.
"""
Return `(A,Nx,Ny,x,y)` for the 2d Helmholtz problem.
"""
function Helmholtz2d(Lx, Ly, ε, ω; dpml=2, resolution=20, Rpml=1e-20)
# PML σ = σ₀ x²/dpml², with σ₀ chosen so that
# the round-trip reflection is Rpml:
σ₀ = -log(Rpml) / (4dpml/3)
M = round(Int, (Lx+2dpml) * resolution)
N = round(Int, (Ly+2dpml) * resolution)
dx = (Lx+2dpml) / (M+1)
dy = (Ly+2dpml) / (N+1)
x = (1:M) * dx .- Lx/2 .- dpml # x grid, centered
y = (1:N) * dy .- Ly/2 .- dpml # y grid, centered
# 1st-derivative matrix
x′ = ((0:M) .+ 0.5)*dx # 1st-derivative grid points
y′ = ((0:N) .+ 0.5)*dy # 1st-derivative grid points
ox = fill(1/dx, M)
oy = fill(1/dy, N)
σx = [ξ < dpml ? σ₀*(dpml-ξ)^2 : ξ > Lx+dpml ? σ₀*(ξ-(Lx+dpml))^2 : 0.0 for ξ in x′]
σy = [ξ < dpml ? σ₀*(dpml-ξ)^2 : ξ > Ly+dpml ? σ₀*(ξ-(Ly+dpml))^2 : 0.0 for ξ in y′]
Dx = spdiagm(0=>inv.(@. 1+(im/ω)*σx)) * spdiagm_nonsquare(M+1,M, -1=>-ox, 0=>ox)
Dy = spdiagm(0=>inv.(@. 1+(im/ω)*σy)) * spdiagm_nonsquare(N+1,N, -1=>-oy, 0=>oy)
Ix = sparse(I,M,M)
Iy = sparse(I,N,N)
return (kron(Ix, transpose(Dy)*Dy) + kron(transpose(Dx)*Dx, Iy) -
spdiagm(0=>vec([ω^2 * ε(ξ, ζ) for ζ in y, ξ in x])),
M, N, x, y)
end
Helmholtz2d
Let's set up a scattering problem with a cylindrical scatterer (a slightly slower wave speed, e.g. a different depth of water, inside a small cylindrical region, with a wavelength $2\pi/\omega$ of 1:
A, Nx, Ny, x, y = Helmholtz2d(20,20, (x,y) -> hypot(x,y) < 0.5 ? 12 : 1, 2π)
size(A), nnz(A) / size(A,1)
((230400, 230400), 4.991666666666666)
Again, this is a huge matrix: $230400 \times 230400$. But it is incredibly sparse, so solving it will be no problem:
nnz(A)/length(A)
2.166521990740741e-5
For the right-hand side b
, let's use a "point" source on one side.
We'll use the reshape
function to convert between 2d arrays and
column vectors for solving with A
. Note that the solution is complex, since it corresponds physically to an oscillating solution $u(x,y)e^{-i\omega t}$ and $u$ has a phase angle due to the absorbing boundary layers (which make A
non-Hermitian); we'll just plot the real part.
b = zeros(Nx,Ny)
b[Nx÷2, Ny÷4] = 1
@time u = reshape(A \ reshape(b, Nx*Ny), Nx, Ny)
s = maximum(abs, real(u)) / 10
imshow(real(u), cmap="RdBu", vmin=-s, vmax=s,
extent=(minimum(x),maximum(x),minimum(y),maximum(y)))
6.754485 seconds (5.10 M allocations: 879.981 MiB, 6.99% gc time)
PyObject <matplotlib.image.AxesImage object at 0x1414e5438>
We solved a $200000\times200000$ matrix problem in 2–3 seconds, and less than 1GB of memory. Pretty good!
Unfortunately, sparse-direct solvers like those we are using above have two limitations:
They only work if the matrix is sparse. There are lots of problems where $A$ has some special structure that lets you compute $A*x$ quickly, e.g. by FFTs, and avoid storing the whole matrix, but for which $A$ is not sparse.
They scale poorly if the sparse matrix comes from a 3d grid or mesh. For an $s$-element 1d mesh with $n=s$ degrees of freedom, they have $O(s)$ complexity. For an $s \times s$ 2d mesh with $n=s^2$ degrees of freedom, they take $O(n \log n)$ operations and require $O(n)$ storage. But for a 3d $s\times s\times s$ mesh with $n = s^3$, they take $O(n^2)$ operations and require $O(n^{4/3})$ storage (and you often run out of storage before you run out of time).
The alternative is an iterative solver, in which you supply an initial guess for the solution $x$ (often just $x=0$) and then it iteratively improves the guess, converging (hopefully) to the solution $A^{-1} b$, while using only matrix-vector operations $Ax$.
Iterative solvers are the method of choice (or, more accurately, of necessity) for the very largest problems, but they have their downsides. There are many iterative solver algorithms, and you have to know a little bit to pick the best one. They may not converge at all for non-symmetric $A$, and in any case may converge very slowly, unless you provide a "magic" matrix called a preconditioner that is specific to your problem. (It is often a research problem in itself to find a good preconditioner!)
If $A$ is a real-symmetric positive-definite matrix, then solving $Ax = b$ is equivalent to minimizing the function:
$$ f(x) = x^T A x - x^T b - b^T x $$Just compute $\nabla f = \cdots = 2(Ax - b)$, which equals zero at the minimum. The definiteness of $A$ means that the function $f$ is convex, so there is exactly one global minimum. Another way to see this: if $Az=b$, then $f(z+v) - f(z) = \cdots = v^T A v > 0$ for any vector $v \ne 0$, so $f(z)$ must be the minimum.
One of the simplest iterative algorithms is just to go downhill: minimize $f(x + \alpha d)$ over $\alpha$, where $d$ is the downhill direction $-\frac{1}{2}\nabla f = b - Ax = r$, where $r$ is called the residual. We can perform this line minimization analytically for this $f$, for an arbitrary $d$, to find $\alpha = d^T r / d^T A d$.
The steepest-descent algorithm simply performs this downhill line-minimization repeatedly, starting at an initial guess x
(typically just x=0
), e.g. stopping when the norm of the residual is less than some tolerance times the norm of b
.
function SD(A, b, x=0.0*b; tol=1e-8, maxiters=1000)
bnorm = norm(b)
r = b - A*x # initial residual
rnorm = [norm(r)] # return the array of residual norms
Ad = similar(r) # allocate space for Ad
for i = 1:maxiters
d = r # use the steepest-descent direction
mul!(Ad, A, d) # store matvec A*d in-place in Ad
α = dot(d, r) / dot(d, Ad)
@. x = x + α * d # @. is a way to get this in a single loop in-place
@. r = r - α * Ad # update the residual (without computing A*x again)
push!(rnorm, norm(r))
rnorm[end] ≤ tol*bnorm && break # converged
end
return x, rnorm
end
SD (generic function with 2 methods)
A = rand(100,100); A = A'*A # a random SPD matrix
b = rand(100)
x, rnorm = SD(A, b, maxiters=1000)
length(rnorm), rnorm[end]/norm(b)
(1001, 0.3185655983696876)
semilogy(rnorm)
title("steepest-descent convergence")
ylabel("residual norm")
xlabel("iterations")
PyObject Text(0.5, 24.0, 'iterations')
To see what's going on, let's try a $2\times2$ matrix where we can easily visualize the progress.
θ = 0.9 # chosen to make a nice-looking plot
Q = [cos(θ) sin(θ); -sin(θ) cos(θ)] # 2x2 rotation by θ
A = Q * diagm(0=>[10,1]) * Q' # a 2x2 matrix with eigenvalues 10,1
b = A * [1,1] # right-hand side for solution (1,1)
x1 = range(-11,11,length=100)
contour(x1', x1, [dot([x1,x2], A*[x1,x2]) - 2*(x1*b[1]+x2*b[2]) for x1 in x1, x2 in x1], levels=range(1,2000,length=100))
plot(1,1, "r*")
x1s = Float64[]
x2s = Float64[]
for i = 0:20
x, = SD(A, b, [-10.,-10.], maxiters=i)
push!(x1s, x[1])
push!(x2s, x[2])
end
plot(x2s, x1s, "k.-")
title("convergence of 2x2 steepest-descent")
PyObject Text(0.5, 1.0, 'convergence of 2x2 steepest-descent')
The solution "zig-zags" down the long, narrow valley defined by the quadratic function f
. This is a common problem of steepest-descent algorithms: they tend to go towards the center of valleys (down the "steep" direction), rather than along the valleys towards the solution.
To fix this problem, basically we need to implement some kind of "memory": it has to "remember" that it just "zigged" in order to avoid "zagging" back where it came from.
The most famous way to improve steepest descent with "memory" is the conjugate-gradient algorithm. I won't explain it here (Shewchuk's article is a good introduction to its relationship to steepest descent), but the implementation ends up being almost identical to steepest descent. However, instead of setting the line-search direction equal to the downhill direction r
, the line-search direction is instead a linear combination of r
with the previous search direction:
function CG(A, b, x=0.0*b; tol=1e-8, maxiters=1000)
bnorm = norm(b)
r = b - A*x # initial residual
rnorm = [norm(r)] # return the array of residual norms
d = copy(r) # initial direction is just steepest-descent
Ad = similar(r) # allocate space for Ad
for i = 1:maxiters
mul!(Ad, A, d) # store matvec A*d in-place in Ad
α = dot(d, r) / dot(d, Ad)
@. x = x + α * d # @. is a way to get this in a single loop in-place
@. r = r - α * Ad # update the residual (without computing A*x again)
push!(rnorm, norm(r))
@. d = r + d * (rnorm[end]/rnorm[end-1])^2 # conjugate direction update
rnorm[end] ≤ tol*bnorm && break # converged
end
return x, rnorm
end
CG (generic function with 2 methods)
A = rand(100,100); A = A'*A # a random SPD matrix
b = rand(100)
x, rnorm = CG(A, b)
length(rnorm), rnorm[end]/norm(b)
(223, 9.583684275765905e-9)
After some initial slow progress, the conjugate-gradient algorithm quickly zooms straight to the solution:
semilogy(rnorm)
title("conjugate-gradient convergence")
ylabel("residual norm")
xlabel("iterations")
PyObject Text(0.5, 24.0, 'iterations')
In our 2x2 problem, you can see that it actually converges in two steps:
θ = 0.9 # chosen to make a nice-looking plot
Q = [cos(θ) sin(θ); -sin(θ) cos(θ)] # 2x2 rotation by θ
A = Q * diagm(0=>[10,1]) * Q' # a 2x2 matrix with eigenvalues 10,1
b = A * [1,1] # right-hand side for solution (1,1)
x1 = range(-11,11,length=100)
contour(x1', x1, [dot([x1,x2], A*[x1,x2]) - 2*(x1*b[1]+x2*b[2]) for x1 in x1, x2 in x1], levels=range(1,2000,length=100))
plot(1,1, "r*")
x1s = Float64[]
x2s = Float64[]
for i = 0:2
x, = CG(A, b, [-10.,-10.], maxiters=i)
push!(x1s, x[1])
push!(x2s, x[2])
end
plot(x2s, x1s, "k.-")
title("convergence of 2x2 conjugate-gradient")
PyObject Text(0.5, 1.0, 'convergence of 2x2 conjugate-gradient')
There are several packages out there with iterative solvers that you can use, e.g. the IterativeSolvers package:
# do this if you haven't installed it yet: ] add IterativeSolvers
using IterativeSolvers
A = rand(100,100); A = A'*A # a random SPD matrix
b = rand(100)
x, ch = cg(A, b, maxiter=300, log=true)
norm(A*x - b) / norm(b)
1.0186741997168474e-8
semilogy(ch[:resnorm])
xlabel("iteration")
ylabel(L"\Vert \mathrm{residual} \Vert")
title("convergence of conjugate gradient")
PyObject Text(0.5, 1.0, 'convergence of conjugate gradient')
Most iterative solvers are greatly accelerated if you can provide a preconditioner $P$: roughly, an approximate inverse of $A$ that is easy to compute. The preconditioner is applied at every step of the iteration in order to speed up convergence. (For example, you could solve $PAx = Pb$ instead of $Ax=b$; this is called a "left" preconditioner.)
For example, let's consider a problem where the matrix $A = L + S$ is a sum of the symmetric-tridiagonal discrete Laplacian $L$ (from above) and a small, sparse perturbation $S$. As our preconditioner, we'll simply use $P = L^{-1}$, since this is a good approximation for $A$ and L \ b
is fast (linear time):
n = 300
L = SymTridiagonal(fill(2.0,n), fill(-1.0, n-1))
b = rand(n)
S = sprand(n,n,0.001) * 0.1 # a small random, sparse perturbation
S = S'*S # needs to be symmetric positive-definite
A = sparse(L + S)
300×300 SparseMatrixCSC{Float64,Int64} with 910 stored entries: [1 , 1] = 2.0 [2 , 1] = -1.0 [1 , 2] = -1.0 [2 , 2] = 2.0 [3 , 2] = -1.0 [2 , 3] = -1.0 [3 , 3] = 2.00005 [4 , 3] = -1.0 [3 , 4] = -1.0 [4 , 4] = 2.0 [5 , 4] = -1.0 [4 , 5] = -1.0 ⋮ [297, 296] = -1.0 [296, 297] = -1.0 [297, 297] = 2.0 [298, 297] = -1.0 [297, 298] = -1.0 [298, 298] = 2.0 [299, 298] = -1.0 [298, 299] = -1.0 [299, 299] = 2.0 [300, 299] = -1.0 [299, 300] = -1.0 [300, 300] = 2.0
Our home-brewed CG
function above does not accept a preconditioner, but the IterativeSolvers package cg
function does, and it makes a huge difference in the convergence:
x, ch = cg(A, b, maxiter=300, log=true)
x′, ch′ = cg(A, b, Pl=ldlt(L), maxiter=300, log=true)
semilogy(ch[:resnorm], "b-")
semilogy(ch′[:resnorm], "r-")
xlabel("iterations")
ylabel("residual norm")
legend(["no preconditioner", "preconditioner"], loc="lower right")
PyObject <matplotlib.legend.Legend object at 0x13ea07f98>
If you can find a good preconditioner, you can often speed things up by orders of magnitude. Unfortunately, finding preconditioners is hard and problem-dependent. There are some general "recipes" for things to try, but there are many problems (like the scalar-Helmholtz problem above) where good preconditioners are still an open research problem.
What makes a good preconditioner matrix $P$ for a matrix $A$? For the "ideal" preconditioner $P = A^{-1}$, you would have $PA = I$, but of course this is impractical: if you could compute $A^{-1}$ quickly, you wouldn't need an iterative solver. So, what you want is for $PA$ to be "like" $I$ in some sense. The eigenvalues of $I$ are all 1, and it turns out that a good preconditioner makes $PA$ have eigenvalues that are mostly clustered together.
Let's see how the eigenvalues of $A$ and $PA = L^{-1} A$ compare in this case.
semilogy(sort!(eigvals(Matrix{Float64}(A))), "b.")
semilogy(sort!(eigvals(Matrix{Float64}(A),Matrix{Float64}(L))), "r.")
xlabel(L"index $k$ of eigenvalue $\lambda_k$")
ylabel(L"eigenvalues $\lambda_k$")
legend([L"unpreconditioned $A$", L"preconditioned $L^{-1} A$"])
PyObject <matplotlib.legend.Legend object at 0x14168a470>
You can see from the plot that $A$ has eigenvalues all over the map (from $10^{-3}$ to 10), but $L^{-1} A$ has eigenvalues that are mostly 1 (like $I$) with a handful of outliers.
Large matrix problems ($> 10^4 \times 10^4$) almost have a matrix $A$ with a special structure that allows you to multiply Ax quickly. Most commonly, $A$ is sparse (mostly zero).
Many specialized methods are available to take advantage of this special structure in order to solve problems much faster than the n³ time and n² memory of "dense" solvers.
For sparse matrices, A \ b
can actually be solved directly by ordinary Gaussian elimination, cleverly re-ordering the unknowns so that the LU factors remain sparse. These "sparse direct" methods are easy to use and widely available.
For the very largest matrices, for eigenproblems, or non-sparse matrices with a fast Ax, the alternative is an iterative method: start with guess for the solution, and do some clever operations that rapidly improve the guess using only matrix-times-vector.
In many problems, you can speed up iterative solvers by orders of magnitude if you have an approximate solution for Ax=b, even a very "bad" approximation that only uses part of th matrix. Finding such preconditioners is often more of an art than a science.
Fully understanding the methods for large sparse problems goes far beyond the scope of 18.06. Better coverage is given in 18.335. But it is important to know that such methods are out there, even if you don't understand the detail, so that you know where to begin looking if you encounter a huge matrix in practice.