"""Exact-formula Floquet optimizer for y'' + u(s)y = 0, 1 <= u <= R**2. Floating-point evaluation is not a formal proof or an interval certificate. All times passed to this module are dimensionless. """ from __future__ import annotations from dataclasses import dataclass import math from typing import Sequence import numpy as np from scipy.optimize import brentq @dataclass(frozen=True) class Candidate: winding: int eta: float high_time: float low_time: float log_gain: float def cell_times(R: float, eta: float) -> tuple[float, float]: """High-u then low-u residence times; numerically stable at large |eta|.""" if not math.isfinite(R) or R <= 1 or not math.isfinite(eta): raise ValueError('Require finite R > 1 and finite eta.') if eta >= 0: low = math.atan2(1.0, eta) high = (math.pi - math.atan2(R, eta)) / R else: low = math.pi - math.atan2(1.0, -eta) high = math.atan2(R, -eta) / R return high, low def cell_gain(R: float, eta: float) -> float: if R <= 1: raise ValueError('R must exceed one.') return 0.5 * math.log1p((R * R - 1.0) / (1.0 + eta * eta)) def solve_eta(R: float, period: float) -> float: if not math.pi / R < period < math.pi: raise ValueError('Require pi/R < cell period < pi.') bracket = 1.0 while sum(cell_times(R, -bracket)) < period or sum(cell_times(R, bracket)) > period: bracket *= 2.0 if bracket > 1e15: raise ArithmeticError('Period is too close to an endpoint for this float implementation.') return float(brentq(lambda a: sum(cell_times(R, a)) - period, -bracket, bracket, xtol=2e-13, rtol=2e-14)) def fixed_period(R: float, S: float) -> tuple[float, list[Candidate]]: """Return the sharp log-spectral-radius bound and every positive candidate. Winner ties must not be broken into a mathematical uniqueness claim by floating-point tolerance. Extremely near resonance endpoints use exact arithmetic or certified interval evaluation instead. """ if not math.isfinite(R) or R <= 1 or not math.isfinite(S) or S <= 0: raise ValueError('Require finite R > 1 and S > 0.') candidates = [] for n in range(1, math.floor(R * S / math.pi) + 1): p = S / n if not math.pi / R < p < math.pi: continue a = solve_eta(R, p) h, l = cell_times(R, a) candidates.append(Candidate(n, a, h, l, n * cell_gain(R, a))) return max((c.log_gain for c in candidates), default=0.0), candidates def free_period(R: float) -> Candidate: """Unique root G(eta) = eta*tau(eta); eta is the optimal growth rate.""" if R <= 1: raise ValueError('R must exceed one.') a = float(brentq(lambda e: cell_gain(R, e) - e * sum(cell_times(R, e)), 0.0, R, xtol=2e-14)) h, l = cell_times(R, a) return Candidate(1, a, h, l, cell_gain(R, a)) def transfer(u: float, duration: float) -> np.ndarray: if not math.isfinite(u) or u <= 0 or not math.isfinite(duration) or duration < 0: raise ValueError('Require u > 0 and duration >= 0, both finite.') w = math.sqrt(u) c, s = math.cos(w * duration), math.sin(w * duration) return np.array([[c, s / w], [-w * s, c]], dtype=float) def monodromy(controls: Sequence[float], durations: Sequence[float]) -> np.ndarray: if len(controls) != len(durations) or not len(controls): raise ValueError('Control and duration lists must have the same positive length.') M = np.eye(2) for u, t in zip(controls, durations): M = transfer(float(u), float(t)) @ M return M def log_spectral_radius(M: np.ndarray) -> float: """For a real determinant-one 2x2 monodromy; not stable for huge products.""" if np.asarray(M).shape != (2, 2): raise ValueError('Expected a 2x2 matrix.') d = abs(float(np.trace(M))) / 2.0 return math.acosh(d) if d > 1.0 else 0.0 def phase_speed(theta: float, u: float) -> float: return u * math.cos(theta)**2 + math.sin(theta)**2 def radial_speed(theta: float, u: float) -> float: return (u - 1.0) * math.sin(theta) * math.cos(theta) def phase_selector(theta: float, eta: float, R: float) -> float: c, s = math.cos(theta), math.sin(theta) return R * R if c * s + eta * c * c > 0 else 1.0 def defect_integrand(theta: float, u: float, eta: float, R: float) -> float: star = phase_selector(theta, eta, R) switch = abs(math.sin(theta) * math.cos(theta) + eta * math.cos(theta)**2) return switch * abs(u - star) / (phase_speed(theta, u) * phase_speed(theta, star))