Elimination solves in one pass. A different family of solvers never eliminates anything: it starts from a guess and improves it, again and again, until the guess stops changing. Each improvement is cheap, which is why large solvers often work this way.
The idea is to rearrange each equation so that one unknown stands alone. Equation ,
becomes
If you knew every other unknown, this would hand you exactly. You don't, so you plug in your current guess for them and get a better value for . Doing that once for every unknown, in the order , is one sweep.
There are two classic ways to run a sweep. They differ in just one thing: where the values of the other unknowns come from.
| method | when computing , the other unknowns are taken from… |
|---|---|
| Jacobi | the guess as it stood at the start of this sweep. New values only take over once the sweep is finished. |
| Gauss–Seidel | the latest values. Any already updated earlier in this sweep is used straight away. |
For example, take and with the starting guess . Both methods begin with . Jacobi then computes from the old , while Gauss–Seidel computes from the new one.
Task: write sweep_solve(A, b, x0, sweeps, method).
A is an list of lists, and b and x0 are lists of length . x0 is the starting guess.sweeps sweeps (possibly 0) with method, which is either "jacobi" or "gauss-seidel".sweeps = 0 that is just x0, as floats.Every diagonal entry in the tests is non-zero, and every test system is one where both methods close in on the true solution.