Let's look at one more way to solve the equation Ax=b. We assume that A∈Rn×n is nonsingular, and define the k-th Krylov subspace as follows:
Kk:=span{b,Ab,⋯,Ak−1b}.
Krylov subspace methods are efficient and popular iterative methods for solving large, sparse linear systems. When A is symmetric, positive definite (SPD), i.e.
{AT=AxTAx>0,∀x=0,
we can use a Krylov subspace method called Conjugate Gradient (CG).
Today, let's find out how CG works and use it to solve 2D Poisson's equation.
4. Conjugate Gradient
a. Creating iterates
Let (⋅,⋅) be an inner product for Rn, and ∣∣⋅∣∣:=(⋅,⋅) the norm associated with the inner product.
How Krylov subspace methods work is simple. We will approximate the exact solution x=A−1b with a sequence of iteratesxk that meet two conditions:
1.xk∈Kk2.∣∣xk−x∣∣=y∈Kkmin∣∣y−x∣∣.
In other words, when measured in the norm ∣∣⋅∣∣, xk is the best approximation to x from the k-th Krylov subspace.
(For initialization, we will take x0=0 and K0={0}.)
The optimality condition above is the same as the orthogonality condition xk−x⊥Kk under the inner product (⋅,⋅). Therefore, xk satisfies the equation,
(xk−x,y)=0,∀y∈Kk.
Lemma 1.
The orthogonality condition and the fact that Kk−1⊂Kk imply two things. At every iteration k≥1,
1.xk−xk−1∈Kk2.xk−xk−1⊥Kk−1.
Proof.
First, note that the vector
xk−xk−1≡(xk−x)−(xk−1−x)
represents the decrease in error over 1 iteration.
Since xk∈Kk and xk−1∈Kk−1⊂Kk, we know that xk−xk−1∈Kk.
Furthermore, for any y∈Kk−1(⊂Kk),
(xk−xk−1,y)==0(xk−x,y)−=0(xk−1−x,y)=0.
■
b. Creating residuals
Next, we select a vector pk=0 that meets these conditions:
1.pk∈Kk2.pk⊥Kk−1.
(For initialization, we will take p1=b.)
The vector xk−xk−1 also satisfied these conditions, so the two are linearly dependent. There is some constant αk∈R\{0} such that xk−xk−1=αkpk.
We now have a way to compute the best approximation xk from the previous one:
xk=xk−1+αkpk,∀k≥1.
This holds, provided we have a search directionpk.
Since xk−x⊥Kk and pk∈Kk, the LHS is equal to 0. Hence,
αk=−(pk,pk)(xk−1−x,pk).
Note, rk−1=A(x−xk−1).
■
Lemma 3.
The residuals are related in the following manner:
rk=rk−1−αkApk,∀k≥1.
Proof.
Apply A to both sides of the equation:
(x−xk)=(x−xk−1)−αkpk.
■
c. Creating search directions
We will now construct the search direction pk+1 so that we can continue the iteration. Along the way, we required the search directions to satisfy,
pi∈Ki\{0}andpi⊥Ki−1,∀i=1,⋯,k.
Thus, if pk+1 also meets these conditions, namely,
pk+1∈Kk+1\{0}andpk+1⊥Kk,
then {p1,⋯,pk+1} will end up forming an orthogonal basis for Kk+1.
We can create such a sequence using Gram-Schmidt method! Specifically, pk+1 will be the sum of a vector in Kk (which can be written as a linear combination of p1,⋯,pk) and an offshootwk∈Kk+1\Kk:
pk+1=wk+i=1∑kγipi,∀k≥1.
Then, we orthogonalize pk+1 against p1,⋯,pk.
Lemma 4.
In the k-th iteration, the coefficients γi are given by,
γi=−(pi,pi)(wk,pi),∀i=1,⋯,k.
Proof.
Given the equation,
pk+1=wk+j=1∑kγjpj,
we can take the inner product with pi:
(pk+1,pi)=(wk,pi)+j=1∑kγj(pj,pi).
Since pk+1⊥Ki and pi∈Ki, the LHS is equal to 0. Furthermore, {p1,⋯,pk} forms an orthogonal basis, so the summation on the RHS reduces to a single term:
j=1∑kγj(pj,pi)=γi(pi,pi).
We can now solve for γi:
γi=−(pi,pi)(wk,pi).
■
Isn't it cool to work backwards and see the motivations? We just need to figure out how to choose wk∈Kk+1\Kk.
d. SPD
So far, we have not used the fact that A is SPD. We do so now to obtain CG.
Lemma 5.
If A∈Rn×n is SPD, then
(x,y)A:=xTAy
defines an inner product for Rn. Then, ∣∣x∣∣A:=(x,x)A gives us a norm, called the A-norm.
Proof.
Let's show that, for all vectors x,y,z∈Rn and scalars α,β∈R, the A-inner product is symmetric, bilinear, and positive-definite:
Symmetric:
(x,y)A=xTAy=xTATy=(Ax)Ty=yT(Ax)=(y,x)A.
Notice how we used symmetry of the Euclidean inner product.
Since the A-inner product is symmetric, we also have linearity in the second argument.
Positive-definite:
Because A is positive definite,
(x,x)A=xTAx>0,∀x=0.
The A-inner product is equal to zero only when x=0.
■
The key thing to realize is that Sections 4a – 4c are valid for any nonsingular matrix A. We can create a Krylov subspace method even if A is not positive definite, or even symmetric. We just need to define an inner product and select wk appropriately.
Let's summarize what we learned so far using the A-inner product.
We want to arrive at the solution x by a sequence of best approximations xk. Best approximations satisfy the orthogonality condition:
(xk−x,y)A=0,∀y∈Kk.
By sheer luck (or well-planning), the residual rk is orthogonal to the Krylov subspace Kk in the Euclidean inner product:
(rk,y)E=(x−xk,y)A=0,∀y∈Kk.
In particular, this means the residuals are orthogonal to each other. Hence the name Conjugate Gradient ("orthogonal residuals" didn't sound good).
Given a best approximation xk−1∈Kk−1, we can find the next one xk∈Kk:
Notice how we only need a sparse matrix-vector multiply, a vector-scalar multiply (axpy), and a dot product to solve a matrix equation. This is the power of iterative methods. We can make our code more efficient by reusing variables:
% Initializex = zeros(n, 1), r = b, p = b;r_dot = r' * r;for k = 1 : numIterations % Sparse matrix-vector multiply Ap = A * p; % Find alpha and initialize gamma alpha = r_dot / (p' * Ap); gamma = r_dot; % Find the iterate x = x + alpha * p; % Find the residual r = r - alpha * Ap; r_dot = r' * r; % Find gamma gamma = r_dot / gamma; % Find the search direction p = r + gamma * p;end
5. Example
Let's go back to 2D Poisson's equation on a unit square domain, with homogeneous Dirichlet BC. Our RHS function is given by,
f(x,y)=(a2+b2)π2sin(aπx)sin(bπy),
and we choose a=1, b=9, and N=200 as before.
Stunningly, CG converges in 1 iteration! As far as iterative methods go, you can't do better than that.
The question is how? The RHS function that I chose makes the Poisson's equation an eigenfunction problem. Even at the discrete level, the RHS vector f happens to be an eigenvector of K:
Kf=λf.
As a result, in the first iteration,
α1=fTKffTf=λ1r1=f−α1Kf=f−λ1⋅λf=0.
Talk about luck (or well-planning)!
Notes
A big thanks goes to Dr. Ye, one of my teachers at Kentucky. He instilled my passion for numerical linear algebra and applications thereof. To learn additional iterative methods, I recommend reading his paper linked below.