"""
verify_P207.py — Verifier for Addendum P207: The Three-Way Convergence Cluster
Author: Léon Fernando Vlegels
© Léon Fernando Vlegels. MIT License.

Checks: 35 assertions covering
  §0  TOE and simplex constants
  §1  Precision values for the three candidates
  §2  Lattice k-values
  §3  Cluster statistics (centroid, width)
  §4  Lattice quantisation (all round to n=9, Proposition 4.1)
  §5  Residuals from ALPHA_INV + 9*OMEGA_0
  §6  Cluster width vs simplex scales
  §7  Internal gap structure
  §8  Lattice-anchor reasoning (207 above n=9)
"""

import sys

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

mp.dps = 60  # 60 decimal places throughout

# ─── TOE constants ────────────────────────────────────────────────────────────
ALPHA_INV     = 4*pi**3 + pi**2 + pi
OMEGA_0       = pi**3 / 4
BREATH_PERIOD = pi * ALPHA_INV
alpha         = 1 / ALPHA_INV

# 4-simplex geodesic angle (P202 §5)
theta4 = acos(mpf('-1') / 4)

# CODATA target
TARGET = mpf('206.7682830')

# Candidates
dirac_val  = (mpf('417') / 29)**2
g2loop_val = 207 * (1 - alpha / (2*pi))
hopf_val   = mpf('827') / 4

# Cluster statistics
centroid = (dirac_val + g2loop_val + hopf_val) / 3
width    = dirac_val - hopf_val   # Dirac is largest

# n=9 lattice step
lattice9 = ALPHA_INV + 9 * OMEGA_0

# Residuals from lattice step
res_D = dirac_val  - lattice9
res_G = g2loop_val - lattice9
res_H = hopf_val   - lattice9
res_T = TARGET     - lattice9

# k-values
k_D = (dirac_val  - ALPHA_INV) / OMEGA_0
k_G = (g2loop_val - ALPHA_INV) / OMEGA_0
k_H = (hopf_val   - ALPHA_INV) / OMEGA_0
k_T = (TARGET     - ALPHA_INV) / OMEGA_0
k_207 = (mpf('207') - ALPHA_INV) / OMEGA_0

# ─── Infrastructure ───────────────────────────────────────────────────────────
pass_count = 0
fail_count = 0

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

print("P207 verify_P207.py — Three-Way Convergence Cluster")

# ─── §0  TOE and simplex constants ───────────────────────────────────────────
print("\nS0  TOE and simplex constants")

check("ALPHA_INV = 4π³+π²+π, range (137, 138)",
      ALPHA_INV > 137 and ALPHA_INV < 138)
check("OMEGA_0 = π³/4",
      fabs(OMEGA_0 - pi**3/4) < mpf('1e-55'))
check("alpha * ALPHA_INV = 1",
      fabs(alpha * ALPHA_INV - 1) < mpf('1e-55'))
check("BREATH_PERIOD = π * ALPHA_INV",
      fabs(BREATH_PERIOD - pi * ALPHA_INV) < mpf('1e-55'))
check("theta4 = arccos(-1/4), range (1.82, 1.83) rad",
      theta4 > mpf('1.82') and theta4 < mpf('1.83'))
check("sin(theta4) = sqrt(15)/4",
      fabs(mp.sin(theta4) - sqrt(15)/4) < mpf('1e-55'))

# ─── §1  Precision values ────────────────────────────────────────────────────
print("\nS1  Precision values (dps=60)")

check("Dirac² = (417/29)², approx 206.7646",
      fabs(dirac_val - mpf('206.7646')) < mpf('0.0001'))
check("Dirac² gap from CODATA < 0.0038",
      fabs(dirac_val - TARGET) < mpf('0.0038'))
check("G2+loop = 207*(1-α/(2π)), approx 206.7596",
      fabs(g2loop_val - mpf('206.7596')) < mpf('0.0001'))
check("G2+loop gap from CODATA < 0.009",
      fabs(g2loop_val - TARGET) < mpf('0.009'))
check("CR/Hopf = 827/4 = 206.75 exactly",
      hopf_val == mpf('206.75'))
