Topic

Iterative and eigenvalue methods

The rest of this library returns exact answers and avoids the classical numerical methods on purpose. That makes it a good place to study them. The functions under methods run each method as a textbook does, in doubles. Beside every step they put what exact arithmetic says, so the error of the method and the error added by rounding can be told apart.

Three errors at every step

Each row of a run has the double iterate xk and three distances, all measured in the largest-entry norm:

  • error: from xk to the exact solution x*.
  • methodError: from the same iteration carried out in exact rationals to x*. This is the error of the method alone.
  • rounding: from xk to that exact iterate. This is the part that floating point added.

Every double is a rational number, so these distances are computed exactly and only then printed. Exact iterates grow long, so they are kept for exactIterations steps (20 by default). After that, the rows show the error only.

import { methods } from "@ratmath/linalg";

const run = methods.jacobi([[3, 1], [1, 3]], [1, 2], { iterations: 12 });
run.solution;         // ["1/8", "5/8"]: exact
run.rows[12];         // { n: 12, x: [0.12499976…, 0.62499882…], error: 1.18e-6, methodError: 1.18e-6, rounding: 3.19e-17, residual }
run.spectralRadius;   // 0.3333…: the error shrinks by this factor per step
run.ratio;            // 0.3333…: the factor actually observed

Jacobi, Gauss–Seidel and over-relaxation

Split A = D + L + U into its diagonal and its strictly lower and upper parts. Jacobi solves the i-th equation for xi using the previous iterate: xk+1 = D⁻¹(b − (L + U)xk). Gauss–Seidel uses each new component as soon as it is computed: xk+1 = (D + L)⁻¹(b − Uxk). Successive over-relaxation moves a factor ω of the way from the old value to the Gauss–Seidel value; ω = 1 is Gauss–Seidel.

Each is xk+1 = T xk + c for an iteration matrix T. The error ek = xk − x* satisfies ek+1 = T ek, so the iteration converges from every start exactly when the spectral radius ρ(T), the largest |eigenvalue|, is below 1. Then the error shrinks by about ρ(T) per step. A strictly diagonally dominant A is enough to guarantee convergence for Jacobi and Gauss–Seidel.

methods.gaussSeidel([[3, 1], [1, 3]], [1, 2]).iterationMatrix;   // [["0", "-1/3"], ["0", "1/9"]]: ρ = 1/9, the square of Jacobi's 1/3
methods.jacobi([[1, 2], [2, 1]], [3, 3]).rows.map((r) => r.error); // 1, 2, 4, 8, …: ρ = 2, so it diverges
// over-relaxation on [[4, −1, 0], [−1, 4, −1], [0, −1, 4]]: ρ(T) for ω = 1, 11/10, 6/5, 3/2, 19/10
// 0.125, 0.1, 0.2, 0.5, 0.9

For this tridiagonal matrix the best ω is 2/(1 + √(1 − ρJ²)) ≈ 1.03, where ρJ is Jacobi's spectral radius. Beyond the best ω, ρ = ω − 1, and the method slows down again.

Conjugate gradients

For a symmetric positive definite A, conjugate gradients minimises the A-norm of the error over a growing Krylov space. Its search directions are A-orthogonal and its residuals orthogonal. So in exact arithmetic it reaches x* in at most n steps: it is a direct method in disguise. In doubles that orthogonality decays, and the exact finish is lost.

const hilbert = [[1, "1/2", "1/3", "1/4"], ["1/2", "1/3", "1/4", "1/5"], ["1/3", "1/4", "1/5", "1/6"], ["1/4", "1/5", "1/6", "1/7"]];
const cg = methods.conjugateGradient(hilbert, [1, 1, 1, 1], { iterations: 8 });
cg.exactSteps;                          // 4: the exact iteration lands on x* after n = 4 steps
cg.rows.map((r) => r.error);            // the doubles stall near 2.5e-11, set by the condition number (about 15514)

The power method and inverse iteration

Repeatedly multiplying by A, and rescaling, turns almost any start towards the eigenvector of the eigenvalue of largest size, as long as that eigenvalue is real and strictly largest. The error in the eigenvector falls like |λ₂/λ₁|k. The Rayleigh quotient xᵀAx/xᵀx estimates λ₁, and for a symmetric matrix its error falls twice as fast. Inverse iteration runs the power method on (A − σI)⁻¹, whose largest eigenvalue belongs to the eigenvalue of A nearest the shift σ. The true eigenvalues come from the exact routines, so the error column is the true error.

const p = methods.powerMethod([[2, 1], [1, 3]], { iterations: 15 });
p.target;                 // 3.618… = (5 + √5)/2
p.convergenceFactor;      // 0.382… = λ₂/λ₁
p.rows[10].exactEstimate; // "103682/28657": exact Rayleigh quotients are ratios of Fibonacci-like numbers
methods.inverseIteration([[2, 1], [1, 3]], { shift: "3/2", iterations: 8 }).rows[8].estimate;   // 1.381966… = (5 − √5)/2

The QR algorithm

Factor Ak = QkRk and multiply back the other way: Ak+1 = RkQk = QkᵀAkQk. Every Ak has the eigenvalues of A. Without shifts, the entries below the diagonal shrink like the ratios |λi+1/λi|k, and the diagonal sorts itself into the eigenvalues, largest first. Complex pairs stay as 2×2 blocks. QR needs square roots, so qrAlgorithm runs in doubles only. Its rows give the diagonal and the largest subdiagonal entry, beside the exact eigenvalues.

Pivoting

Elimination in doubles can go badly wrong with a tiny pivot, even for a perfectly well-conditioned matrix. pivoting eliminates both ways and compares each result with the exact solution. The growth factor is the largest entry met during elimination divided by the largest entry of A.

methods.pivoting([[1e-17, 1], [1, 1]], [1, 2]);
// exact: [100000000000000000/99999999999999999, 99999999999999998/99999999999999999]
// none:    { x: [0, 1], error: 1, growth: 1e17 }: x₁ is lost entirely
// partial: { x: [1, 1], error: 1e-17, growth: 1 }

The exact routines elsewhere in this library never face this: they pivot on any nonzero entry, because there is no rounding to amplify.

Try itEdit the code and run it with @ratmath/linalg and Core's Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs