Iterative Methods: Jacobi and Gauss-Seidel
Learning Objectives
- Write the Jacobi and Gauss-Seidel sweeps entrywise and as splittings , and count the flops of a sweep on a sparse matrix.
- State the spectral radius theorem and the sufficient conditions (strict diagonal dominance, symmetric positive definiteness) with their hypotheses, and show what happens when each one is missing.
- Measure the convergence factor from a run and compare it with .
- Stop on the residual knowing what it does and does not say about the error, and find the point where rounding ends the iteration.
- Decide from measured fill-in, sweep counts, and the number of right-hand sides when iteration beats factorization, and when it does not.
Context & Motivation
lu-decomposition-and-partial-pivoting solves a dense system in about flops. A sparse matrix does not stay sparse under that process: and gain nonzeros where has none, which is called fill-in. For a 3D grid with unknowns, has nonzeros and its sparse factors have .
An iteration avoids that entirely. It touches only the existing nonzeros, it stores a few vectors, and it can stop as soon as the answer is good enough. Jacobi needs nothing beyond the diagonal and the product of with a vector. The price is that it may converge slowly or not at all, depending on the matrix. Jacobi and Gauss-Seidel are the two simplest such methods, and their convergence conditions show the kind of analysis any iteration needs.
Core Theory
Two sweeps
Solve equation for , assuming every . Jacobi uses only old values:
Gauss-Seidel uses each new value as soon as it exists:
Jacobi needs two vectors, ignores the order of the equations, and its updates can run simultaneously. Gauss-Seidel overwrites in place, but its iterates depend on the order, and its updates are sequential unless the sparsity pattern allows groups of independent unknowns. A sweep of either on a matrix with nnz nonzeros costs multiplications, subtractions, and divisions: flops.
The splitting
Write , the diagonal and the strictly lower and upper parts. These and are pieces of , not the factors of an LU decomposition. Then
and each method is . The solution of satisfies , so subtracting gives and . This is fixed-point-iteration in dimensions, with in the role of .
The convergence theorem
The spectral radius is the largest over the eigenvalues of .
Hypotheses. is nonsingular and every . Conclusion. The iteration converges to for every if and only if .
If then . If , a start whose error is an eigenvector for that eigenvalue never converges. A particular lucky start may still work, which is why the theorem says "every". Asymptotically the error shrinks by per sweep, so reducing it by takes about sweeps. That is a long-run rate, not a promise for each sweep.
Sufficient conditions
Computing can be as hard as the original problem, so simpler tests matter.
- Strict diagonal dominance by rows: for all . The row sums of are then below 1, so . Jacobi and Gauss-Seidel both converge for every start, and is automatically nonsingular. The same holds with columns in place of rows.
- Weak dominance plus irreducibility, with strict inequality in at least one row, also suffices. This covers the model problem of Example 2.
- Symmetric positive definite : Gauss-Seidel converges for every start. Jacobi needs more: must be positive definite too.
All of these are sufficient, not necessary, and in general neither method dominates the other (Example 3).
A separate relation is not a test at all: for tridiagonal with nonzero diagonal, . When Jacobi converges, one Gauss-Seidel sweep is then worth two Jacobi sweeps. When it diverges, Gauss-Seidel diverges too, as shows in Example 3.
Stopping, and what the residual means
The computable quantity is the residual , a backward-error measure. The error obeys , which gives
A small residual on an ill-conditioned matrix can therefore hide a large error. See conditioning-and-numerical-stability. Also cap the sweeps, since a diverging run may never trigger the test.
Worked Examples
Example 1: a strictly dominant system
The off-diagonal sum divided by the diagonal is , , by row, so both methods converge and the error falls by at least per sweep in the infinity norm under Jacobi. From , one Jacobi sweep gives . One Gauss-Seidel sweep gives , then , then . A float64 run, with the infinity norm of the error:
textsweep Jacobi Gauss-Seidel 1 6.000e-01 5.833e-01 4 1.389e-02 4.958e-04 8 2.277e-04 9.970e-08 12 2.168e-06 2.498e-12 16 3.527e-08 4.441e-16
The eigenvalues give and . Those eigenvalues are complex, so the sweep-to-sweep ratio oscillates, and the right measurement is a geometric mean: for Jacobi over sweeps 4 to 24 and for Gauss-Seidel over sweeps 2 to 12. Both are below the guaranteed . numpy.linalg.solve returns the same .
Example 2: the model problem and the measured rate
The tridiagonal matrix with on the diagonal and beside it, of size , comes from a 1D diffusion problem. It is only weakly dominant, but it is irreducible, so both methods converge. Its Jacobi matrix has . For that predicts for Jacobi and for Gauss-Seidel. With exact solution , , and :
textsweep Jacobi error ratio Gauss-Seidel error ratio 100 7.939e-01 0.998038 6.556e-01 0.996215 1000 1.435e-01 0.998103 2.153e-02 0.996210 2000 2.149e-02 0.998103 4.832e-04 0.996210 4000 4.823e-04 0.998103 2.433e-07 0.996210 8000 2.428e-07 0.998103 6.398e-14 0.996677
Errors are relative in the 2-norm, and the ratio is . The measured ratios match the prediction to six digits. Gauss-Seidel at sweep 1000 equals Jacobi at sweep 2000, as says. The ratio in the last row is rounding starting to interfere: the Gauss-Seidel error freezes at by sweep 9000, while numpy.linalg.solve gets . Before the freeze the error is the iteration's truncation error. After it, more sweeps do nothing.
The rate is the problem. Reducing the error by takes about 334 Jacobi sweeps at , 28,554 at , and at , growing like . Here a banded LU solves the system in flops, so iterating on this one-dimensional matrix is a poor choice.
The residual test also behaves as stated. At the relative residual first drops below at sweep 6722 for Jacobi and 3362 for Gauss-Seidel, with relative error for both. With the bound allows . It is a bound, not an estimate.
Example 3: what each hypothesis does
Each run takes or , sets , and starts from zero.
| Matrix | Missing | Measured from | ||
|---|---|---|---|---|
| undefined | undefined | division by zero on the first sweep; swapping the rows fixes it | ||
| dominance | both diverge | |||
| nothing | the same equations reordered: both converge | |||
| positive definite | positive definite, yet Jacobi's error is at sweep 40; Gauss-Seidel's is | |||
| dominance | Jacobi is exact after 3 sweeps (); Gauss-Seidel's error is at sweep 12 | |||
| dominance | both converge: dominance is not necessary |
So neither method is always better, and row order can decide everything. The row with also warns about the asymptotic reading in the other direction. Take minus on the superdiagonal, , with all ones and . Jacobi has and is exact at sweep 30, but its error grows to at sweep 29 first. decides the limit, not the path.
Example 4: when iteration beats factorization
Each row below compares scipy.sparse.linalg.splu (one factorization and one solve, relative residual near ) with Jacobi run from zero to a relative residual of . The first three rows use x = default_rng(0).standard_normal(n) and b = A @ x. Sweep counts depend on : on the first matrix, five other right-hand sides gave 18,188 to 28,555 sweeps. The last row solves the PageRank system below and stops at . Times are from one run on one machine and only their rough size matters. The accuracies differ, so this compares six-digit Jacobi with full-accuracy LU.
textproblem n nnz(A) nnz(L+U) splu sweeps Jacobi 2D Poisson, 100x100 10,000 49,600 6.5e+05 0.03 s 14,905 1.1 s 3D Poisson, 40x40x40 64,000 438,400 9.6e+07 31 s 3,282 2.8 s I + 2D Poisson, 500x500 250,000 1.25e+06 2.9e+07 1.5 s 52 0.3 s PageRank, random graph 20,000 219,946 2.7e+08 670 s 18 0.004 s
2D Poisson: factorization wins. The sparse factors are only 13 times denser than , and Jacobi needs tens of thousands of sweeps because as the grid refines. A large sparse matrix alone is not a reason to iterate.
3D Poisson: fill-in decides. The factors are 218 times denser than , and growing fast with the grid, so even a slow iteration wins.
A step of implicit diffusion: does not depend on . Adding the identity to the 2D Poisson matrix makes the diagonal and the off-diagonal row sum at most , so on any grid. Jacobi took 52 sweeps on the , and grids alike, so its total cost is linear in . Started from the previous solution after a small change in the right-hand side, it took 23 on the largest grid. This is the good case for iteration: a dominant diagonal and a good initial guess.
PageRank is Jacobi. The update of pagerank-via-power-iteration is exactly the Jacobi sweep for when no page links to itself, because then . With a column-stochastic , , which guarantees a factor of per sweep whatever is. On this graph (10 random links per page) the measured factor was , and it took 18 sweeps at , and , each at flops. A dense LU at would be flops, and the sparse one above had 1,200 times the nonzeros of .
Many right-hand sides favor factoring. After splu on the grid, each new solve took s, while 23 sweeps at the rate above take about s, so factorization pays off once enough right-hand sides arrive.
Book vs today
Plain Jacobi and Gauss-Seidel are rarely the final solver now. On the 2D problem above, scipy.sparse.linalg.cg (for symmetric positive definite ) needed 187 iterations against Jacobi's 14,905 with the same , and 91 against 3,282 on the 3D one. Its count varies with too: 1 to 256 on those five other right-hand sides. Krylov methods and multigrid took over, with these sweeps surviving inside them as preconditioners and smoothers. On the model problem with , five Gauss-Seidel sweeps leave of the highest-frequency error mode and of the smoothest, the behavior a multigrid cycle exploits. Five plain Jacobi sweeps leave of both, because its eigenvalues at the two ends have the same size, so a multigrid code built on Jacobi uses a damped variant.
Common Misconceptions & Pitfalls
- "Diagonal dominance is required." It is sufficient. The last row of Example 3 converges without it, and Example 2 has only weak dominance.
- "Gauss-Seidel is always faster than Jacobi." Example 3 has a matrix where Jacobi finishes in 3 sweeps and Gauss-Seidel diverges.
- "Positive definite means Jacobi works." That guarantee belongs to Gauss-Seidel. Example 3 has on a positive definite matrix.
- " means the error falls every sweep." Example 3's closing case grows by a factor of first.
- "Iteration wins on any large sparse matrix." 2D Poisson with direct
spluwas about 40 times faster than Jacobi, with far more accuracy. - "More sweeps, more digits." Example 2 freezes at .
- "A small residual means a small error." Only up to a factor of .
- "These and are the LU factors." They are the strict triangles of itself.
Summary
Jacobi solves each equation with old values, and Gauss-Seidel uses new ones at once. Both are with error , so with nonsingular and nonzero diagonal they converge from every start exactly when , at about per sweep in the long run. Strict diagonal dominance guarantees both, positive definiteness guarantees Gauss-Seidel, and tridiagonal structure gives , measured here to six digits. None of these is necessary, and neither method always wins. A sweep costs flops, so iteration wins when fill-in is severe or when stays away from 1 as grows, as in implicit steps and PageRank, and it loses on the 2D Poisson matrix and with many right-hand sides. Rounding sets a floor near in Example 2, and the residual bounds the error only through .
Documentation Links
- Barrett et al. (1994): Templates for the Solution of Linear Systems, Stationary Iterative Methods
- Barrett et al. (1994): Templates for the Solution of Linear Systems, Convergence of the Jacobi method
- Barrett et al. (1994): Templates for the Solution of Linear Systems, The Gauss-Seidel Method
- Saad (2003): Iterative Methods for Sparse Linear Systems, 2nd ed.
- Driscoll and Braun: Fundamentals of Numerical Computation, 8.1 Sparsity and structure
- Driscoll and Braun: Fundamentals of Numerical Computation, 8.6 MINRES and conjugate gradients
- SciPy documentation: scipy.sparse.linalg.splu
- SciPy documentation: scipy.sparse.linalg.cg