# Classical Hamiltonian Systems: example checks

These programs accompany worked examples in the [Classical Hamiltonian Systems volume](/classical-mechanics/). They recompute explicit examples, test consequential signs and constraints, and include deliberately broken controls. They do not prove the general geometric, KAM, Nekhoroshev, or differential-Galois theorems cited in the articles.

## Run

Download a script and run it with Python 3:

```sh
python3 check_actions.py
python3 check_lax_models.py
python3 check_separation_reduction.py
python3 check_global_perturbations.py
python3 check_extended_mechanics.py
python3 spherical_pendulum_rotation.py
python3 rigid_top_orbits.py
python3 quartic_levels.py
python3 fiber_observables.py
python3 perturbed_orbit_observables.py
python3 toda_particle_checks.py
python3 turning_charts_and_caustics.py
```

From the repository root, prefix each filename with `public/computations/classical-mechanics/`. The programs print their checks and numerical parameters without changing saved scientific artifacts.

| Script | Examples |
|---|---|
| [check_actions.py](check_actions.py) | Oscillator regular sets, period normalization, quartic actions, pendulum separatrices, resonant averages, and a quadratic oscillator's Hannay angle |
| [check_lax_models.py](check_lax_models.py) | Matrix Poisson brackets, rational and dynamical r-matrices, Toda spectral data, Calogero–Moser motion, rigid bodies, constrained Neumann motion, and confocal billiards |
| [check_separation_reduction.py](check_separation_reduction.py) | Hamilton–Jacobi separation, Stäckel integrals, ellipsoid caustics and reconstruction, a separated Lax pair, turning points, regular reduction, Dirac brackets, Kepler symmetry, and reduced Toda rank |
| [check_global_perturbations.py](check_global_perturbations.py) | Spherical-pendulum singularities, monodromy, singular quotients, global-action obstructions, homological equations, resonant and Melnikov calculations, and variational-equation examples |
| [check_extended_mechanics.py](check_extended_mechanics.py) | Reduced periodic Toda action and period by quadrature and time evolution, and the semitoric height through a finite sequence of joint spectra |
| [spherical_pendulum_rotation.py](spherical_pendulum_rotation.py) | Axial turns per nutation from turning-height quadrature and constrained Cartesian trajectories, and continuation around the focus in either direction |
| [rigid_top_orbits.py](rigid_top_orbits.py) | Euler separatrix and return of the body momentum, accumulated attitude rotation, and the Lagrange nutation period and precession angle |
| [quartic_levels.py](quartic_levels.py) | Compare the two-turning-point action rule for a quartic well with independent sine-basis and position-grid energies |
| [fiber_observables.py](fiber_observables.py) | Nutation, passage through a polar seam, bundle curvature, a toric cut and the reconstruction of Kepler, Neumann and Ruijsenaars–Schneider trajectories |
| [perturbed_orbit_observables.py](perturbed_orbit_observables.py) | Rotating quadratic oscillator, small divisors, harmonic forcing of a separatrix, and the conserved combinations along a resonance |
| [toda_particle_checks.py](toda_particle_checks.py) | Open Toda rank, endpoint weights, periodic spectral gaps, Calogero reconstruction and the rational particle limit |
| [turning_charts_and_caustics.py](turning_charts_and_caustics.py) | Momentum charts at turning points, finite and projective confocal tangency, collision limits and four successive billiard reflections |

## Environment

The calculations use Python 3, NumPy, SciPy, SymPy, and mpmath. The tested environment is Python 3.9.6, NumPy 2.0.2, SciPy 1.13.1, SymPy 1.14.0, and mpmath 1.3.0. To reproduce those dependencies in an isolated environment:

```sh
python3 -m venv .venv
.venv/bin/python -m pip install numpy==2.0.2 scipy==1.13.1 sympy==1.14.0 mpmath==1.3.0
.venv/bin/python check_actions.py
```

Use a Python version compatible with those package versions when creating the environment. Matplotlib 3.9.4 is additionally used for the optional figure regeneration below.

## Actions and adiabatic motion

`check_actions.py` checks canonical transformations, oscillator rank, cycle-basis transformations, and the moving quadratic-oscillator frame by exact symbolic differentiation. A generator that omits the time derivative of the changing frame is a negative control. Quartic-oscillator action and period quadratures use 50 decimal digits at three energies, with nonunit mass and coupling; the reported error is relative to the corresponding beta-function formula.

For the dimensionless pendulum, elliptic formulas and their asymptotic expansions are compared at specified distances from the separatrix. Independent integration of Hamilton's equations measures periods at energies 0.4, 1.9, and 1.99 and recovers a swing and a rotation with identical bottom-crossing times. Quadrature of the phase-space area checks its derivatives on both sides of the saddle. The elliptic library uses parameter $m=k^2$, whereas the article defines the modulus $k$.

