#!/usr/bin/env python3
"""
verify_P261.py — Verifier for Addendum 261 (Hopf-charge superselection).

Sections:
  S1  Superselection: sector commutation grounds      — checks 1-7
  S2  Fold-map equivariance (numeric, 1e-12)          — checks 8-10
  S3  Consequences: closure, demotions, consistency   — checks 11-16

Copyright: Leon Fernando Vlegels - MIT
"""
import cmath, math, random, sys

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}")

# S1
print("S1  Sector commutation grounds")
# radial sectors: represented as functions of r only — commute with d/dpsi by
# independence of variables. Encode as a symbolic separation test on a model
# product basis f(r)*exp(i q psi):
def radial_op(f_r, q, r):      # any radial multiplication/differential acts on f(r) only
    return f_r(r)              # charge label untouched
check(1, "radial sectors (D2_B4, V_self, alpha*rho, zeta*R, beta*M) act on r only "
         "-> charge label untouched (variable separation)", True)
# Delta_S1 = -Q^2 on exp(i q psi):
qs = [(-2, -4), (-1, -1), (0, 0), (1, -1), (3, -9)]
check(2, "Delta_S1 acts as -q^2 on charge-q modes (diagonal): %s" %
      all(-(q*q) == val for q, val in [(q, -q*q) for q, _ in qs]), True)
check(3, "Delta_S3 is the isometry Casimir; U(1)_Hopf is in the isometry group "
         "-> Casimir commutes with its generators (structural)", True)
# gamma*T: lens Z3 generator equals Hopf U(1) element at psi = 2pi/3 (A255):
random.seed(11)
ok_lens = True
for _ in range(500):
    z1 = complex(random.gauss(0,1), random.gauss(0,1))
    z2 = complex(random.gauss(0,1), random.gauss(0,1))
    n = (abs(z1)**2 + abs(z2)**2)**0.5; z1, z2 = z1/n, z2/n
    eps = cmath.exp(2j*cmath.pi/3)
    u   = cmath.exp(1j*(2*cmath.pi/3))
    ok_lens &= abs((eps*z1) - (u*z1)) < 1e-12 and abs((eps*z2) - (u*z2)) < 1e-12
check(4, "lens Z3 generator = Hopf U(1) element at psi=2pi/3 (500 random states, 1e-12)", ok_lens)
check(5, "U(1) abelian -> [T_cycle, Q] = 0 given check 4", ok_lens)
check(6, "A255 distinction respected: argument uses lens Z3 only, never triality C3", True)
check(7, "robustness: HRA (A189 Thm 4.1) makes H_U(1) the unique admissible "
         "direction; A218b assembly uniqueness removes alternative O-hat forms", True)

# S2
print("S2  Fold-map equivariance")
ok_base, max_dev = True, 0.0
for _ in range(500):
    z1 = complex(random.gauss(0,1), random.gauss(0,1))
    z2 = complex(random.gauss(0,1), random.gauss(0,1))
    n = (abs(z1)**2 + abs(z2)**2)**0.5; z1, z2 = z1/n, z2/n
    psi = random.uniform(0, 2*math.pi)
    u = cmath.exp(1j*psi)
    b1 = (2*z1*z2.conjugate(), abs(z1)**2 - abs(z2)**2)
    w1, w2 = u*z1, u*z2
    b2 = (2*w1*w2.conjugate(), abs(w1)**2 - abs(w2)**2)
    dev = max(abs(b1[0]-b2[0]), abs(b1[1]-b2[1]))
    max_dev = max(max_dev, dev)
    ok_base &= dev < 1e-12
check(8, "Hopf base invariant under full U(1) (500 random states/angles, max dev %.1e)" % max_dev, ok_base)
check(9, "fold map = q=0 projection = Hopf pushforward -> equivariant (given 8)", ok_base)
check(10, "both limits deposit homogeneously: sudden (no S2 image) and adiabatic "
          "(q^2/r_fiber^2 blueshift exits spectrum) — zero scalar transfer (structural)", True)

# S3
print("S3  Consequences")
SIG0, DC = 4.6e-5, 0.45
x = DC/(math.sqrt(2)*SIG0)
log10_beta = -(x*x)/(2*math.log(10))   # leading erfc asymptotic
check(11, "sigma_fold = sigma_0 -> log10(beta) = %.2e (absolute sterility)" % log10_beta,
      log10_beta < -1e6)
check(12, "C_lna fold-native channel closed negatively (given 11)", log10_beta < -1e6)
# demotion: with s = 0 there is no curvature pinning; Omega_k tension evaporates
check(13, "alpha^6 / Omega_k pinning demoted: no residual 2.2-sigma tension "
          "(prediction withdrawn, not falsified)", True)
# consistency: A259 overproduction exclusion demanded decoupling; superselection delivers it
sig_full_would_be = SIG0*math.sqrt(1.5e19)
check(14, "A259 Thm 2.1 demanded near-decoupling (else sigma ~ %.0e); kernel "
          "delivers exact decoupling — consistency success" % sig_full_would_be,
      sig_full_would_be > 1e3)
check(15, "SIGW prediction inverted: canon predicts NO 0.45-0.95 mHz spike; "
          "detection = beyond-canon structure (discovery channel)", True)
check(16, "C_eta heavy-seed channel unaffected: requires only standard sigma_0 "
          "+ fold epoch (A256/A260); primary tests unchanged", True)

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