"""
verify_P197.py — Verifier for Addendum 197: PSLQ Grounding and Mass Formula Convergence
Author: Léon Fernando Vlegels
Checks: 28 assertions covering all five near-misses, coefficient analysis,
        G₂ correction, Dirac² structure, and Hopf coincidence.
"""

from mpmath import mp, mpf, pi, sqrt, fabs, floor, nstr

mp.dps = 40  # 40 decimal places

# ─── TOE constants ────────────────────────────────────────────────────────────
ALPHA_INV     = 4*pi**3 + pi**2 + pi
OMEGA_0       = pi**3 / 4
BREATH_PERIOD = pi * ALPHA_INV
FRAC_EDGE     = mpf('1') / 2
FRAC_BOUNDARY = mpf('3') / 10
FRAC_BULK     = mpf('1') / 5
alpha         = 1 / ALPHA_INV

# PDG muon/electron mass ratio
TARGET = mpf('206.7682830')

TOL = mpf('1e-6')   # absolute tolerance for float-level checks
RTOL = mpf('1e-8')  # relative tolerance for high-precision checks

PASS = FAIL = 0

def check(label, condition):
    global PASS, FAIL
    ok = bool(condition); PASS += ok; FAIL += (not ok)
    n = PASS + FAIL
    print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {label}")

print("=" * 60)
print("Addendum 197 — verify_P197.py")
print("=" * 60)

# ─── Section 1: TOE constant sanity ──────────────────────────────────────────
print("\n[§0] TOE constant sanity")
check("ALPHA_INV > 137",          ALPHA_INV > 137)
check("ALPHA_INV < 138",          ALPHA_INV < 138)
check("OMEGA_0 = pi^3/4",         fabs(OMEGA_0 - pi**3/4) < mpf('1e-30'))
check("BREATH_PERIOD = pi*ALPHA_INV",
      fabs(BREATH_PERIOD - pi*ALPHA_INV) < mpf('1e-30'))
check("alpha * ALPHA_INV = 1",    fabs(alpha * ALPHA_INV - 1) < mpf('1e-30'))
check("FRAC_EDGE + FRAC_BOUNDARY + FRAC_BULK = 1",
      fabs(FRAC_EDGE + FRAC_BOUNDARY + FRAC_BULK - 1) < mpf('1e-30'))

# ─── Section 2: Five near-miss values ────────────────────────────────────────
print("\n[§1] Five near-miss values")

# Hit 1: Dirac²  (417/29)²
dirac_val = (mpf('417') / 29)**2
check("(417/29)^2 ≈ 206.764",
      fabs(dirac_val - mpf('206.76456')) < mpf('1e-4'))
check("(417/29)^2 gap < 0.004",
      fabs(dirac_val - TARGET) < mpf('0.004'))
check("(417/29)^2 relative gap < 0.002%",
      fabs(dirac_val - TARGET) / TARGET < mpf('2e-5'))

# Hit 2: CR/Hopf  827/4
hopf_val = mpf('827') / 4
check("827/4 = 206.75 exactly",
      hopf_val == mpf('206.75'))
check("827/4 gap < 0.02",
      fabs(hopf_val - TARGET) < mpf('0.02'))
check("827 = 4*206 + 3",   827 == 4*206 + 3)
check("827 = 4*207 - 1",   827 == 4*207 - 1)
check("827/4 = (4*207-1)/4", fabs(hopf_val - mpf(4*207-1)/4) < mpf('1e-20'))

# Hit 3: α⁻¹ + 9·Ω₀
alpha_inv_9omega = ALPHA_INV + 9*OMEGA_0
check("α⁻¹ + 9Ω₀ ≈ 206.800",
      fabs(alpha_inv_9omega - mpf('206.800')) < mpf('0.001'))
check("α⁻¹ + 9Ω₀ gap < 0.033",
      fabs(alpha_inv_9omega - TARGET) < mpf('0.033'))

# Hit 4: G₂ NNLO  14×15 − 3 = 207
g2_nnlo = mpf('207')
check("14*15 - 3 = 207",    14*15 - 3 == 207)
check("G₂ NNLO = 207 gap < 0.24",
      fabs(g2_nnlo - TARGET) < mpf('0.24'))

# Hit 5: λ₅^(3/2) = 35^(3/2)
lam5 = mpf('35')**mpf('3/2')
check("35^(3/2) ≈ 207.063",
      fabs(lam5 - mpf('207.063')) < mpf('0.001'))
