Krylov subspaces and GMRES
Extend Krylov-subspace solving to nonsymmetric systems with GMRES.
Conjugate gradient relies on being symmetric positive definite. GMRES (Generalized Minimal Residual) drops both assumptions: it works for any invertible . The price is that GMRES has to store the full basis of the Krylov subspace, giving a memory cost at iteration for an -dimensional problem, which is why it is restarted in practice[1].
Krylov subspaces
The Krylov subspace of order for and starting vector is
Both CG and GMRES find their iterates in . The subspace is generated by repeated multiplication by alone, which is why these methods need nothing but the ability to compute for any vector .
The Arnoldi process
For nonsymmetric , building an orthonormal basis of the Krylov subspace uses the Arnoldi iteration, a Gram-Schmidt orthogonalization of against all previous basis vectors. After steps it produces:
- An orthonormal matrix whose columns span .
- An upper Hessenberg matrix satisfying
For SPD , the Hessenberg matrix degenerates to tridiagonal and Arnoldi reduces to the Lanczos process, which CG uses implicitly through its short three-term recurrence (giving CG its constant memory cost).
The GMRES idea
GMRES picks to minimize the residual norm:
Substituting and using :
where and the last step uses that has orthonormal columns. The big residual minimization becomes a small least-squares problem in , solved by Givens rotations applied to [2].
Memory and restarting
Storing costs memory and orthogonalization at step costs work. For large this is prohibitive, so practical implementations use GMRES(): restart every iterations using the current as the new . Restarting trades a bounded memory cost for the loss of strict residual monotonicity across restart boundaries, and sometimes for slower convergence. Preconditioning is even more important for GMRES than for CG, because reducing reduces both work and memory[3].
Check it on arrays
import numpy as np
rng = np.random.default_rng(0)
# Nonsymmetric system: A has no real spectral structure.
n = 80
A = rng.normal(size=(n, n)) + 2 * np.eye(n)
b = rng.normal(size=n)
def gmres(A, b, max_iter=None, tol=1e-10):
m = len(b)
max_iter = max_iter or m
Q = np.zeros((m, max_iter + 1))
H = np.zeros((max_iter + 1, max_iter))
r0 = b - A @ np.zeros(m)
beta = np.linalg.norm(r0)
Q[:, 0] = r0 / beta
residuals = [beta]
for k in range(max_iter):
v = A @ Q[:, k]
for j in range(k + 1):
H[j, k] = Q[:, j] @ v
v = v - H[j, k] * Q[:, j]
H[k + 1, k] = np.linalg.norm(v)
if H[k + 1, k] < 1e-14:
break
Q[:, k + 1] = v / H[k + 1, k]
# Least-squares: min ||beta e_1 - H_{k+1, k} y||
e1 = np.zeros(k + 2); e1[0] = beta
y, *_ = np.linalg.lstsq(H[:k + 2, :k + 1], e1, rcond=None)
x = Q[:, :k + 1] @ y
res = np.linalg.norm(b - A @ x)
residuals.append(res)
if res < tol:
return x, residuals
return x, residuals
x_gmres, residuals = gmres(A, b, max_iter=60)
x_true = np.linalg.solve(A, b)
print(f"GMRES iterations: {len(residuals) - 1}")
print(f"final residual: {residuals[-1]:.2e}")
print(f"error vs solve: {np.linalg.norm(x_gmres - x_true):.2e}")
print(f"residual monotone decrease: {all(residuals[i] >= residuals[i+1] for i in range(len(residuals)-1))}")GMRES drives the residual down monotonically and converges in well under the ambient dimension.
Exercises
Exercise 1. Explain why the GMRES residual norm is monotonically nonincreasing across iterations (within a single run, no restart), using the nested structure of the Krylov subspaces.
Solution
Step 1. GMRES picks to minimize over the affine space , where .
Step 2. The Krylov subspaces are nested: , since every generator of is also a generator of . So .
Step 3. Minimizing the same residual norm over a larger set cannot increase the minimum: the previous minimizer is still a candidate at step . Hence
The residual norm never rises. Restarting breaks this, because it discards the accumulated subspace and starts a fresh minimization from .
Exercise 2. The Arnoldi relation is with having orthonormal columns. Show that minimizing over reduces to the small least-squares problem with .
Solution
Step 1. Write the residual at : , since .
Step 2. Substitute the Arnoldi relation . Also write , because is the first column of and is the first standard basis vector. Then
Step 3. Orthonormal columns preserve the 2-norm: for any . Therefore
The -dimensional residual minimization collapses to a least-squares problem in the Hessenberg matrix , which is cheap to solve by Givens rotations.
Exercise 3 (code). Run GMRES on a nonsymmetric system and confirm both that the residual norms are monotonically nonincreasing and that the solution matches a direct solve.
Solution
import numpy as np
rng = np.random.default_rng(0)
n = 50
A = rng.normal(size=(n, n)) + 2 * np.eye(n) # nonsymmetric, well-separated spectrum
b = rng.normal(size=n)
m = len(b)
Q = np.zeros((m, m + 1))
H = np.zeros((m + 1, m))
r0 = b.copy() # x0 = 0
beta = np.linalg.norm(r0)
Q[:, 0] = r0 / beta
residuals = [beta]
x = np.zeros(m)
for k in range(m):
v = A @ Q[:, k]
for j in range(k + 1):
H[j, k] = Q[:, j] @ v
v = v - H[j, k] * Q[:, j]
H[k + 1, k] = np.linalg.norm(v)
if H[k + 1, k] < 1e-14:
break
Q[:, k + 1] = v / H[k + 1, k]
e1 = np.zeros(k + 2); e1[0] = beta
y, *_ = np.linalg.lstsq(H[:k + 2, :k + 1], e1, rcond=None)
x = Q[:, :k + 1] @ y
residuals.append(np.linalg.norm(b - A @ x))
residuals = np.array(residuals)
assert np.all(np.diff(residuals) <= 1e-10) # monotone nonincreasing
assert np.linalg.norm(x - np.linalg.solve(A, b)) < 1e-6
print("residual decreased monotonically over", len(residuals) - 1, "iterations")Each iteration minimizes over a strictly larger Krylov subspace, so the residual norms form a nonincreasing sequence, and the final solution agrees with the direct solve to full working precision.
The final A4 lesson turns from algorithms to the precision they run in: mixed-precision arithmetic.
References
- [1]Saad, Y., & Schultz, M. H. (1986). "GMRES: A generalized minimal residual algorithm for solving nonsymmetric linear systems". SIAM Journal on Scientific and Statistical Computing, 7(3), 856-869. The original GMRES and restarting (doi:10.1137/0907058).↩
- [2]Trefethen, L. N., & Bau, D. (1997). Numerical Linear Algebra. SIAM. Arnoldi, GMRES, and the least-squares formulation (Lectures 33-35).↩
- [3]Saad, Y. (2003). Iterative Methods for Sparse Linear Systems (2nd ed.). SIAM. Krylov methods for nonsymmetric systems and preconditioning (doi:10.1137/1.9780898718003).↩