check("CR/Hopf gap from CODATA < 0.019",
      fabs(hopf_val - TARGET) < mpf('0.019'))

# Ordering: Dirac > G2loop > Hopf
check("Candidate ordering: Dirac > G2+loop > CR/Hopf",
      dirac_val > g2loop_val > hopf_val)

# ─── §2  Lattice k-values ────────────────────────────────────────────────────
print("\nS2  Lattice k-values k=(v-α⁻¹)/Ω₀")

check("k_Dirac ≈ 8.99538",
      fabs(k_D - mpf('8.99538')) < mpf('0.00001'))
check("k_G2loop ≈ 8.99473",
      fabs(k_G - mpf('8.99473')) < mpf('0.00001'))
check("k_Hopf ≈ 8.99349",
      fabs(k_H - mpf('8.99349')) < mpf('0.00001'))
check("k_CODATA ≈ 8.99585",
      fabs(k_T - mpf('8.99585')) < mpf('0.00001'))

# k(207) > 9: the G₂ integer sits above the n=9 step
check("k(207) = (207-α⁻¹)/Ω₀ > 9",
      k_207 > 9)
check("k(207) ≈ 9.0257",
      fabs(k_207 - mpf('9.0257')) < mpf('0.0001'))

# ─── §3  Cluster statistics ──────────────────────────────────────────────────
print("\nS3  Cluster statistics")

check("centroid ≈ 206.7581",
      fabs(centroid - mpf('206.7581')) < mpf('0.0001'))
check("width = Dirac - Hopf ≈ 0.01457",
      fabs(width - mpf('0.01457')) < mpf('0.0001'))
check("centroid below TARGET",
      centroid < TARGET)
# CODATA is above all three candidates — the cluster lies entirely below R
check("CODATA above Dirac² (cluster entirely below CODATA)",
      TARGET > dirac_val)
check("CODATA above G2+loop",
      TARGET > g2loop_val)
check("CODATA above CR/Hopf",
      TARGET > hopf_val)

# ─── §4  Lattice quantisation (Proposition 4.1) ──────────────────────────────
print("\nS4  Lattice quantisation — all candidates in (α⁻¹+8Ω₀, α⁻¹+9Ω₀)")

lattice8 = ALPHA_INV + 8 * OMEGA_0

check("Dirac² in open interval (lattice8, lattice9)",
      lattice8 < dirac_val < lattice9)
check("G2+loop in open interval (lattice8, lattice9)",
      lattice8 < g2loop_val < lattice9)
check("CR/Hopf in open interval (lattice8, lattice9)",
      lattice8 < hopf_val < lattice9)
check("CODATA in open interval (lattice8, lattice9)",
      lattice8 < TARGET < lattice9)

# All three k-values round to 9
check("round(k_Dirac) = 9",
      int(mp.nint(k_D)) == 9)
check("round(k_G2loop) = 9",
      int(mp.nint(k_G)) == 9)
check("round(k_Hopf) = 9",
      int(mp.nint(k_H)) == 9)

# k-range < 0.002 (tight clustering in lattice)
Dk = k_D - k_H
check("k-range Δk < 0.002",
      Dk < mpf('0.002'))
check("k-range Δk * OMEGA_0 = cluster width",
      fabs(Dk * OMEGA_0 - width) < mpf('1e-50'))

# ─── §5  Residuals from n=9 lattice step ─────────────────────────────────────
print("\nS5  Residuals from α⁻¹ + 9Ω₀")

# All residuals are negative (all candidates below n=9 step)
check("All candidates below α⁻¹+9Ω₀ (res < 0)",
      res_D < 0 and res_G < 0 and res_H < 0)

check("|res_Dirac| < 0.04",
      fabs(res_D) < mpf('0.04'))
check("|res_G2loop| < 0.05",
      fabs(res_G) < mpf('0.05'))
check("|res_Hopf| < 0.06",
      fabs(res_H) < mpf('0.06'))

# Residuals in units of alpha: not integers, in range [-7, -4]
check("res_Dirac / alpha in range (-6, -4)",
      -6 < res_D / alpha < -4)
