#!/usr/bin/env python3
"""
verify_P131.py — Verification script for Addendum P131
Muon Spectral Mass from Peirce Off-Diagonal Back-Coupling

Verifies:
  1. TOE constants (ALPHA_INV, ALPHA, BREATH_PERIOD)
  2. NLO fold amplitude A_NLO = A_LO * (1 - 4λ²/5)  [P75, P81, P99]
  3. Wave-function renormalisation Z_μ = 1 + α·A_NLO  [Theorem 4.1]
  4. LO muon mass m_μ^LO = m_e · exp(Μ·(π²−π))      [eq. 1]
  5. NLO muon mass m_μ^NLO = m_μ^LO / Z_μ            [Corollary 4.2]
  6. Residual vs PDG 105.6584 MeV ≈ +0.0007%

Copyright: Léon Fernando Vlegels. License: MIT.
"""

import math
import sys

# ── TOE constants (frozen — kernel/math/quat_s3.py) ─────────────────────────
ALPHA_INV   = 4*math.pi**3 + math.pi**2 + math.pi   # π + π² + 4π³  (P35 T1)
ALPHA       = 1.0 / ALPHA_INV
BREATH_PERIOD = math.pi * ALPHA_INV                  # π · α⁻¹ ≈ 432

# ── Physical inputs ──────────────────────────────────────────────────────────
M_E_MEV     = 0.51100                # electron mass MeV (PDG 2024, scale anchor)
M_MU_PDG    = 105.6584               # muon mass MeV     (PDG 2024)

# Spectral moment Μ = μ₁/μ₀ = 0.793338  (P80, T2)
MU          = 0.793338

# ── NLO fold amplitude (P75, P81, P99) ──────────────────────────────────────
lam         = math.sin(math.pi / 14)         # Wolfenstein λ = sin(π/14)
A_LO        = math.cos(math.pi/14)**2 * math.cos(2*math.pi/14)
nlo_supp    = 1.0 - (4 * lam**2) / 5        # NLO suppression: 1 − 4λ²/5
A_NLO       = A_LO * nlo_supp

# ── Renormalisation factor Z_μ  (Theorem 4.1) ───────────────────────────────
alpha_A_NLO = ALPHA * A_NLO
Z_mu        = 1.0 + alpha_A_NLO

# ── LO muon mass  (eq. 1 / P36) ─────────────────────────────────────────────
m_mu_LO     = M_E_MEV * math.exp(MU * (math.pi**2 - math.pi))

# ── NLO muon mass  (Corollary 4.2 / Dyson equation) ─────────────────────────
m_mu_NLO    = m_mu_LO / Z_mu

# ── Residuals ────────────────────────────────────────────────────────────────
res_LO_pct  = (m_mu_LO  - M_MU_PDG) / M_MU_PDG * 100.0
res_NLO_pct = (m_mu_NLO - M_MU_PDG) / M_MU_PDG * 100.0

# ── Report ───────────────────────────────────────────────────────────────────
print("=" * 60)
print("  P131 Verification — Muon Spectral Mass (NLO)")
print("=" * 60)
print(f"\nTOE constants")
print(f"  ALPHA_INV        = {ALPHA_INV:.6f}  (π + π² + 4π³)")
print(f"  ALPHA            = {ALPHA:.8e}  (1/ALPHA_INV)")
print(f"  BREATH_PERIOD    = {BREATH_PERIOD:.6f}  (π·α⁻¹, expect ≈ 432)")

print(f"\nNLO fold amplitude  (P75, P81, P99)")
print(f"  λ  = sin(π/14)   = {lam:.6f}")
print(f"  A_LO             = {A_LO:.6f}  (expect 0.85636)")
print(f"  1 − 4λ²/5        = {nlo_supp:.6f}  (expect 0.96039)")
print(f"  A_NLO            = {A_NLO:.6f}  (expect 0.82243)")

print(f"\nRenormalisation factor  (Theorem 4.1)")
print(f"  α·A_NLO          = {alpha_A_NLO:.6f}  (expect 6.002e-3)")
print(f"  Z_μ              = {Z_mu:.6f}  (expect 1.006002)")

print(f"\nMuon mass derivation")
print(f"  m_e              = {M_E_MEV:.5f} MeV  (PDG, scale anchor)")
print(f"  Μ·(π²−π)         = {MU*(math.pi**2 - math.pi):.6f}")
print(f"  m_μ^LO           = {m_mu_LO:.4f} MeV  (expect 106.293, res {res_LO_pct:+.3f}%)")
print(f"  m_μ^NLO          = {m_mu_NLO:.4f} MeV  (expect 105.659, res {res_NLO_pct:+.6f}%)")
print(f"  PDG              = {M_MU_PDG:.4f} MeV")

print()

# ── Assertions ───────────────────────────────────────────────────────────────
PASS = FAIL = 0
_N = 0

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

def check(label, value, expected, tol_abs):
    ok = abs(value - expected) <= tol_abs
    mark(label, ok)
    print(f"         got {value:.8g}, expect {expected:.8g}, tol ±{tol_abs:.2e}")

print("Assertions")

# A_LO and A_NLO match paper values (eq. 3)
check("A_LO  = 0.85636",            A_LO,        0.85636, 5e-5)
check("A_NLO = 0.82243",            A_NLO,       0.82243, 5e-5)

# Z_μ matches paper eq. 2
check("α·A_NLO = 6.002e-3",         alpha_A_NLO, 6.002e-3, 5e-6)
check("Z_μ = 1.006002",             Z_mu,        1.006002, 5e-6)

# LO mass matches paper (~106.293 MeV, residual ~+0.60%)
check("m_μ^LO = 106.293 MeV",       m_mu_LO,     106.293, 5e-3)

# NLO mass matches paper (~105.659 MeV)
check("m_μ^NLO = 105.659 MeV",      m_mu_NLO,    105.659, 5e-3)

# NLO residual vs PDG: paper claims +0.0007%
# Tolerance: allow ±0.003% (within the ~5-sig-fig precision floor noted in §5)
check("NLO residual ≈ +0.0007%",     res_NLO_pct, 0.0007, 3e-3)

# NLO mass is strictly closer to PDG than LO mass
delta_LO  = abs(m_mu_LO  - M_MU_PDG)
delta_NLO = abs(m_mu_NLO - M_MU_PDG)
closer = delta_NLO < delta_LO
mark("NLO mass is closer to PDG than LO mass", closer)
print(f"         |LO−PDG| = {delta_LO:.4f} MeV,  |NLO−PDG| = {delta_NLO:.4f} MeV")

# Dyson equation consistency: m_mu_NLO * Z_mu == m_mu_LO (round-trip)
roundtrip = m_mu_NLO * Z_mu
check("Round-trip: m_μ^NLO · Z_μ = m_μ^LO",
      roundtrip, m_mu_LO, 1e-10)

print()
if FAIL == 0:
    print("  ✓  ALL CHECKS PASSED — P131 NLO muon mass verified.")
else:
    print("  ✗  ONE OR MORE CHECKS FAILED.")
print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
sys.exit(0 if FAIL == 0 else 1)
