# Euler's two-center problem: stars of unit mass fixed at (+-1, 0), planet moving in the plane.
# Checks the second integral for point masses and computes the Lyapunov exponent
# with softened cores (force of magnitude 1/eps^2 inside radius eps, as in 3body.bas).
# Rob - Clint Sprott's AI assistant, November 7, 2026
import numpy as np
from scipy.integrate import solve_ivp

def acc(x, y, eps):
    ax = ay = 0.0
    for c in (1.0, -1.0):
        dx, r = x - c, np.hypot(x - c, y)
        k = 1/r**3 if r >= eps else 1/(eps*eps*r)
        ax, ay = ax - k*dx, ay - k*y
    return ax, ay

def f(t, s, eps):                                  # orbit plus a tangent vector (finite difference)
    x, y, vx, vy = s[:4]; d = 1e-7
    ax, ay = acc(x, y, eps)
    axx, ayx = acc(x + d*s[4], y + d*s[5], eps)
    return [vx, vy, ax, ay, s[6], s[7], (axx - ax)/d, (ayx - ay)/d]

def C(s):                                          # second integral (centers at +-1, unit masses)
    x, y, vx, vy = s[:4]; r1, r2 = np.hypot(x - 1, y), np.hypot(x + 1, y)
    return (x*vy - y*vx)**2 - vy**2 - 2*((x - 1)/r1 - (x + 1)/r2)

def run(s0, eps, T):
    s, lam, Cs = np.array(s0 + [1.0, 0, 0, 0]), 0.0, []
    for t in range(int(T)):                        # renormalize the tangent vector every unit time
        s = solve_ivp(f, (0, 1), s, args=(eps,), method='DOP853', rtol=1e-11, atol=1e-11).y[:, -1]
        n = np.linalg.norm(s[4:]); lam += np.log(n); s[4:] /= n; Cs.append(C(s))
    return lam/T, np.ptp(Cs)

for eps in (0.0, 0.05):
    for s0 in ([0.0, 1.5, 0.9, 0.0], [0.26, -0.5, 0.0, 1.0]):
        lam, dC = run(s0, eps, 1000)
        print("eps = %.2f  x0 = %s  LE = %.3f  range of C = %.1e" % (eps, s0, lam, dC))
