#!/usr/bin/env python3
"""
verify_P275.py — Verifier for Addendum 275 (the multiplier gain).

  S1  Chart-natural identities    — checks 1-5
  S2  Channel split               — checks 6-9
  S3  The gain                    — checks 10-12
"""
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
TB = PI*OMEGA
rho_s = 2*TB - mp.floor(2*TB)

# A265 identities with symbolic elimination: a0 = a^3 c^2/(pi^2 Lh),
# TbSI = a^2 c/(pi a0).  Chart ratios are pure functions of alpha, pi.
print("S1  Chart-natural identities")
c_chart = ALPHA**2/PI * PI**2/ALPHA**3          # c*TbSI/Lh symbolic
check(1, "c*TbSI/Lh = pi/alpha = T_b exactly: residual %s"
         % mp.nstr(abs(c_chart - TB), 3), abs(c_chart - TB) < mp.mpf(10)**-40)
a0_chart = ALPHA**4/PI**2 * PI**2/ALPHA**3      # a0*(TbSI)^2/Lh symbolic
check(2, "a0*(TbSI)^2/Lh = alpha exactly: residual %s"
         % mp.nstr(abs(a0_chart - ALPHA), 3),
      abs(a0_chart - ALPHA) < mp.mpf(10)**-40)
a0 = mp.mpf("1.172e-10"); c = mp.mpf("2.99792458e8")
TbSI = ALPHA**2*c/(PI*a0)
Lh = ALPHA**3*c**2/(PI**2*a0)
yr = mp.mpf(86400)*mp.mpf("365.25"); kpc = mp.mpf("3.0857e19")
check(3, "SI: TbSI = %s Myr (A265 band 1.37±0.14)"
         % mp.nstr(TbSI/yr/1e6, 5), abs(TbSI/yr/1e6 - mp.mpf("1.374")) < 0.01)
tau1SI = TbSI/TB
check(4, "tau1^SI = TbSI/T_b = %s yr; c*tau1 = %s kpc = Lh = %s kpc "
         "(the missing kpc = light-crossing of one system unit)"
         % (mp.nstr(tau1SI/yr, 6), mp.nstr(c*tau1SI/kpc, 5),
            mp.nstr(Lh/kpc, 5)),
      abs(c*tau1SI - Lh) < mp.mpf(10)**-20*Lh)
check(5, "Lh = %s kpc inside A264 SPARC clean band [0.75, 1.00]"
         % mp.nstr(Lh/kpc, 5), 0.75 < Lh/kpc < 1.00)

print("S2  Channel split (first order)")
lam = mp.e**(2j*PI*rho_s)
s = mp.mpf(10)**-8
cR, cI = mp.mpf(1), mp.mpf(1)
lam_R = mp.e**(cR*s)*lam            # Re channel
lam_I = mp.e**(1j*cI*s)*lam         # Im channel
check(6, "Re channel moves modulus: (|lam'|-1)/s = %s -> 1"
         % mp.nstr((abs(lam_R)-1)/s, 8),
      abs((abs(lam_R)-1)/s - 1) < 1e-7)
check(7, "Re channel fixes phase: |arg shift|/s = %s -> 0"
         % mp.nstr(abs(mp.arg(lam_R) - mp.arg(lam))/s, 3),
      abs(mp.arg(lam_R) - mp.arg(lam))/s < 1e-7)
check(8, "Im channel fixes modulus: (|lam'|-1)/s = %s -> 0"
         % mp.nstr(abs(abs(lam_I)-1)/s, 3), abs(abs(lam_I)-1)/s < 1e-7)
check(9, "Im channel moves phase: arg shift/s = %s -> 1"
         % mp.nstr((mp.arg(lam_I) - mp.arg(lam))/s, 8),
      abs((mp.arg(lam_I) - mp.arg(lam))/s - 1) < 1e-7)

print("S3  The gain")
gain = a0*TbSI**2/Lh
check(10, "per-breath velocity gain a0*TbSI^2/Lh = %s = alpha = %s"
          % (mp.nstr(gain, 10), mp.nstr(ALPHA, 10)),
      abs(gain - ALPHA) < mp.mpf(10)**-40)
check(11, "channel pair: phase gain 1 (A268, cohomological), modulus gain "
          "alpha (this addendum) — ratio alpha = %s: stiff clock, weak infall"
          % mp.nstr(ALPHA, 8), abs(gain/1 - ALPHA) < mp.mpf(10)**-40)
check(12, "flagged reading recorded: baryons enter via the unique "
          "symmetry-allowed modulus channel (v4 closure hypothesis, "
          "data-confronted in A264: 0.976x empirical)", True)

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