#!/usr/bin/env python3
"""
PROVENANCE: EMPIRICAL

PST Computation 138 — CKM consistency with the S_3-leading structural-scope theorem
===================================================================================
Witness computation for the CKM claim of the structural-scope theorem
(sec:yukawa-scope): at leading (S_3-symmetric) order the Yukawa matrices are
degenerate, so the leading CKM matrix is the identity, and the observed mixing
is a T(C)-contingent perturbation. The values themselves are out of scope
(the structural-scope theorem forbids deriving them); what CAN be checked is
that the measured CKM is CONSISTENT with "identity plus a small perturbation":

  * V_CKM is close to the identity: ||V - I|| is Cabibbo-sized (~0.23), i.e.
    O(lambda) with lambda the Wolfenstein parameter, NOT O(1);
  * every off-diagonal element is <= the Cabibbo angle |V_us|;
  * the measured matrix is (approximately) unitary, V^dagger V = I.

Because this compares against measured data (PDG), the provenance is EMPIRICAL:
it is a falsifiable consistency bound, not a derivation. Had the measured CKM
been O(1)-far from the identity, the S_3-leading reading would be excluded.
"""
import numpy as np

# PDG 2022 CKM magnitudes |V_ij| (central values)
V = np.array([
    [0.97435, 0.22500, 0.00369],
    [0.22486, 0.97349, 0.04182],
    [0.00857, 0.04110, 0.999118],
])
I = np.eye(3)
lam = 0.22500          # Wolfenstein lambda ~ |V_us| (the Cabibbo angle)

dev_fro = np.linalg.norm(V - I, "fro")
dev_spec = np.linalg.norm(V - I, 2)
max_offdiag = np.max(np.abs(V - np.diag(np.diag(V))))
# unitarity: V is only magnitudes here, but rows/cols of |V|^2 should sum ~1
row_sums = np.sum(V ** 2, axis=1)
col_sums = np.sum(V ** 2, axis=0)
unit_defect = max(np.max(np.abs(row_sums - 1)), np.max(np.abs(col_sums - 1)))

c1 = dev_spec < 0.5                       # far from O(1): a genuinely small perturbation
c2 = max_offdiag <= lam + 1e-9            # no off-diagonal exceeds the Cabibbo angle
c3 = unit_defect < 2e-3                   # measured magnitudes are unitary to sub-percent

print("Measured CKM |V_ij| (PDG):")
for r in V:
    print("   " + "  ".join(f"{x:.5f}" for x in r))
print(f"\n||V - I||_F   = {dev_fro:.4f}")
print(f"||V - I||_2   = {dev_spec:.4f}   (Cabibbo-sized ~ lambda={lam}; << 1)")
print(f"max off-diag  = {max_offdiag:.4f}   (<= |V_us| = {lam})")
print(f"unitarity defect max|sum|V|^2 - 1| = {unit_defect:.2e}")
print(f"\nchecks: small-perturbation={c1}  offdiag<=Cabibbo={c2}  unitary={c3}")
ok = c1 and c2 and c3
print("RESULT:", "PASS  (measured CKM = identity + O(lambda) perturbation, "
      "consistent with the S_3-leading theorem)" if ok else "FAIL")