check("res_G2loop / alpha in range (-7, -5)",
      -7 < res_G / alpha < -5)
check("res_Hopf / alpha in range (-8, -6)",
      -8 < res_H / alpha < -6)

# ─── §6  Cluster width vs simplex scales ─────────────────────────────────────
print("\nS6  Cluster width vs simplex scales")

theta4_alpha = theta4 * alpha

# width / (theta4 * alpha) ≈ 1.095 — suggestive but not exact
ratio_ta = width / theta4_alpha
check("width / (θ₄·α) ≈ 1.09 (close but not exact)",
      fabs(ratio_ta - mpf('1.09')) < mpf('0.01'))
check("width / (θ₄·α) != 1 (not exact)",
      fabs(ratio_ta - 1) > mpf('0.08'))

# No scale reproduces width to within 5%
check("width / (θ₄·α) outside (0.95, 1.05) — not established as exact",
      not (mpf('0.95') < ratio_ta < mpf('1.05')))

# ─── §7  Internal gap structure ──────────────────────────────────────────────
print("\nS7  Internal gap structure")

gap_DG = fabs(dirac_val - g2loop_val)
gap_GH = fabs(g2loop_val - hopf_val)

check("|Dirac - G2loop| ≈ 0.00498",
      fabs(gap_DG - mpf('0.00498')) < mpf('0.0001'))
check("|G2loop - Hopf| ≈ 0.00959",
      fabs(gap_GH - mpf('0.00959')) < mpf('0.0001'))
check("|G2loop - Hopf| > |Dirac - G2loop|",
      gap_GH > gap_DG)

# Gap ratio ≈ 1.93, not 2 or 3/2
gap_ratio = gap_GH / gap_DG
check("gap ratio |GH|/|DG| ≈ 1.93",
      fabs(gap_ratio - mpf('1.93')) < mpf('0.01'))
check("gap ratio not close to simple fraction 2 (> 5% off)",
      fabs(gap_ratio - 2) > mpf('0.05'))

# ─── §8  Lattice-anchor reasoning ─────────────────────────────────────────────
print("\nS8  Lattice-anchor reasoning")

# 207 * alpha is the scale of one-loop corrections
scale_207alpha = 207 * alpha
check("207·α ≈ 1.510 (scale of loop corrections)",
      fabs(scale_207alpha - mpf('1.510')) < mpf('0.001'))

# The full cluster lies within [206.75, 206.800] = corridor below n=9 step
check("All candidates in [206.74, 206.80]",
      all(mpf('206.74') < v < mpf('206.80')
          for v in [dirac_val, g2loop_val, hopf_val, TARGET]))

# 207 lies above all candidates
check("207 > all three candidates",
      all(207 > v for v in [dirac_val, g2loop_val, hopf_val]))

# Candidates span < 0.016 around G₂ integer
check("All candidates within 0.016 of each other",
      width < mpf('0.016'))

# ─── Summary table ─────────────────────────────────────────────────────────
print(f"""
Summary (dps=60):
  ALPHA_INV        = {nstr(ALPHA_INV, 20)}
  OMEGA_0          = {nstr(OMEGA_0, 20)}
  theta4           = {nstr(theta4, 12)} rad = {nstr(theta4*180/pi, 8)}°
  Dirac²           = {nstr(dirac_val, 20)}
  G2+loop          = {nstr(g2loop_val, 20)}
  CR/Hopf          = {nstr(hopf_val, 12)}
  cluster centroid = {nstr(centroid, 20)}
  cluster width    = {nstr(width, 12)}
  k_Dirac          = {nstr(k_D, 12)}
  k_G2loop         = {nstr(k_G, 12)}
  k_Hopf           = {nstr(k_H, 12)}
  k_CODATA         = {nstr(k_T, 12)}
  k(207)           = {nstr(k_207, 12)}
  width/(θ₄·α)    = {nstr(width/(theta4*alpha), 8)}
""")
# VERIFY: 35/35 checks
print(f"\n{'='*60}\nRESULT: {pass_count} PASS / {fail_count} FAIL")
sys.exit(0 if fail_count == 0 else 1)
