from traitlets.config.manager import BaseJSONConfigManager
import jupyter_core
path = "/Users/i.oseledets/anaconda2/envs/teaching/etc/jupyter/nbconfig"
cm = BaseJSONConfigManager()
cm.update("livereveal", {
"theme": "sky",
"transition": "zoom",
"start_slideshow_at": "selected",
"scroll": True
})
Concept of iterative methods for linear systems:
If we want to achieve O(N) complexity of solving sparse linear systems, then direct solvers are not appropriate.
If we want to solve partial eigenproblem, the full eigendecomposition is too costly.
For both problems we will use iterative, Krylov subspace solvers, which treat the matrix as a black-box linear operator.
We have now an absolutely different view on a matrix: matrix is now a linear operator, that acts on a vector,
and this action can be computed in O(N) operations.
This is the only information we know about the matrix: the matrix-by-vector product (matvec)
Can we solve linear systems using only matvecs?
Of course, we can multiply by the colums of the identity matrix, and recover the full matrix, but it is not what we need.
The simplest idea is the "simple iteration method" or Richardson iteration.
Ax=f,
where τ is the iteration parameter, which can be always chosen such that the method converges.
The Richardson iteration has a deep connection to the Ordinary Differential Equations (ODE).
Consider a time-dependent problem (A=A∗>0)
dydt+Ay=f,y(0)=y0.Then y(t)→A−1f as t→∞, and the Euler scheme reads
yk+1−ykτ=−Ayk+f.which leads to the Richardson iteration yk+1=yk−τ(Ayk−f)
Let x∗ be the solution; introduce an error ek=xk−x∗, then
ek+1=(I−τA)ek,therefore if ‖I−τA‖<1 in any norm, the iteration converges.
For symmetric positive definite case it is always possible to select τ such that the method converges.
What about the non-symmetric case? Below demo will be presented...
The choise of τ that minimizes ‖I−τA‖2 for A=A∗>0 is (prove it!) τopt=2λmin+λmax.
where λmin is the minimal eigenvalue, and λmax is the maximal eigenvalue of the matrix A.
So, to find optimal parameter, we need to know the bounds of the spectrum of the matrix A, and we can compute it by using power method.
Even with the optimal parameter choice, the error at the next step satisfies
‖ek+1‖2≤q‖ek‖2,→‖ek‖2≤qk‖e0‖2,where
q=λmax−λminλmax+λmin=cond(A)−1cond(A)+1,is the condition number of A.
Let us do some demo...
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rc("text", usetex=True)
import scipy as sp
import scipy.sparse
import scipy.sparse.linalg as spla
import scipy
from scipy.sparse import csc_matrix
n = 10
ex = np.ones(n);
A = sp.sparse.spdiags(np.vstack((-ex, 2*ex, -ex)), [-1, 0, 1], n, n, 'csr');
rhs = np.ones(n)
ev1, vec = spla.eigsh(A, k=2, which='LA')
ev2, vec = spla.eigsh(A, k=2, which='SA')
lam_max = ev1[0]
lam_min = ev2[0]
tau_opt = 2.0/(lam_max + lam_min)
fig, ax = plt.subplots()
plt.close(fig)
niters = 100
x = np.zeros(n)
res_richardson = []
for i in range(niters):
rr = A.dot(x) - rhs
x = x - tau_opt * rr
res_richardson.append(np.linalg.norm(rr))
#Convergence of an ordinary Richardson (with optimal parameter)
plt.semilogy(res_richardson)
plt.xlabel("Number of iterations, $k$", fontsize=20)
plt.ylabel("Residual norm, $\|Ax_k - b\|_2$", fontsize=20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
print("Maximum eigenvalue = {}, minimum eigenvalue = {}".format(lam_max, lam_min))
print("Condition number = {}".format(lam_max.real / lam_min.real))
Maximum eigenvalue = 3.6825070656623633, minimum eigenvalue = 0.08101405277100564 Condition number = 45.45516413147901
Thus, for ill-conditioned matrices the error of the simple iteration method decays very slowly.
This is another reason why condition number is so important:
Main questions for the iterative method is how to make the matrix better conditioned.
The answer is use preconditioners . Preconditioners will be discussed in further lectures.
Possible cases of Richardson iteration behaviour:
Q: how can we identify our case before running iterative method?
# B = np.random.randn(2, 2)
B = np.array([[1, 2], [-1, 0]])
# B = np.array([[0, 1], [-1, 0]])
x_true = np.zeros(2)
f = B.dot(x_true)
eigvals = np.linalg.eigvals(B)
print("Spectrum of the matrix = {}".format(eigvals))
# Run Richardson iteration
x = np.array([0, -1])
tau = 1e-2
conv_x = [x]
r = B.dot(x) - f
conv_r = [np.linalg.norm(r)]
num_iter = 1000
for i in range(num_iter):
x = x - tau * r
conv_x.append(x)
r = B.dot(x) - f
conv_r.append(np.linalg.norm(r))
Spectrum of the matrix = [0.5+1.32287566j 0.5-1.32287566j]
plt.semilogy(conv_r)
plt.xlabel("Number of iteration, $k$", fontsize=20)
plt.ylabel("Residual norm", fontsize=20)
Text(0,0.5,'Residual norm')
plt.scatter([x[0] for x in conv_x], [x[1] for x in conv_x])
plt.xlabel("$x$", fontsize=20)
plt.ylabel("$y$", fontsize=20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.title("$x_0 = (0, -1)$", fontsize=20)
Text(0.5,1,'$x_0 = (0, -1)$')
But before preconditioners, we can use better iterative methods.
There is a whole zoo of iterative methods, but we need to know just few of them.
Suppose we change τ every step, i.e.
xk+1=xk−τk(Axk−f).A possible choice of τk is such that it minimizes norm of the current residual (was considered in the pset 1).
This method is called the steepest descent.
However, it still converges similarly to the Richardson iteration.
Another way to find τk is to consider
ek+1=(I−τkA)ek=(I−τkA)(I−τk−1A)ek−1=…=p(A)e0,where p(A) is a matrix polynomial (simplest matrix function)
p(A)=(I−τkA)…(I−τ0A),and p(0)=1.
The error is written as
ek+1=p(A)e0,and hence ‖ek+1‖≤‖p(A)‖‖e0‖,
To get better error reduction, we need to minimize
‖p(A)‖over all possible polynomials p(x) of degree k+1 such that p(0)=1. We will use ‖⋅‖2.
Important special case: A=A∗>0.
Then A=UΛU∗,
and
‖p(A)‖2=‖Up(Λ)U∗‖2=‖p(Λ)‖2=maxi|p(λi)|!≤maxλmin≤λ≤λmax|p(λ)|.The latter inequality is the only approximation. Here we make a crucial assumption that we do not want to benefit from distribution of spectra between λmin and λmax.
Thus, we need to find a polynomial such that p(0)=1, that has the least possible deviation from 0 on [λmin,λmax].
We can do the affine transformation of the interval [λmin,λmax] to the interval [−1,1]:
ξ=λmax+λmin−(λmin−λmax)x2,x∈[−1,1].The problem is then reduced to the problem of finding the polynomial least deviating from zero on an interval [−1,1]
with some normalization constraint p(0)=1.
The exact solution to this problem is given by the famous Chebyshev polynomials of the form
Tn(x)=cos(narccosx)This is a polynomial!
We can express Tn from Tn−1 and Tn−2:
|Tn(x)|≤1 on x∈[−1,1].
It has (n+1) alternation points, where the maximal absolute value is achieved (this is the sufficient and necessary condition for the optimality) (Chebyshev alternance theorem, no proof here).
The roots are just
We can plot them...
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x1 = np.linspace(-1, 1, 128)
x2 = np.linspace(-1.1, 1.1, 128)
p = np.polynomial.Chebyshev((0, 0, 0, 0, 0, 0, 0, 0, 0, 1), (-1, 1)) #These are Chebyshev series, a proto of "chebfun system" in MATLAB
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot(x1, p(x1))
ax1.set_title('Interval $x\in[-1, 1]$')
ax2.plot(x2, p(x2))
ax2.set_title('Interval $x\in[-1.1, 1.1]$')
Text(0.5,1,'Interval $x\\in[-1.1, 1.1]$')
Note that p(x)=(1−τnx)…(1−τ0x), hence roots of p(x) are 1/τi and that we additionally need to map back from [−1,1] to [λmin,λmax]. This results into
τi=2λmax+λmin−(λmax−λmin)xi,xi=cosπ(2i+1)2ni=0,…,n−1The convergence (we only give the result without the proof) is now given by
ek+1≤Cqke0,q=√cond(A)−1√cond(A)+1,which is better than in the Richardson iteration.
We have made an important assumption about the spectrum: it is contained within an interval over the real line (and we need to know the bounds)
If the spectrum is contained within two intervals, and we know the bounds, we can also put the optimization problem for the optimal polynomial.
For the case of two segments the best polynomial is given by Zolotarev polynomials (expressed in terms of elliptic functions). Original paper was published in 1877, see details here
For the case of more than two segments the best polynomial can be expressed in terms of hyperelliptic functions
The implementation of the Chebyshev acceleration requires the knowledge of the spectrum.
It only stores the previous vector xk and computes the new correction vector
It belongs to the class of two-term iterative methods, i.e. it approximates xk+1 using 2 vectors: xk and rk.
It appears that if we store more vectors, then we can go without the spectrum estimation (and better convergence in practice)!
The Chebyshev method produces the approximation of the form
xk+1=x0+p(A)r0,i.e. it lies in the Krylov subspace of the matrix which is defined as
Kk(A,r0)=Span(r0,Ar0,A2r0,…,Ak−1r0)The most natural approach then is to find the vector in this linear subspace that minimizes certain norm of the error
The idea is to minimize given functional:
To make methods practical one has to
We will consider these methods in details on the next lecture.
from IPython.core.display import HTML
def css_styling():
styles = open("./styles/custom.css", "r").read()
return HTML(styles)
css_styling()