1. What this solves
Large phase errors can obscure a long conservative trajectory. Two-stage Gauss–Legendre uses two slopes inside each step and gives fourth-order endpoint accuracy. With accurately solved stages it is reversible and symplectic on canonical Hamiltonian systems, including nonseparable ones. It preserves quadratic first integrals in exact arithmetic.
2. When to use
Use a fixed step for a smooth canonical ODE with coupled position and momentum where long-run geometry and phase accuracy matter. Supply and a Jacobian or another reliable stage solver. Strongly oscillatory modes can be linearly stable with a large step while their phase still needs resolution.
3. When not to use (and what to use instead)
| Situation | Use instead |
|---|---|
| Second-order accuracy suffices for a canonical problem | implicit-midpoint solves only one stage. |
| Smooth separable mechanics with cheap force evaluations | stormer-verlet, or yoshida4 when fourth order is useful. |
| Fast decay should vanish over a large step | bdf2 for an accurate fixed grid or radau-iia-5 for adaptive steps. |
| Exact position and velocity constraints | rattle when its mechanical assumptions apply. |
| General nonstiff adaptive integration | dormand-prince-rk45. |
4. Algorithm
Let . Use , and
A = [[1/4, 1/4 - d],
[1/4 + d, 1/4 ]]
Find both stage states together. Start with both stages equal to . Form the stage residuals and the Newton blocks . Solve for the corrections and subtract them from the stage states. Evaluate the slopes at the converged stages and return
The weights satisfy . This yields symplecticity in canonical coordinates and exact preservation of quadratic invariants in exact arithmetic. The two nodes and weights give a symmetric map, so reversing the sign of reverses the step on the same root branch. For the scalar stability factor is . It has modulus at most one in the left half-plane, exactly one on the imaginary axis, and tends to one in the stiff limit.
5. Parameters
| Parameter | How to choose it |
|---|---|
| Signed fixed step | Choose it for phase and trajectory accuracy. Refine on a fixed time interval and check the observed order. A zero step returns the input. |
| Newton controls | The reference allows 12 updates and bounds each stage correction component by max(tol * scale, 4 * ulp(scale)). For each component, scale is its largest absolute value in the step input, the old stage, and the updated stage. Here ulp is floating-point spacing and the default is tol=2e-14. Compare invariants after tightening the solve. A singular matrix, nonfinite values or an exhausted limit is a failed step. |
7. Verification
Export step(fun, jac, t, y, h) -> y_next in port_adapter.py, with jac rows indexed by output component. Run python <skill>/checks/check_port.py from the adapter directory and show the output. Other languages can use checks/cases.json. The checker tests order, signed-step reversal, and two quadratic invariants of nonlinear rigid-body rotation.
On your model compare fixed-time errors with implicit-midpoint on the same grids. Track early and late peak energy errors on a long well-resolved run. Nonquadratic energy is generally approximate even with exact stage solves.
8. Failure signatures
| Symptom | Likely cause |
|---|---|
| Order drops to two or errors depend strongly on forcing time | Check the off-diagonal tableau signs, both stage times and the final weights. |
| Quadratic invariants or reversibility drift | Verify that both coupled stages converged, the root branch is consistent, and the force corresponds to your equations. |
| Rapid decay persists | The A-stable formula passes far-left modes with a factor near one. Use an L-stable solver for decay. |
| Stable trajectory has the wrong period | Reduce the step to resolve the oscillation. |
9. Files and commands
reference/gauss_legendre_4.py implements the fixed step. uv run examples/canonical.py compares with an exact solution obtained using shifted momentum. uv run verify/check_cases.py checks the port including mixed-scale stages, long-time energy, comparison with midpoint, oscillatory stability, and three broken ports. uv run checks/make_cases.py prints the fixture JSON.
10. Sources
- Butcher analyzes implicit Runge–Kutta formulas based on Gaussian quadrature and notes that the two-stage case had been proposed by Hammer and Hollingsworth (1955). Butcher (1964)
- Hairer, Lubich and Wanner prove that the Gauss methods conserve quadratic invariants (Thm. IV.2.1), are symmetric (Thm. V.2.1) and are symplectic (Thm. VI.4.2). Geometric Numerical Integration (2006)