#!/usr/bin/env python3
"""LUMEN Second Edition -- Matter & Gauge chapter verifier (the full Standard-Model table).

Everything here is computed from the geometry at zero free parameters, EXCEPT where a row is
explicitly marked as input-dependent or conditional. The honest status of each result is carried
in the note and in the residual column -- read those, not just the PASS/FAIL.

  - J3(O) is forced (dim 27; Hermitian octonionic matrices are Jordan only for n<=3 -> 3 families).
  - The mass spectrum: m = m_e * exp(MU*(E - pi)), with MU = mu1/mu0 from the density moments and a
    STRUCTURAL spectral energy E per particle. Charged leptons and the c-quark are clean structural
    identities; the d- and t-quark energies are "structurally targeted" (moment-logs, open OP-D/OP-B);
    the b-quark closes to -0.25% only with a QCD scheme correction that consumes the measured alpha_s.
  - Gauge group U(1)xSU(2)xSU(3) from the division-algebra tower R/C/H/O -- a STRUCTURAL ASSIGNMENT,
    not a full derivation (the hypercharge representation is not constructed; corpus-flagged).
  - Electroweak: sin^2(theta_W) at NLO, and the M_W/M_Z ratio. M_W, M_Z absolute are tree-level only.
  - CKM: lambda and A (NLO) are clean; the CP phase delta_CKM is CP-violating and matches.
  - Neutrinos: the corrected PMNS angles; the seesaw mass m_nu3 (conditional); and leptonic delta_CP,
    which the corpus predicts CP-conserving (pi) -- the 240deg geometric value was REFUTED.

Particle predictions are judged at the percent level (corpus RMS ~2%); the column shows the residual,
since the measurements are far more precise. alpha_s(M_Z) is NOT derived by the corpus and every use
of it is flagged. Method: numpy integration + exact algebra. Pinned by verify_P084/P088/P082/P063."""
import sys, os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import numpy as np
from numpy import pi, exp, sin, cos
from se_verify_common import Report

trap = getattr(np, "trapezoid", np.trapz)
x = np.linspace(0, 1, 1_000_001)
I = lambda f: float(trap(f, x))
m_e = 0.51099895  # MeV, PDG

R = Report("Matter & Gauge",
    "The Standard-Model table from J3(O) and the density -- masses, gauge group, electroweak, CKM, neutrinos",
    "numpy integration of the density + exact algebra; particle predictions judged at the percent level",
    sources=["PDG 2024 quark masses (u,d,s MSbar 2GeV; c,b MSbar m(m); t pole): "
             "2.16/4.67/93.4/1270/4180/172760 MeV",
             "PDG 2024: m_tau/m_e=3477.23, m_mu/m_e=206.7683, sin^2 th_W=0.23122, |V_cb|=0.0411, lambda=0.2245",
             "NuFIT 5.3 (NO) angles: sin^2 th12=0.307, th23=0.545, th13=0.02224",
             "NuFIT 6.0 (NO, IC24+SK): delta_CP=212deg (177deg without SK-atm); Dm3l^2=+2.513e-3 eV^2"],
    pins=["verify_P084 (quark table)", "verify_P088 (Weinberg)", "verify_P082 (CKM)",
          "verify_P063 (PMNS)", "verify_P116 (tau mass)"])

# ---- forced algebra -------------------------------------------------------
R.check("J3(O) dimension", 3*1 + 3*8, 27, source="E6 fundamental", tol=1e-9,
        note="3 real diagonal + 3 octonion off-diagonal; H_n(O) is Jordan iff n<=3")
R.check("three families = max octonionic Jordan rank", True, kind="fact",
        note="no J4(O), hence no 4th generation -- the family count is forced, not fitted")
w = np.exp(2j*pi/3)
P = np.array([[(1/3)*sum(w**((a-b)*j) for j in range(3)) for b in range(3)] for a in range(3)])
R.check("Z3 Schur split (mass=diagonal, mixing/CP=off-diag)", np.allclose(P, np.eye(3)), kind="fact",
        note="(1/3) sum_j w^{(a-b)j} = delta_{a=b}: the diagonal is self-observation (mass)")

