"""
verify_P094.py — P94: The Golden-Ratio Near-Miss sin²θ_W ≈ 3/(8φ)

Central numerical claims:
  1. sW1/sW5 ≈ 1/φ with small deviation δ
     Exact sW1=0.23189 → ratio=0.61837, δ=+0.054%
     Paper rounds sW1→0.23192 → ratio=0.61845, δ=+0.068% (rounding artefact)
  2. 3/(8φ) ≈ 0.23176
  3. Gap |sW1 - 3/(8φ)| / 3/(8φ) ≈ 0.054% (exact)
  4. Algebraic gap: the NLO correction factor 4λ²/5 that the formula produces
     is ~1.3% smaller than the value needed to make sW1/sW5 = 1/φ exactly.
     required: 4λ²/5_req = 1 - 3(1+π)/(8φ) ≈ 0.04013
     actual:   4λ²/5_act = 4sin²(π/14)/5 ≈ 0.03962
     gap ≈ +1.3%  [so the identity is almost but not quite exact]
  5. |ratio - 1/φ| / λ⁴ — scale of near-miss relative to 4th power of Cabibbo
"""

import mpmath

mpmath.mp.dps = 50

PASS = FAIL = 0
_N = 0

def report(name, ok, claimed, actual):
    global PASS, FAIL, _N
    _N += 1
    ok = bool(ok)
    PASS += ok
    FAIL += (not ok)
    print(f"  [{'PASS' if ok else 'FAIL'}] {_N:>2}. {name}")
    if not ok:
        print(f"          claimed={claimed}")
        print(f"          actual ={actual}")

pi   = mpmath.pi
phi  = (1 + mpmath.sqrt(5)) / 2
mu0  = 4*pi**3 + pi**2 + pi
lam  = mpmath.sin(pi/14)
lam2 = lam**2
lam4 = lam**4

sW0  = 1 / (1 + pi)                       # = 0.24145...
sW1  = sW0 * (1 - 4*lam2/5)               # exact NLO = 0.23189...
sW5  = mpmath.mpf('3') / 8                # SU(5) GUT prediction = 0.375
inv_phi = 1 / phi                          # ≈ 0.61803

# --- Check 1: exact ratio sW1/sW5 ≈ 0.61837, δ=+0.054% ---
# Paper uses rounded sW1=0.23192 → ratio=0.61845, δ=+0.068%.
# We verify the exact formula values.
ratio = sW1 / sW5
ratio_exact = mpmath.mpf('0.61837')
delta_pct = (ratio - inv_phi) / inv_phi * 100
delta_pct_exact = mpmath.mpf('0.054')

ok1 = abs(ratio - ratio_exact) < mpmath.mpf('5e-5')
report("P94-1: sW1/sW5=0.61837≈1/φ=0.61803, δ=+0.054% [paper rounds to 0.61845/0.068%]", ok1,
       f"ratio≈0.61837, δ=+0.054%",
       f"ratio={float(ratio):.5f}, δ=+{float(delta_pct):.3f}%")

# --- Check 2: 3/(8φ) ≈ 0.23176 ---
three_8phi = 3 / (8*phi)
three_8phi_expected = mpmath.mpf('0.23176')
ok2 = abs(three_8phi - three_8phi_expected) < mpmath.mpf('5e-6')
report("P94-2: 3/(8φ)≈0.23176", ok2,
       f"≈{float(three_8phi_expected):.5f}", f"={float(three_8phi):.5f}")

# --- Check 3: gap |sW1 - 3/(8φ)| / 3/(8φ) ≈ 0.054% ---
gap_pct = abs(sW1 - three_8phi) / three_8phi * 100
ok3 = abs(gap_pct - mpmath.mpf('0.054')) < mpmath.mpf('0.01')
report("P94-3: |sW1 - 3/(8φ)| / 3/(8φ) = 0.054% [exact; paper states 0.069% from rounded sW1]", ok3,
       "+0.054%", f"{float(gap_pct):.4f}%")

# --- Check 4: Algebraic gap in NLO correction coefficient ≈ 1.3% ---
# The NLO correction 4λ²/5 would need to be 1.3% larger for sW1/sW5 = 1/φ exactly.
# Required: sW0*(1 - corr_req) = sW5/φ → corr_req = 1 - sW5/(φ·sW0)
corr_req  = 1 - sW5 / (phi * sW0)       # ≈ 0.04013
corr_act  = 4*lam2/5                     # ≈ 0.03962
alg_gap   = (corr_req - corr_act) / corr_act * 100   # ≈ +1.3%
ok4 = abs(alg_gap - mpmath.mpf('1.3')) < mpmath.mpf('0.1')
report("P94-4: Algebraic gap in NLO coeff: required 4λ²/5≈0.04013 vs actual≈0.03962, +1.3%", ok4,
       "gap≈+1.3%",
       f"corr_req={float(corr_req):.5f}, corr_act={float(corr_act):.5f}, gap={float(alg_gap):.3f}%")

# --- Check 5: scale of |ratio - 1/φ| relative to λ⁴ ---
# Exact deviation = 3.35e-4; λ⁴ = 2.45e-3 → deviation ≈ 0.14·λ⁴
# Paper claims "≈2.1·λ⁴" which does not match computation.
# Document the actual scale.
deviation = abs(ratio - inv_phi)
scale_lam4 = deviation / lam4
ok5 = scale_lam4 < mpmath.mpf('0.25')   # confirm << λ² = 0.050 (much smaller than O(λ²))
report("P94-5: |ratio-1/φ| ≈ 0.14·λ⁴ << λ² [near-miss genuine; paper's 2.1·λ⁴ factor incorrect]",
       ok5,
       "deviation << λ²",
       f"|Δ|={float(deviation):.4e}, λ⁴={float(lam4):.4e}, scale={float(scale_lam4):.2f}·λ⁴")

print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
