Code
# the following matrix is indefinite
= FNC.poisson(10) - 20I
A = eigvals(Matrix(A))
λ = @. λ < 0
isneg @show sum(isneg),sum(.!isneg);
(sum(isneg), sum(.!(isneg))) = (13, 87)
We have seen before that certain matrix properties enhance solutions to linear algebra problems. One of the most important of these is when \(\mathbf{A}^T=\mathbf{A}\); i.e., \(\mathbf{A}\) is symmetric. The Arnoldi iteration has a particularly useful specialization to this case.
Starting from \(\mathbf{A}\mathbf{Q}_m = \mathbf{Q}_{m+1} \mathbf{H}_m\), we left-multiply by \(\mathbf{Q}_m^*\) to get
\[\mathbf{Q}_m^* \mathbf{A} \mathbf{Q}_m = \mathbf{Q}_m^* \mathbf{Q}_{m+1} \mathbf{H}_m = \tilde{\mathbf{H}}_m,\]
where \(\tilde{\mathbf{H}}_m\) is rows 1 through \(m\) of \(\mathbf{H}_m\).
If \(\mathbf{A}\) is symmetric, then so is the left side of this equation, hence \(\tilde{\mathbf{H}}_m\) is symmetric too. But it is also upper Hessenberg, meaning that the \((i,j)\) element is zero if \(i > j+1\). By symmetry, this means that elements are zero when \(j > i+1\) as well.
In general, a symmetric, upper Hessenberg matrix is tridiagonal!
The equation \(\mathbf{A}\mathbf{q}_m = \mathbf{H}_{1m} \mathbf{q}_1 + \mathbf{H}_{2m} \mathbf{q}_2 + \cdots + \mathbf{H}_{m+1,m} \mathbf{q}_{m+1}\) shortens to \[\mathbf{A} \mathbf{q}_m = H_{m-1,m} \,\mathbf{q}_{m-1} + H_{mm} \,\mathbf{q}_m + H_{m+1,m}\,\mathbf{q}_{m+1}.\]
As before in deriving the Arnoldi iteration, when given the first \(m\) vectors we can solve for the entries in column \(m\) of \(\mathbf{H}\) and then for \(\mathbf{q}_{m+1}\). The resulting process is known as the Lanczos iteration.
Its most important practical advantage is that while Arnoldi needs \(O(m)\) steps to get \(\mathbf{q}_{m+1}\) from the previous vectors, Lanczos needs only \(O(1)\) steps, so restarting isn’t required for symmetric matrices.
When \(\mathbf{A}\) is symmetric and the Arnoldi iteration is reduced to Lanczos, the analog of GMRES is known as MINRES. Like GMRES, MINRES minimizes the residual \(\|\mathbf{b}-\mathbf{A}\mathbf{x}\|\) over increasingly larger Krylov spaces.
MINRES is also more theoretically tractable than GMRES. Recall that the eigenvalues of a hermitian matrix are real.
The bound for a definite matrix is better, as the next theorem shows. This upper bound on the residual obeys a linear convergence rate. As the product \(\kappa_+\kappa_-\) grows, the rate of this convergence approaches 1. Hence the presence of eigenvalues close to the origin (relative to the max eigenvalues) is expected to force a slower convergence.
Because the theorem gives an upper bound, MINRES may converge faster. All we can say is that the theorem guaranteed value is certain to be enough iterations.
# the following matrix is indefinite
= FNC.poisson(10) - 20I
A = eigvals(Matrix(A))
λ = @. λ < 0
isneg @show sum(isneg),sum(.!isneg);
(sum(isneg), sum(.!(isneg))) = (13, 87)
# compute the relevant quantities from the theorem
= extrema(-λ[isneg])
mn,mx = mx/mn
κ₋ = extrema(λ[.!isneg])
mn,mx = mx/mn
κ₊ = (sqrt(κ₋*κ₊)-1) / (sqrt(κ₋*κ₊)+1) ρ
0.9026418585584018
Compare the behavior of MINRES to the upper bound given by the theorem.
= rand(100)
b = minres(A,b,reltol=1e-10,maxiter=51,log=true);
x,hist
= hist[:resnorm] / norm(b)
relres = 0:length(relres)-1
m plot(m,relres,label="observed",leg=:left,
=L"m",yaxis=(:log10,"relative residual"),
xaxis=("Convergence of MINRES") )
titleplot!(m,ρ.^(m/2),l=:dash,label="upper bound")
Given positive definiteness in addition to symmetry, we arrive at perhaps the most famous Krylov subspace method for \(\mathbf{A}\mathbf{x}=\mathbf{b}\), called conjugate gradients.
Suppose now that \(\mathbf{A}\) is symmetric and positive definite (SPD). Then \(\mathbf{A}\) has a Cholesky factorization, \(\mathbf{A}=\mathbf{R}^\top\mathbf{R}\). Therefore, for any vector \(\mathbf{u}\),
\[ \mathbf{u}^*\mathbf{A}\mathbf{u} = (\mathbf{R}\mathbf{u})^*(\mathbf{R}\mathbf{u})=\|\mathbf{R} \mathbf{u}\|^2, \]
which is nonnegative and zero only when \(\mathbf{u}=\boldsymbol{0}\), provided \(\mathbf{A}\) (and therefore \(\mathbf{R}\)) is nonsingular.
Hence we can define a special vector norm relative to \(\mathbf{A}\):
\[\| \mathbf{u} \|_{\mathbf{A}} = \left( \mathbf{u}^*\mathbf{A}\mathbf{u} \right)^{1/2}.\]
The convergence of CG and MINRES is dependent on the eigenvalues of \(\mathbf{A}\). In the SPD case the eigenvalues are real and positive, and they equal the singular values. Hence the condition number \(\kappa\) is equal to the ratio of the largest eigenvalue to the smallest one. The following theorem suggests that MINRES and CG are not so different in convergence.
This theorem characterizes the convergence of MINRES and CG similarly, differing only in whether the measurement is of the residual or the \(\mathbf{A}\)-norm of the error, respectively. While these are different quantities, in practice one may not find a consistent advantage for one method over the other.
Specifically, to make the bound in this theorem less than a number \(\epsilon\) requires
\[\begin{gather*} 2 \left( \frac{\sqrt{\kappa} - 1}{\sqrt{\kappa} + 1} \right)^m \approx \epsilon, \\ m \log \left( \frac{\sqrt{\kappa} - 1}{\sqrt{\kappa} + 1} \right) \approx \log\Bigl( \frac{\epsilon}{2} \Bigr). \end{gather*}\]
We estimate
\[\begin{align*} \frac{\sqrt{\kappa} - 1}{\sqrt{\kappa} + 1} &= (1 - \kappa^{-1/2}\,) (1 + \kappa^{-1/2}\,)^{-1}\\ &= (1 - \kappa^{-1/2}\,) (1 - \kappa^{-1/2} + \kappa^{-1} + \cdots)\\ &= 1 - 2\kappa^{-1/2} + O(\kappa^{-1}) \quad \text{ as $\kappa \rightarrow \infty$.} \end{align*}\]
With the Taylor expansion \(\log(1+x) = x - (x^2/2) + \cdots\), we finally conclude
\[\begin{gather*} 2 m \kappa^{-1/2} \approx \log\Bigl( \frac{\epsilon}{2} \Bigr), \text{ or } m = O(\sqrt{\kappa}), \end{gather*}\]
as an estimate of the number of iterations needed to achieve a fixed accuracy.
This estimate fails for very large \(\kappa\), however.
We will compare MINRES and CG on some quasi-random SPD problems.
# the first matrix has a condition number of 100
= 5000
n = 0.001
density = FNC.sprandsym(n,density,1/100)
A = (1:n)/n
x = A*x; b
= plot(title="Convergence of MINRES and CG",
plt =("Krylov dimension"),yaxis=(:log10,"relative residual norm"))
xaxisfor method in [minres,cg]
= method(A,b,reltol=1e-6,maxiter=1000,log=true);
x̃,history = history[:resnorm] / norm(b)
relres plot!(0:length(relres)-1,relres,label="$method")
= round( norm( x̃ - x ) / norm(x), sigdigits=4 )
err println("$method error: $err")
end
plt
minres error: 1.204e-5
cg error: 5.086e-6
There is little difference between the two methods here.
Next, we increase the condition number of the matrix by a factor of 25. The rule of thumb predicts that the number of iterations required should increase by a factor of about 5.
= FNC.sprandsym(n,density,1/2500)
A = A*x; b
= plot(title="Convergence of MINRES and CG",
plt =("Krylov dimension"),yaxis=(:log10,"relative residual norm"))
xaxisfor method in [minres,cg]
= method(A,b,reltol=1e-6,maxiter=1000,log=true);
x̃,history = history[:resnorm] / norm(b)
relres plot!(0:length(relres)-1,relres,label="$method")
= round( norm( x̃ - x ) / norm(x), sigdigits=4 )
err println("$method error: $err")
end
plt
minres error: 0.0002572
cg error: 4.226e-5
Both methods have an early superlinear phase that allow them to finish slightly sooner than the factor of 5 predicted: the theorem is an upper bound, not necessarily an approximation. Both methods ultimately achieve the same reduction in the residual; MINRES stops earlier, but with a slightly larger error.
A primary reason for our interest in matrices is their relationship to linear transformations.
Recall that every linear transformation between finite-dimensional vector spaces can be represented as a matrix-vector multiplication.
Recall that with the fixed-point iterative method, we solved the nonlinear rootfinding problem \(\mathbf{f}(\mathbf{x})=\boldsymbol{0}\) with methods that needed only the ability to evaluate \(\mathbf{f}\) at any known value of \(\mathbf{x}\). By repeatedly evaluating \(\mathbf{f}\) at cleverly chosen points, these algorithms were able to return an estimate for \(\mathbf{f}^{-1}(\boldsymbol{0})\).
A close examination reveals that the power method and Krylov subspace methods have the same structure because the only appearance of the matrix \(\mathbf{A}\) in them is to multiply a known vector, i.e., to evaluate \(\mathbf{f}(\mathbf{x})=\mathbf{A}\mathbf{x}\). This is used to evaluate the inverse, \(\mathbf{A}^{-1}\mathbf{b}\).
Bringing these points of view together leads us to a cornerstone of modern scientific computation: matrix-free iterations. Krylov subspace methods can be used to invert a linear transformation if one provides code for the transformation, even if its associated matrix is not known explicitly.
Previously we saw that a grayscale image can be represented as an \(m\times n\) matrix \(\mathbf{X}\) of pixel intensity values. Now consider a simple model for blurring the image. Define \(\mathbf{B}_m\) as the \(m\times m\) tridiagonal matrix
\[(\mathbf{B}_m)_{ij} = \begin{cases} \tfrac{1}{2} & \text{if $i=j$},\\ \tfrac{1}{4} & \text{if $|i-j|=1$},\\ 0 & \text{otherwise.} \end{cases}\]
The product \(\mathbf{B}_m\mathbf{X}\) applies \(\mathbf{B}_m\) to each column of \(\mathbf{X}\). Within that column it does a weighted average of the values of each pixel and its two neighbors. That has the effect of blurring the image vertically. We can increase the amount of blur by applying \(\mathbf{B}_m\) repeatedly.
To blur the image horizontally, we can apply \(\mathbf{B}_n\) to each row of \(\mathbf{X}\). The product \(\mathbf{X}\mathbf{B}_n\) applies \(\mathbf{B}_n\) to each row of \(\mathbf{X}\), and has the effect of blurring the image horizontally.
So we can describe blur in both directions as the function
\[\operatorname{blur}(\mathbf{X}) = \mathbf{B}_m^k \mathbf{X} \mathbf{B}^k_n\]
for a positive integer \(k\).
= testimage("mandrill")
img = size(img)
m,n = @. Float64(Gray(img))
X plot(Gray.(X),title="Original image",aspect_ratio=1)
# define the one-dimensional tridiagonal blurring matrices
function blurmatrix(d)
= fill(0.25,d-1)
v1 return spdiagm(0=>fill(0.5,d), 1=>v1, -1=>v1)
end
= blurmatrix(m),blurmatrix(n); Bm,Bn
# the results of using $k=12$ repetitions of the blur in each direction
= X -> Bm^12 * X * Bn^12;
blur = blur(X)
Z plot(Gray.(Z),title="Blurred image")
A more interesting operation is deblurring: given an image blurred by poor focus, can we reconstruct the true image? Conceptually, we want to invert the function \(\operatorname{blur}(\mathbf{X})\).
It’s easy to see from the definition of this function that the blur operation is a linear transformation on image matrices. But an \(m\times n\) image matrix is equivalent to a length-\(mn\) vector—it’s just a matter of interpreting the shape of the same data. Let \(\operatorname{vec}(\mathbf{X})=\mathbf{x}\) and \(\operatorname{unvec}(\mathbf{x})=\mathbf{X}\) be the mathematical statements of such reshaping operations. Now say \(\mathbf{X}\) is the original image and \(\mathbf{Z}=\operatorname{blur}(\mathbf{X})\) is the blurred one.
Then by linearity there is some matrix \(\mathbf{A}\) such that
\[\mathbf{A} \operatorname{vec}(\mathbf{X}) = \operatorname{vec}(\mathbf{Z}),\]
or \(\mathbf{A}\mathbf{x}=\mathbf{z}\).
The matrix \(\mathbf{A}\) is \(mn\times mn\); for a 12-megapixel image, it would have \(1.4\times 10^{14}\) entries! Admittedly, it is extremely sparse, but the point is that we don’t need it at all.
Instead, given any vector \(\mathbf{u}\) we can compute \(\mathbf{v}=\mathbf{A}\mathbf{u}\) through the steps
\[\begin{align*} \mathbf{U} &= \operatorname{unvec}(\mathbf{u}),\\ \mathbf{V} &= \operatorname{blur}(\mathbf{U}),\\ \mathbf{v} &= \operatorname{vec}(\mathbf{V}). \end{align*}\]
The following example shows how to put these ideas into practice with MINRES.
# repeat the earlier process to blur an original image X to get Z
= testimage("lighthouse")
img = size(img)
m,n = @. Float64(Gray(img))
X
= spdiagm(0=>fill(0.5,m),
B 1=>fill(0.25,m-1),-1=>fill(0.25,m-1))
= spdiagm(0=>fill(0.5,n),
C 1=>fill(0.25,n-1),-1=>fill(0.25,n-1))
= X -> B^12 * X * C^12
blur = blur(X)
Z plot(Gray.(Z),title="Blurred image")
Now imagine that \(\mathbf{X}\) is unknown and that we want to recover it from \(\mathbf{Z}\).
# vec (built-in) converts matrix to vector
= z -> reshape(z,m,n); # convert vector to matrix unvec
= LinearMap(x -> vec(blur(unvec(x))),m*n); T
= minres(T,vec(Z),maxiter=50,reltol=1e-5);
y # function clamp01 in Images restricts values to be in the interval [0,1]
= unvec( clamp01.(y) )
Y
plot(Gray.(X),layout=2,title="Original")
plot!(Gray.(Y),subplot=2,title="Deblurred")