# ---- the mass lever, from the density moments -----------------------------
rho  = 16*pi**3*x**3 + 3*pi**2*x**2 + 2*pi*x
MU = I(x*rho)/I(rho)                       # = mu1/mu0
lamC = sin(pi/14)                          # Cabibbo/Wolfenstein lambda (the G2 half-alcove angle)
R.check("MU = mu1/mu0 (mass lever, from moments)", MU, 0.79334, source="density", tol=2e-3)

# ---- charged leptons (the headline: tau by full self-lensing integral) -----
lam = MU/(1+MU)
rho_l  = 16*pi**3*x**3 + 3*pi**2*x**2 + 2*pi*lam*x
rhop_l = 48*pi**3*x**2 + 6*pi**2*x + 2*pi*lam
E_self = I(rhop_l**2)/(2*I(rho_l)**2)
m_tau = exp(MU*(E_self - pi))
R.check("m_tau/m_e  (DERIVED, 0 params)", m_tau, 3477.23, source="PDG 2024", tol=2e-2,
        note="exp(MU*(E_self(lambda_tau)-pi)) by integrating the density; E_self=%.3f" % E_self)
m_mu = exp(MU*(pi**2 - pi))
R.check("m_mu/m_e  (geometric, E=pi^2)", m_mu, 206.7683, source="PDG 2024", tol=3e-2,
        note="exp(MU*(pi^2 - pi))")

# ---- the quark ladder: m = m_e*exp(MU*(E-pi)), structural E ----------------
mu0 = I(rho); mu1 = I(x*rho)
qE = {
 "m_u": ("pi^(7/5)",            pi**(7/5),               2.16,   "F  structural (Fano ratio 7/5)"),
 "m_d": ("ln(mu1)/MU",          np.log(mu1)/MU,          4.67,   "R  moment-log, 'structurally targeted' (OP-D open)"),
 "m_s": ("pi^2 - 1/7",          pi**2 - 1/7,             93.4,   "F  structural (one Fano step below muon)"),
 "m_c": ("pi^2 + pi",           pi**2 + pi,              1270.0, "F  exact integral identity int(3pi^2 x^2+2pi x)=pi^2+pi"),
 "m_b": ("pi^(7/3)",            pi**(7/3),               4180.0, "F* structural; raw -3.2%, closes to -0.25% with QCD corr (uses measured alpha_s)"),
 "m_t": ("pi^2+pi+ln(mu0)/MU",  pi**2 + pi + np.log(mu0)/MU, 172760.0,
         "R  formula-consistent t = +2.0% (NOT the 172.4GeV/-0.2% the table mis-states); m_t/m_c=alpha^-1, OP-B open"),
}
for name,(formula,E,pdg,status) in qE.items():
    m = m_e*exp(MU*(E - pi))
    R.check("%s = m_e*exp(MU*(%s-pi))" % (name, formula), m, pdg, unit="MeV",
            source="PDG", tol=4e-2, note=status)
# the cleanest scheme-stable light test
R.check("m_d/m_u = exp(2*MU*lambda^2*pi^2)", exp(2*MU*lamC**2*pi**2), 2.162, source="PDG", tol=2e-2,
        note="G2 Weyl step (lambda=sin(pi/14)); the cleanest light-quark ratio")
R.check("m_t/m_c = alpha^-1 (= MU0)", mu0, 136.03, source="PDG ratio", tol=2e-2,
        note="trace identity proved; full derivation OPEN (OP-B)")

# ---- gauge group from the division-algebra tower --------------------------
R.check("U(1) x SU(2) x SU(3) from R/C/H/O", True, kind="fact",
        note="C->U(1) (Hopf S1, EM); H->SU(2) (S3=SU(2), weak); O->G2=Aut(O) sup SU(3) (strong)")
R.check("gauge group: STRUCTURAL ASSIGNMENT, not full derivation", True, kind="note",
        note="P20 self-flags: the hypercharge representation is NOT constructed; maximal-subgroup argument")
R.check("dim G2 = 14 = 2*Im(O)", 14, 14, source="Aut(O)", tol=1e-9,
        note="G2 = automorphism group of the octonions; SU(3) = stabiliser of one imaginary unit")

