Jacobi and Gauss-Seidel Iterations
For very large sparse systems, direct factorization becomes too expensive in memory and time. Iterative methods start from a guess and improve it using only cheap operations with . The oldest are the Jacobi and Gauss-Seidel methods: solve each equation for its own unknown, using the current values of the others. They are slow on their own, but they are the smoothers inside multigrid and a good introduction to convergence of iterations.
The methods
Split into diagonal, strictly lower and strictly upper parts.
- Jacobi: , i.e. . All updates use old values, so they can be done in parallel.
- Gauss-Seidel: use each new value as soon as it is computed: .
- SOR (successive over-relaxation): move further in the Gauss-Seidel direction by a factor .
Convergence
Both are splitting methods with . The error obeys , so the method converges for every start exactly when the spectral radius , and the error shrinks by about per iteration (Fixed-Point Iteration). Sufficient conditions: strictly diagonally dominant (both methods), or symmetric positive definite (Gauss-Seidel and SOR for ).
import numpy as np
def jacobi(A, b, x, iters):
D = np.diag(A); R = A - np.diag(D)
for _ in range(iters):
x = (b - R @ x) / D
return x
def gauss_seidel(A, b, x, iters, omega=1.0):
x = x.copy(); n = len(b)
for _ in range(iters):
for i in range(n):
gs = (b[i] - A[i, :i] @ x[:i] - A[i, i + 1:] @ x[i + 1:]) / A[i, i]
x[i] = (1 - omega) * x[i] + omega * gs
return x
n = 30
A = 2 * np.eye(n) - np.eye(n, k=1) - np.eye(n, k=-1) # 1D Laplacian
x_true = np.sin(np.linspace(0, 3, n))
b = A @ x_true
x0 = np.zeros(n)
for iters in (100, 400, 1600):
ej = np.linalg.norm(jacobi(A, b, x0, iters) - x_true)
eg = np.linalg.norm(gauss_seidel(A, b, x0, iters) - x_true)
es = np.linalg.norm(gauss_seidel(A, b, x0, iters, omega=1.81) - x_true)
print(f"{iters:4d} iterations: Jacobi {ej:.2e}, Gauss-Seidel {eg:.2e}, SOR(1.81) {es:.2e}")
D = np.diag(np.diag(A)); L = np.tril(A, -1)
rho_j = max(abs(np.linalg.eigvals(np.linalg.solve(D, D - A))))
rho_gs = max(abs(np.linalg.eigvals(np.linalg.solve(D + L, -(A - D - L)))))
print(f"spectral radii: Jacobi {rho_j:.5f}, Gauss-Seidel {rho_gs:.5f} (= Jacobi squared {rho_j**2:.5f})") 100 iterations: Jacobi 2.32e+00, Gauss-Seidel 1.41e+00, SOR(1.81) 2.66e-06
400 iterations: Jacobi 4.97e-01, Gauss-Seidel 6.42e-02, SOR(1.81) 5.46e-15
1600 iterations: Jacobi 1.04e-03, Gauss-Seidel 2.79e-07, SOR(1.81) 3.24e-15
spectral radii: Jacobi 0.99487, Gauss-Seidel 0.98976 (= Jacobi squared 0.98976)
Why they are slow for PDEs
For the Laplacian on a grid with spacing , . Reducing the error by a fixed factor takes iterations, which grows rapidly as the grid is refined. Gauss-Seidel is twice as fast; optimal SOR needs only iterations.
Yet these methods remove high-frequency (oscillatory) error components very quickly; only the smooth components are slow. Multigrid exploits this: a few Gauss-Seidel sweeps smooth the error, which is then corrected on a coarser grid where it is no longer smooth. The result is an solver.
import numpy as np
n = 63
A = 2 * np.eye(n) - np.eye(n, k=1) - np.eye(n, k=-1)
x = np.arange(1, n + 1) / (n + 1)
for k in (1, 31): # smooth mode and oscillatory mode
e = np.sin(k * np.pi * x) # error vector (solve A e = 0 from this start)
D = np.diag(A); R = A - np.diag(D)
for _ in range(10):
e = (0 - R @ e) / D * 2 / 3 + e / 3 # weighted Jacobi (omega = 2/3), a classic smoother
print(f"mode k = {k:2d}: error reduced to {np.linalg.norm(e) / np.linalg.norm(np.sin(k * np.pi * x)):.3f} of its size after 10 sweeps")mode k = 1: error reduced to 0.992 of its size after 10 sweeps
mode k = 31: error reduced to 0.000 of its size after 10 sweeps
In software
- Plain Jacobi and Gauss-Seidel are rarely used as solvers; they appear as smoothers in multigrid (PyAMG, hypre) and as simple preconditioners (Preconditioning and GMRES).
- Jacobi iterations parallelize perfectly and are used on GPUs and in distributed settings; asynchronous variants tolerate stale data.
Exercises
Exercise 1: One Jacobi step by hand
For from , take one Jacobi step and one Gauss-Seidel step.
Answer
Jacobi ; Gauss-Seidel . (Exact .)
Solution
Jacobi: , . Gauss-Seidel uses the new : .
Exercise 2: Iterations needed
If , how many iterations reduce the error by ?
Answer
About 13,800.
Solution
.
Exercise 3: Diagonal dominance (code)
For a random matrix with diagonal and off-diagonal entries uniform in (seed 102), compute the Jacobi spectral radius and run 30 Jacobi iterations.
Answer
is well below 1 (strict diagonal dominance), and the residual falls to about rounding level.
Solution
import numpy as np
rng = np.random.default_rng(102)
A = rng.uniform(-1, 1, (50, 50)); np.fill_diagonal(A, 60.0)
b = rng.normal(size=50)
D = np.diag(A); R = A - np.diag(D)
print(f"rho = {max(abs(np.linalg.eigvals(R / D[:, None]))):.4f}")
x = np.zeros(50)
for _ in range(30):
x = (b - R @ x) / D
print(f"residual {np.linalg.norm(b - A @ x):.1e}")rho = 0.0732
residual 1.5e-15
Test yourself
A stationary iteration converges for every starting vector exactly when…
- is symmetric
- the spectral radius of the iteration matrix is below 1
- the diagonal is positive
Gauss-Seidel differs from Jacobi by…
- using new values as soon as they are computed
- using the transpose
- being a direct method
- needing no diagonal
Jacobi and Gauss-Seidel are effective multigrid smoothers because they…
- solve exactly
- quickly damp oscillatory error components
- remove smooth errors fast
- need no matrix