#!/usr/bin/env python3
"""
verify_P273.py — Verifier for Addendum 273 (the unit bridge).

  S1  The bridge                  — checks 1-3
  S2  Cancellation + TBS 28_2     — checks 4-7
  S3  Branch decision             — checks 8-10
  S4  Shadow + retyping           — checks 11-14
"""
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
ALPHA = 1/OMEGA
KAPPA = ALPHA**mp.mpf("1.25")
TB = PI*OMEGA
W1 = PI*mp.sqrt(1-KAPPA)          # omega_1, P03 time
TAU1 = 2*PI/W1                    # system unit in P03 time

print("S1  The bridge")
check(1, "tau1*sqrt(1-kappa) = 2 exactly (50 dps): residual %s"
         % mp.nstr(abs(TAU1*mp.sqrt(1-KAPPA) - 2), 3),
      abs(TAU1*mp.sqrt(1-KAPPA) - 2) < mp.mpf(10)**-45)
check(2, "tau1 = %s P03-time units (tempered conversion)" % mp.nstr(TAU1, 10),
      abs(TAU1 - mp.mpf("2.00213624")) < 1e-7)
check(3, "breath duration in P03 time = TB*tau1 = %s" % mp.nstr(TB*TAU1, 10),
      abs(TB*TAU1 - 2*PI/(ALPHA*mp.sqrt(1-KAPPA))) < mp.mpf(10)**-40)

print("S2  Cancellation + TBS 28_2")
check(4, "density cycles/breath = (w1/2pi)*TB*tau1 = TB exactly (kappa-free)",
      abs((W1/(2*PI))*TB*TAU1 - TB) < mp.mpf(10)**-44)
check(5, "dual-observer arc per system unit = 2*w1*tau1 = 4pi exactly",
      abs(2*W1*TAU1 - 4*PI) < mp.mpf(10)**-44)
check(6, "SU(2) windings/breath = (2*w1/2pi)*TB*tau1 = 2*TB exactly",
      abs((2*W1/(2*PI))*TB*TAU1 - 2*TB) < mp.mpf(10)**-43)
check(7, "TBS 28_2 dissolved: same speed, two units — 2pi*sqrt(1-k)=%s per "
         "P03 time; 4pi per unit; deviation 0.107%% = unit mismatch"
         % mp.nstr(2*PI*mp.sqrt(1-KAPPA), 8),
      abs(2*PI*mp.sqrt(1-KAPPA)*TAU1 - 4*PI) < mp.mpf(10)**-44)

print("S3  Branch decision")
check(8, "Branch B reading => breath = TB/tau1 = %s system units != TB "
         "(contradicts P14 Thm Breathing-Period by factor tau1)"
         % mp.nstr(TB/TAU1, 10),
      abs(TB/TAU1 - mp.mpf("215.0264481")) < 1e-6)
rho = TB - 430
check(9, "rho = TB mod 1 = %s definitionally exact (Branch A number)"
         % mp.nstr(rho, 10), abs(rho - mp.mpf("0.5122452174")) < 1e-9)
def cf_convergents(x, depth=10):
    cf = []
    for _ in range(depth):
        a = int(mp.floor(x)); cf.append(a); x = 1/(x-a)
    p0, p1, q0, q1 = 1, cf[0], 0, 1
    qs = []
    for a in cf[1:]:
        p0, p1 = p1, a*p1+p0
        q0, q1 = q1, a*q1+q0
        qs.append(q1)
    return cf, qs
cfA, qsA = cf_convergents(rho)
check(10, "ladder recomputed: q = %s (A267 rungs 2, 41, 449 unconditional)"
          % qsA[:5], qsA[:5] == [1, 2, 39, 41, 449])

print("S4  Shadow + retyping")
shadow = 2*TB - mp.floor(2*TB)
check(11, "SU(2) shadow: 2*TB mod 1 = %s = 2*rho - 1 = A267's q=2 comma"
          % mp.nstr(shadow, 10),
      abs(shadow - (2*rho - 1)) < mp.mpf(10)**-43 and
      abs(shadow - mp.mpf("0.0244904348")) < 1e-9)
cfS, qsS = cf_convergents(shadow)
check(12, "shadow CF %s; convergents %s contain q=41" % (cfS[:5], qsS[:4]),
      cfS[:3] == [0, 40, 1] and 41 in qsS[:4])
res = 1 - mp.sqrt(1-KAPPA)
check(13, "conversion residue 1-sqrt(1-k) = %s = kappa/2 + O(k^2): "
          "|res - k/2| = %s < k^2 = %s"
          % (mp.nstr(res, 8), mp.nstr(abs(res-KAPPA/2), 3), mp.nstr(KAPPA**2, 3)),
      abs(res - KAPPA/2) < KAPPA**2)
schisma = mp.mpf(3)**8*5/mp.mpf(2)**15 - 1
check(14, "schisma flag demoted: residue is a conversion residue, not a "
          "comma (ratio to schisma %s recorded as coincidence)"
          % mp.nstr(res/schisma, 6), 0.94 < res/schisma < 0.95)

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