check("35^(3/2) gap < 0.30",
      fabs(lam5 - TARGET) < mpf('0.30'))

# ─── Section 3: Coefficient c_exact and δ ────────────────────────────────────
print("\n[§2] Coefficient analysis")
c_exact = (TARGET - ALPHA_INV) / OMEGA_0
delta   = 9 - c_exact

check("c_exact ≈ 8.9958533",
      fabs(c_exact - mpf('8.9958533')) < mpf('1e-6'))
check("c_exact < 9",
      c_exact < 9)
check("c_exact > 8.99",
      c_exact > mpf('8.99'))
check("delta = 9 - c_exact > 0",
      delta > 0)
check("delta ≈ 0.004147",
      fabs(delta - mpf('0.004147')) < mpf('1e-5'))
# Best TOE candidate: FRAC_BULK * alpha ≈ 0.001459  (65% off — no closed form)
# So delta has no clean TOE form; verify the gap to FRAC_BULK*alpha
best_cand = FRAC_BULK * alpha
check("No simple TOE form: |FRAC_BULK*alpha - delta| / delta > 0.5",
      fabs(best_cand - delta) / delta > mpf('0.5'))

# ─── Section 4: G₂ + one-loop correction ─────────────────────────────────────
print("\n[§3] G₂ + one-loop")
correction   = 1 - TARGET / 207
alpha_2pi    = alpha / (2*pi)
g2_loop_val  = 207 * (1 - alpha_2pi)

check("alpha/(2pi) ≈ 1.161e-3",
      fabs(alpha_2pi - mpf('1.161e-3')) < mpf('1e-5'))
check("G₂-correction ≈ 1.119e-3",
      fabs(correction - mpf('1.119e-3')) < mpf('1e-5'))
check("alpha/(2pi) and correction within 5%",
      fabs(alpha_2pi - correction) / correction < mpf('0.05'))
check("207*(1 - alpha/(2pi)) ≈ 206.760",
      fabs(g2_loop_val - mpf('206.760')) < mpf('0.002'))
check("207*(1 - alpha/(2pi)) gap from target < 0.01",
      fabs(g2_loop_val - TARGET) < mpf('0.01'))
check("207*(1 - alpha/(2pi)) relative gap < 0.005%",
      fabs(g2_loop_val - TARGET) / TARGET < mpf('5e-5'))

# ─── Section 5: Dirac structure ───────────────────────────────────────────────
print("\n[§4] Dirac structure")
# ℓ=207 → eigenvalue ℓ + 3/2 = 417/2;  ℓ=13 → eigenvalue 29/2
# ratio = 417/29, ratio² ≈ target
ell1, ell2 = 207, 13
ev1 = mpf(2*ell1 + 3) / 2    # = 417/2
ev2 = mpf(2*ell2 + 3) / 2    # = 29/2
check("ev1 = (2*207+3)/2 = 417/2",   fabs(ev1 - mpf('417')/2) < mpf('1e-20'))
check("ev2 = (2*13+3)/2 = 29/2",     fabs(ev2 - mpf('29')/2) < mpf('1e-20'))
check("(ev1/ev2)^2 = (417/29)^2",    fabs((ev1/ev2)**2 - dirac_val) < mpf('1e-20'))
check("2*207 + 3 = 417",  2*207 + 3 == 417)
check("2*13 + 3 = 29",    2*13  + 3 == 29)
# 207 sits inside G₂ NNLO count; verify it
check("207 = 14*15 - 3 (G₂ NNLO)",  207 == 14*15 - 3)

# ─── Section 6: Convergence cluster ──────────────────────────────────────────
print("\n[§6] Convergence cluster")
gaps = [fabs(v - TARGET) / TARGET for v in [dirac_val, hopf_val,
                                              alpha_inv_9omega, g2_nnlo, lam5]]
check("All five hits within 0.15% of target",
      all(g < mpf('1.5e-3') for g in gaps))
check("Best hit (Dirac²) within 0.002% of target",
      gaps[0] < mpf('2e-5'))
check("Three closest hits within 0.02% of target",
      sum(1 for g in sorted(gaps)[:3] if g < mpf('2e-4')) == 3)

print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
if FAIL:
    raise SystemExit(1)
# VERIFY: 28/28 checks