# ---- electroweak ----------------------------------------------------------
sw2_LO = 1/(1+pi)
sw2_NLO = sw2_LO*(1 - 4*lamC**2/5)
R.check("sin^2(theta_W) NLO = (1/(1+pi))(1-4lam^2/5)", sw2_NLO, 0.23122, source="PDG effective", tol=1e-2,
        note="LO = 1/(1+pi) = %.5f (+4.4%%); 3/(8phi)=0.23176 is a near-identity, NOT the derived value" % sw2_LO)
cw = (1 - sw2_NLO)**0.5
R.check("M_W/M_Z = cos(theta_W)", cw, 80.377/91.1876, source="PDG", tol=1e-2,
        note="the clean EW ratio (-0.56%); rho-parameter = 1 (custodial)")
R.check("M_W, M_Z absolute: tree-level only (-3.7%)", True, kind="note",
        note="tree M_W=77.4, M_Z=88.3 GeV; the ~3.7% gap is the known SM radiative (Delta r) correction, not closed")
R.check("alpha_s(M_Z): NOT derived by the corpus", True, kind="note",
        note="F4 route off by ~8x, E6 result tautological, SU(5)/E6 embedding proved incompatible; every alpha_s use is PDG input")

# ---- CKM ------------------------------------------------------------------
R.check("CKM lambda = sin(pi/14)", lamC, 0.2245, source="PDG (Cabibbo)", tol=1e-2,
        note="G2 half-alcove angle theta_C = pi/14")
A0 = cos(pi/14)**2 * cos(2*pi/14)
A1 = A0*(1 - 4*lamC**2/5)
R.check("CKM A (NLO) = A0*(1-4lam^2/5)", A1, 0.823, source="PDG", tol=1e-2,
        note="Jordan-loop NLO; +0.075%% -- one of the cleanest hits in the sector")
R.check("|V_cb| = A*lambda^2", A1*lamC**2, 0.0411, source="PDG", tol=2e-2)
dCKM = (pi/3 + pi/28)*180/pi
R.check("delta_CKM (quark CP phase) = pi/3+pi/28", dCKM, 65.6, unit="deg", source="PDG (gamma)", tol=2e-2,
        note="CP-VIOLATING quark phase; this is where meson CP violation lives")
R.check("rho-bar, eta-bar, Jarlskog J: Bryant-calibrated (R)", True, kind="note",
        note="J=3.04e-5 vs (3.18+/-0.15)e-5 within 1 sigma; rho-bar/eta-bar use a 3-form matching convention")

# ---- neutrinos ------------------------------------------------------------
R.check("PMNS sin^2(theta_13) = (4/9)sin^2(pi/14)", (4/9)*sin(pi/14)**2, 0.02224, source="NuFIT", tol=3e-2,
        note="TBM predicted theta_13 = 0, REFUTED at >30 sigma (Daya Bay); the corrected value agrees")
R.check("PMNS sin^2(theta_23) = 15/28", 15/28, 0.545, source="NuFIT", tol=3e-2, note="near-maximal mixing")
R.check("PMNS sin^2(theta_12) = cos^2(pi/14)/3", cos(pi/14)**2/3, 0.307, source="NuFIT", tol=4e-2,
        note="quark-lepton complementarity: theta_12 ~ pi/4 - theta_C")
R.check("leptonic delta_CP = pi (CP-conserving)", 180.0, 212.0, unit="deg", source="NuFIT 6.0 NO (IC24+SK)", sigma=0.78,
        note="240deg geometric value REFUTED (A344, 3.3sig); A66 lepton value retired (A395, 6.1sig); "
             "current stance allowed at 0.78sig on the with-SK fit (0.16sig on the without-SK fit, 177deg)")
R.check("m_nu3 = 48.7 meV (seesaw, CONDITIONAL)", True, kind="note",
        note="conditional on the spectral-angle assumption; uses CODATA M_Pl anchor, no neutrino-data input; "
             "vs sqrt(Dm31^2)=50.1+/-0.2 meV (NuFIT 6.0, NO, m1~0): 48.7 sits below the global fit's 3-sigma "
             "range [49.5, 50.8] -- a STANDING TENSION, recorded 2026-07-02. The pre-committed kill is "
             "unchanged: a <=0.5%% direct JUNO/Hyper-K measurement of |Dm31^2| decides at >=3 sigma. "
             "Assumes NORMAL ordering.")
R.emit()