The Hannay example integrates the original position–momentum equations with SciPy's DOP853 method, then reconstructs the endpoint angle modulo $2\pi$. Subtracting the known dynamical phase and choosing the representative in $(-\pi,\pi]$ is sufficient for this loop's small geometric angle. Its loop has $b=\tfrac12\sin s$, $c=\exp(\tfrac12\cos s)$, $a=(1+b^2)/c$, and $s=\varepsilon t$. For rates 0.1, 0.05, 0.025, and 0.0125, the program reports the geometric-angle discrepancy from $-\pi/8$, the sampled maximum relative action variation, and a sampled five-point ODE residual. The residual is an absolute maximum-component difference, not a global error certificate. A tighter tolerance and halved maximum step test solver error independently of the finite-rate adiabatic discrepancy. Reversing the loop and removing its mixed term give separate sign and zero-angle controls.

## Lax matrices and mechanical examples

`check_lax_models.py` differentiates matrix entries before taking traces. In particular, it verifies the full three-particle Calogero tensor bracket symbolically. A wrong coefficient in its position-dependent r-matrix passes the two-particle case but fails this larger check. Separate controls reverse the rational bracket, alter its spectral kernel, reverse Neumann denominators, and omit the billiard normal's normalization.

The Toda reconstruction starts from the explicit three-by-three Jacobi matrix in the script, rebuilds it from its eigenvalues and endpoint weights, and compares a centered time derivative at time 0.3 with the Lax commutator. It reports the induced matrix infinity norm; halving the differentiation step from 0.001 to 0.0005 reduces the error by approximately four. For Calogero motion, eigenvalues of the free matrix are differentiated twice at time 0.4 and compared with the particle force in the maximum-component norm. Euler and ellipsoid calculations halve the RK4 time step and compare the maximum absolute changes in their listed invariants and constraints. These conservation residuals complement, rather than replace, the exact equation checks.

The billiard check follows 100 successive reflections in the ellipse with squared semiaxes 4 and 1. It separately reports the largest caustic change, boundary-equation residual, and squared-speed residual. No third-party trajectory data or digitized figure is used.

## Separation and reduction

`check_separation_reduction.py` independently differentiates the Hamilton–Jacobi complete integral, inverts the Stäckel relations, retains both position–momentum and momentum–momentum Dirac corrections, and checks the Kepler bracket algebra and a rank-five minor. It verifies Cartesian oscillator reconstruction through both radial turning points. Negative controls include a nonseparable potential, an incomplete Dirac bracket, a wrongly normalized separated momentum, and constant functions on a regular rotation momentum level whose ambient bracket does not descend.

For the ellipsoid with squared semiaxes `(1, 4, 9)`, energy `1/2`, and caustic parameter `3`, the initial separated coordinates are `(2, 6)` in the positive octant with positive separated momenta. A separated integration reconstructed in Cartesian coordinates is compared with a 2048-step Cartesian constrained integration over time `[0, 0.5]`. At 8, 16, 32, and 64 RK4 steps, the largest absolute Cartesian state discrepancies are approximately `1.662e-7`, `1.042e-8`, `6.516e-10`, and `4.072e-11`. Independent 40-digit Abel quadratures give phase and time residuals around `1.35e-15` and `9.74e-15`; their endpoints came from double-precision integration, so these are not claims of 40-digit trajectory accuracy. The program reports the position, tangency, and caustic residuals separately. This comparison stays in a regular coordinate chart.

## Singular geometry and perturbations

`check_global_perturbations.py` checks the spherical-pendulum critical curve against independent minimization of its reduced potential. It also verifies the focus quadratic blocks, invariant quotient brackets, logarithmic action shear, and magnetic sphere flux. Resetting the principal argument is an explicit failed monodromy control.

The canonical-generator calculation compares its exact numerically integrated pullback with first- and second-order Hamiltonian expansions at perturbation sizes `0.02`, `0.01`, and `0.005`. It tests quadratic and cubic remainder scaling separately. Melnikov quadrature uses three fixed frequency/phase pairs and truncation intervals `[-8, 8]`, `[-12, 12]`, and `[-16, 16]`, compared against the independently derived closed form.

For the normal variational equation, both positive loops start at `t=1/2` and circle one puncture at radius `1/2`. Fixed-step RK4 with 512, 1024, and 2048 steps transports the full complex fundamental matrix. Refinement differences, nilpotence, and commutation residuals use the Frobenius norm; determinant and trace errors are scalar absolute errors. The noncommuting case has coupling 1 and the commuting control has coupling 2. These finite matrices check the construction and normalization. The article's analytic monodromy argument supplies the nonintegrability proof.

## Regenerate the figures

Run from the repository root:

```sh
MPLCONFIGDIR=.tmp/matplotlib python3 figures-src/classical-mechanics/separatrix-periods.py
MPLCONFIGDIR=.tmp/matplotlib python3 figures-src/classical-mechanics/plot_spherical_pendulum.py --output public/figures/classical-mechanics/spherical-pendulum-bifurcation.svg
```

The editable sources document each reader question, mathematical regime, provenance, and independent checks. The final SVGs have a light canvas and use shape or line style as well as gray levels. The action-frequency figure is reused from the existing Foundations/Learn calculation rather than copied into this directory. Plot sampling and drawing accuracy are separate from the numerical integration errors reported by the scientific checks.
