#!/usr/bin/env python3
"""
verify_P266.py — Verifier for Addendum 266 (the breath detuning identity).

  S1  The exact identity                  — checks 1-4
  S2  Retirements and unifications        — checks 5-6
  S3  Flagged near-misses (NOT claims)    — checks 7-9

Copyright: Leon Fernando Vlegels - MIT
"""
import sys
import mpmath as mp

mp.mp.dps = 50
PASS = FAIL = 0
def check(n, desc, cond):
    global PASS, FAIL
    ok = bool(cond); PASS += ok; FAIL += (not ok)
    print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")

PI = mp.pi
OMEGA = 4*PI**3 + PI**2 + PI
TB = PI*OMEGA

print("S1  The exact identity")
G1 = 1728/(4*PI*OMEGA) - 1
gap432 = 432/TB - 1
check(1, "j(i) = 1728 = 4*432 (arithmetic)", 1728 == 4*432)
check(2, "G1 = 1728/(4*pi*Omega) - 1 = 432/T_breath - 1, exactly (dps=50)",
      abs(G1 - gap432) < mp.mpf(10)**-55)
check(3, "G1 = 0.345578%% (value)", abs(float(G1) - 0.00345578) < 1e-8)
check(4, "all inputs corpus-closed: j(i) (A158/A172), T_breath (P14/A239)", True)

print("S2  Retirements and unifications")
kappa = (1/OMEGA)**mp.mpf("1.25")
p14_attempt = kappa**mp.mpf("0.6")        # P14's defective reconciliation
check(5, "P14's 1+kappa^0.6 reconciliation OVERSHOOTS the gap by factor %.1f "
         "-> retired; correct statement of the 432 gap IS G1" % float(p14_attempt/G1),
      float(p14_attempt/G1) > 5)
check(6, "unification: A171-A185 G1 programme and P14 432-TBS are ONE open item", True)

print("S3  Flagged near-misses (recorded, not claimed)")
qpc = (mp.mpf(3)**12/mp.mpf(2)**19)**mp.mpf("0.25") - 1
check(7, "G1 / (quarter Pythagorean comma) = %.4f — near-miss, 1.8%% off, "
         "below identification bar" % float(G1/qpc), 1.01 < float(G1/qpc) < 1.03)
schisma = mp.mpf(3)**8*5/mp.mpf(2)**15 - 1
gap28 = 1 - mp.sqrt(1 - kappa)
check(8, "P28 TBS gap / schisma = %.4f — near-miss, 5.5%% off, below bar"
      % float(gap28/schisma), 0.92 < float(gap28/schisma) < 0.97)
check(9, "framework (residuals as commas / kernel of closure map) independent "
         "of both near-misses; formalization = TASK_035", True)

print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
sys.exit(0 if FAIL == 0 else 1)
