In mathematics, the conjugate gradient method is an algorithm for the numerical solution of particular systems of linear equations, namely those whose matrix is positive-semidefinite. The conjugate gradient method is often implemented as an iterative algorithm, applicable to sparse systems that are too large to be handled by a direct implementation or other direct methods such as the Cholesky decomposition. Large sparse systems often arise when numerically solving partial differential equations or optimization problems.
The conjugate gradient method can also be used to solve unconstrained optimization problems such as energy minimization. It is commonly attributed to Magnus Hestenes and Eduard Stiefel,[1] [2] who programmed it on the Z4,[3] and extensively researched it.[4] [5]
The biconjugate gradient method provides a generalization to non-symmetric matrices. Various nonlinear conjugate gradient methods seek minima of nonlinear optimization problems.
Suppose we want to solve the system of linear equations
Ax=b
for the vector
x
n x n
A
x
b
x*
See main article: Derivation of the conjugate gradient method.
The conjugate gradient method can be derived from several different perspectives, including specialization of the conjugate direction method for optimization, and variation of the Arnoldi/Lanczos iteration for eigenvalue problems. Despite differences in their approaches, these derivations share a common topic—proving the orthogonality of the residuals and conjugacy of the search directions. These two properties are crucial to developing the well-known succinct formulation of the method.
We say that two non-zero vectors u and v are conjugate (with respect to
A
uTAv=0.
Since
A
uTAv= \langleu,v\rangleA:= \langleAu,v\rangle= \langleu,ATv\rangle= \langleu,Av\rangle.
Two vectors are conjugate if and only if they are orthogonal with respect to this inner product. Being conjugate is a symmetric relation: if
u
v
v
u
P=\{p1,...,pn\}
is a set of
n
A
T | |
p | |
i |
Apj=0
i ≠ j
P
Rn
x*
Ax=b
x*=
n | |
\sum | |
i=1 |
\alphaipi ⇒ Ax*=
n | |
\sum | |
i=1 |
\alphaiApi.
Left-multiplying the problem
Ax=b
T | |
p | |
k |
T | |
p | |
k |
b=
T | |
p | |
k |
Ax*=
n | |
\sum | |
i=1 |
\alphai
T | |
p | |
k |
Api=
n | |
\sum | |
i=1 |
\alphai\left\langlepk,pi\right\rangleA=\alphak\left\langlepk,pk\right\rangleA
\alphak=
\langlepk,b\rangle | |
\langlepk,pk\rangleA |
.
This gives the following method for solving the equation : find a sequence of
n
\alphak
If we choose the conjugate vectors
pk
x*
f(x)=\tfrac12xTAx-xTb, x\inRn.
H(f(x))=A,
\nablaf(x)=Ax-b.
This suggests taking the first basis vector p0 to be the negative of the gradient of f at x = x0. The gradient of f equals . Starting with an initial guess x0, this means we take p0 = b − Ax0. The other vectors in the basis will be conjugate to the gradient, hence the name conjugate gradient method. Note that p0 is also the residual provided by this initial step of the algorithm.
Let rk be the residual at the kth step:
rk=b-Axk.
rk
f
xk
pk
pk=rk-\sumi
| ||||||||||
|
pi
xk+1=xk+\alphakpk
\alphak=
| ||||||||||
|
=
| ||||||||||
|
,
where the last equality follows from the definition of
rk
\alphak
\alphak
\begin{align} f(xk+1)&=f(xk+\alphakpk)=:g(\alphak) \\ g'(\alphak)&\overset{!}{=}0 ⇒ \alphak=
| ||||||||||
|
. \end{align}
The above algorithm gives the most straightforward explanation of the conjugate gradient method. Seemingly, the algorithm as stated requires storage of all previous searching directions and residue vectors, as well as many matrix–vector multiplications, and thus can be computationally expensive. However, a closer analysis of the algorithm shows that
ri
rj
T | |
r | |
i |
rj=0
pi
A
pj
T | |
p | |
i |
Apj=0
i ≠ j
pi
ri
ri
pi
A
xk
x
That is, if the CG method starts with
x0=0
Ax=b
A
x0
\begin{align} &r0:=b-Ax0\\ &\hbox{if}r0issufficientlysmall,thenreturnx0astheresult\\ &p0:=r0\\ &k:=0\\ &repeat\\ & \alphak:=
| ||||||||||
|
\\ & xk+1:=xk+\alphakpk\\ & rk+1:=rk-\alphakApk\\ & \hbox{if}rk+1issufficientlysmall,thenexitloop\\ & \betak:=
| ||||||||||
|
\\ & pk+1:=rk+1+\betakpk\\ & k:=k+1\\ &endrepeat\\ &returnxk+1astheresult \end{align}
This is the most commonly used algorithm. The same formula for is also used in the Fletcher–Reeves nonlinear conjugate gradient method.
We note that
x1
x0
\betak=0
xk+1
xk
The formulas
xk+1:=xk+\alphakpk
rk:=b-Axk
rk+1:=rk-\alphakApk
rk+1:=b-Axk+1
A
Apk
\alphak
rk+1:=b-Axk+1
A norm of the residual is typically used for stopping criteria. The norm of the explicit residual
rk+1:=b-Axk+1
rk+1:=rk-\alphakApk
In the algorithm, is chosen such that
rk+1
rk
\alphak=
| ||||||||||
|
=
| ||||||||||
|
since
rk+1=pk+1-\betakpk
pk+1
pk
\betak=-
| ||||||||||
|
using
rk+1=rk-\alphakApk
and equivalently
Apk=
1 | |
\alphak |
(rk-rk+1),
the numerator of is rewritten as
T | |
r | |
k+1 |
Apk=
1 | |
\alphak |
T | |
r | |
k+1 |
(rk-rk+1)=-
1 | |
\alphak |
T | |
r | |
k+1 |
rk+1
because
rk+1
rk
T | |
p | |
k |
Apk=(rk+\betak-1pk-1)TApk=
1 | |
\alphak |
T | |
r | |
k |
(rk-rk+1)=
1 | |
\alphak |
T | |
r | |
k |
rk
using that the search directions pk are conjugated and again that the residuals are orthogonal. This gives the in the algorithm after cancelling .
Return the solution to `A * x = b` using the conjugate gradient method."""function conjugate_gradient!(A::AbstractMatrix, b::AbstractVector, x::AbstractVector; tol=eps(eltype(b))) # Initialize residual vector residual = b - A * x # Initialize search direction vector search_direction = residual # Compute initial squared residual norm norm(x) = sqrt(sum(x.^2)) old_resid_norm = norm(residual)
# Iterate until convergence while old_resid_norm > tol A_search_direction = A * search_direction step_size = old_resid_norm^2 / (search_direction' * A_search_direction) # Update solution @. x = x + step_size * search_direction # Update residual @. residual = residual - step_size * A_search_direction new_resid_norm = norm(residual) # Update search direction vector @. search_direction = residual + (new_resid_norm / old_resid_norm)^2 * search_direction # Update squared residual norm for next iteration old_resid_norm = new_resid_norm end return xend
Consider the linear system Ax = b given by
Ax=\begin{bmatrix}4&1\ 1&3\end{bmatrix}\begin{bmatrix}x1\ x2\end{bmatrix}=\begin{bmatrix}1\ 2\end{bmatrix},
we will perform two steps of the conjugate gradient method beginning with the initial guess
x0=\begin{bmatrix}2\ 1\end{bmatrix}
in order to find an approximate solution to the system.
For reference, the exact solution is
x=\begin{bmatrix}
1 | \\\ | |
11 |
7 | |
11 |
\end{bmatrix} ≈ \begin{bmatrix}0.0909\\\ 0.6364\end{bmatrix}
r0=\begin{bmatrix}1\ 2\end{bmatrix}-\begin{bmatrix}4&1\ 1&3\end{bmatrix} \begin{bmatrix}2\ 1\end{bmatrix}=\begin{bmatrix}-8\ -3\end{bmatrix}=p0.
Since this is the first iteration, we will use the residual vector r0 as our initial search direction p0; the method of selecting pk will change in further iterations.
We now compute the scalar using the relationship
\alpha0=
| ||||||||||
|
=
\begin{bmatrix | |
-8 |
&-3\end{bmatrix}\begin{bmatrix}-8\ -3\end{bmatrix}}{\begin{bmatrix}-8&-3\end{bmatrix}\begin{bmatrix}4&1\ 1&3\end{bmatrix}\begin{bmatrix}-8\ -3\end{bmatrix}}=
73 | |
331 |
≈ 0.2205
We can now compute x1 using the formula
x1=x0+\alpha0p0=\begin{bmatrix}2\ 1\end{bmatrix}+
73 | |
331 |
\begin{bmatrix}-8\ -3\end{bmatrix} ≈ \begin{bmatrix}0.2356\ 0.3384\end{bmatrix}.
This result completes the first iteration, the result being an "improved" approximate solution to the system, x1. We may now move on and compute the next residual vector r1 using the formula
r1=r0-\alpha0Ap0=\begin{bmatrix}-8\ -3\end{bmatrix}-
73 | |
331 |
\begin{bmatrix}4&1\ 1&3\end{bmatrix}\begin{bmatrix}-8\ -3\end{bmatrix} ≈ \begin{bmatrix}-0.2810\ 0.7492\end{bmatrix}.
Our next step in the process is to compute the scalar that will eventually be used to determine the next search direction p1.
\beta0=
| ||||||||||
|
≈
\begin{bmatrix | |
-0.2810 |
&0.7492\end{bmatrix}\begin{bmatrix}-0.2810\ 0.7492\end{bmatrix}}{\begin{bmatrix}-8&-3\end{bmatrix}\begin{bmatrix}-8\ -3\end{bmatrix}}=0.0088.
Now, using this scalar, we can compute the next search direction p1 using the relationship
p1=r1+\beta0p0 ≈ \begin{bmatrix}-0.2810\ 0.7492\end{bmatrix}+0.0088\begin{bmatrix}-8\ -3\end{bmatrix}=\begin{bmatrix}-0.3511\ 0.7229\end{bmatrix}.
We now compute the scalar using our newly acquired p1 using the same method as that used for .
\alpha1=
| ||||||||||
|
≈
\begin{bmatrix | |
-0.2810 |
&0.7492\end{bmatrix}\begin{bmatrix}-0.2810\ 0.7492\end{bmatrix}}{\begin{bmatrix}-0.3511&0.7229\end{bmatrix}\begin{bmatrix}4&1\ 1&3\end{bmatrix}\begin{bmatrix}-0.3511\ 0.7229\end{bmatrix}}=0.4122.
Finally, we find x2 using the same method as that used to find x1.
x2=x1+\alpha1p1 ≈ \begin{bmatrix}0.2356\ 0.3384\end{bmatrix}+0.4122\begin{bmatrix}-0.3511\ 0.7229\end{bmatrix}=\begin{bmatrix}0.0909\ 0.6364\end{bmatrix}.
The result, x2, is a "better" approximation to the system's solution than x1 and x0. If exact arithmetic were to be used in this example instead of limited-precision, then the exact solution would theoretically have been reached after n = 2 iterations (n being the order of the system).
The conjugate gradient method can theoretically be viewed as a direct method, as in the absence of round-off error it produces the exact solution after a finite number of iterations, which is not larger than the size of the matrix. In practice, the exact solution is never obtained since the conjugate gradient method is unstable with respect to even small perturbations, e.g., most directions are not in practice conjugate, due to a degenerative nature of generating the Krylov subspaces.
As an iterative method, the conjugate gradient method monotonically (in the energy norm) improves approximations
xk
\kappa(A)
A
\kappa(A)
If
\kappa(A)
Ax-b=0
M-1(Ax-b)=0
\kappa(M-1A)
\kappa(A)
Define a subset of polynomials as
* | |
\Pi | |
k |
:=\left\lbrace p\in\Pik : p(0)=1 \right\rbrace,
\Pik
k
Let
\left(xk\right)k
x*
ek:=xk-x*
\begin{align} \left\|ek\right\|A &=
min | |||||||||
|
\left\|p(A)e0\right\|A \\ &\leq
min | |||||||||
|
max|p(λ)| \left\|e0\right\|A \\ &\leq2\left(
\sqrt{\kappa(A) | |
-1 |
}{\sqrt{\kappa(A)}+1}\right)k \left\|e0\right\|A \\ &\leq2\exp\left(
-2k | |
\sqrt{\kappa(A) |
\sigma(A)
\kappa(A)
This shows
k=\tfrac{1}{2}\sqrt{\kappa(A)}log\left(\left\|e0\right\|A\varepsilon-1\right)
2\varepsilon
\varepsilon>0
Note, the important limit when
\kappa(A)
infty
\sqrt{\kappa(A) | |
-1 |
}{\sqrt{\kappa(A)}+1} ≈ 1-
2 | |
\sqrt{\kappa(A) |
≈ 1-
2 | |
\kappa(A) |
No round-off error is assumed in the convergence theorem, but the convergence bound is commonly valid in practice as theoretically explained by Anne Greenbaum.
If initialized randomly, the first stage of iterations is often the fastest, as the error is eliminated within the Krylov subspace that initially reflects a smaller effective condition number. The second stage of convergence is typically well defined by the theoretical convergence bound with , but may be super-linear, depending on a distribution of the spectrum of the matrix
A
See also: Preconditioner. In most cases, preconditioning is necessary to ensure fast convergence of the conjugate gradient method. If
M-1
M-1A
A
r0:=b-Ax0
rm{Solve:}Mz0:=r0
p0:=z0
k:=0
repeat
\alphak:=
| ||||||||||
|
xk+1:=xk+\alphakpk
rk+1:=rk-\alphakApk
if rk+1 is sufficiently small then exit loop end if
Solve Mzk+1:=rk+1
\betak:=
| ||||||||||
|
pk+1:=zk+1+\betakpk
k:=k+1
end repeat
The result is xk+1
The above formulation is equivalent to applying the regular conjugate gradient method to the preconditioned system[11]
E-1A(E-1)T\hat{x
EET=M, \hat{x
The Cholesky decomposition of the preconditioner must be used to keep the symmetry (and positive definiteness) of the system. However, this decomposition does not need to be computed, and it is sufficient to know
M-1
E-1A(E-1)T
M-1A
The preconditioner matrix M has to be symmetric positive-definite and fixed, i.e., cannot change from iteration to iteration. If any of these assumptions on the preconditioner is violated, the behavior of the preconditioned conjugate gradient method may become unpredictable.
An example of a commonly used preconditioner is the incomplete Cholesky factorization.[12]
It is importart to keep in mind that we don't want to invert the matrix
M
M-1
M
L
M=LLT
Then we have to solve:
Mz=r
z=M-1r
But:
M-1=(L-1)TL-1
Then:
z=(L-1)TL-1r
Let's take an intermediary vector
a
a=L-1r
r=La
Since
r
L
L
a
a
z=(L-1)Ta
a=LTz
Since
a
LT
LT
z
Using this method, there is no need to invert
M
L
z
In numerically challenging applications, sophisticated preconditioners are used, which may lead to variable preconditioning, changing between iterations. Even if the preconditioner is symmetric positive-definite on every iteration, the fact that it may change makes the arguments above invalid, and in practical tests leads to a significant slow down of the convergence of the algorithm presented above. Using the Polak–Ribière formula
\betak:=
| ||||||||||
|
instead of the Fletcher–Reeves formula
\betak:=
| ||||||||||
|
may dramatically improve the convergence in this case.[13] This version of the preconditioned conjugate gradient method can be called[14] flexible, as it allows for variable preconditioning. The flexible version is also shown[15] to be robust even if the preconditioner is not symmetric positive definite (SPD).
The implementation of the flexible version requires storing an extra vector. For a fixed SPD preconditioner,
T | |
r | |
k+1 |
zk=0,
The mathematical explanation of the better convergence behavior of the method with the Polak–Ribière formula is that the method is locally optimal in this case, in particular, it does not converge slower than the locally optimal steepest descent method.[16]
In both the original and the preconditioned conjugate gradient methods one only needs to set
\betak:=0
The conjugate gradient method can also be derived using optimal control theory.[17] In this approach, the conjugate gradient method falls out as an optimal feedback controller, for the double integrator system, The quantities
\gammaa
\gammab
The conjugate gradient method can be applied to an arbitrary n-by-m matrix by applying it to normal equations ATA and right-hand side vector ATb, since ATA is a symmetric positive-semidefinite matrix for any A. The result is conjugate gradient on the normal equations (CGN or CGNR).
ATAx = ATb
As an iterative method, it is not necessary to form ATA explicitly in memory but only to perform the matrix–vector and transpose matrix–vector multiplications. Therefore, CGNR is particularly useful when A is a sparse matrix since these operations are usually extremely efficient. However the downside of forming the normal equations is that the condition number κ(ATA) is equal to κ2(A) and so the rate of convergence of CGNR may be slow and the quality of the approximate solution may be sensitive to roundoff errors. Finding a good preconditioner is often an important part of using the CGNR method.
Several algorithms have been proposed (e.g., CGLS, LSQR). The LSQR algorithm purportedly has the best numerical stability when A is ill-conditioned, i.e., A has a large condition number.
The conjugate gradient method with a trivial modification is extendable to solving, given complex-valued matrix A and vector b, the system of linear equations
Ax=b
The advantages and disadvantages of the conjugate gradient methods are summarized in the lecture notes by Nemirovsky and BenTal.[18]
This example is from [19] Let , and defineSince
W