#!/usr/bin/env python3
# framed-rational status: EXACT -- integer / modular-F_p / cyclotomic arithmetic; no float in any asserted check (float, if present, only formats an exact rational for display).
# =====================================================================
#  weak_spectrum.py  --  the propagating electroweak mass spectrum as the
#  quadratic-fluctuation (Hessian) spectrum of the finite gauge-Higgs
#  action S_rho around the drive-broken vacuum.
#
#  Closes the dynamical residue flagged in 27-fields Remark 6.5 / Prop 6.6:
#  the propagating W,Z masses (not only the algebraic adjoint gapping of
#  Theorem 6.4) derived from the explicit finite Higgs doublet, its vacuum
#  orbit, and its stabiliser -- float-free, in exact arithmetic.
#
#  Three independent exact checks:
#    A. structural (over Q): the second variation of the gauged-Higgs
#       hopping term = the gauge-boson mass operator; its spectrum, the
#       photon kernel, custodial rho=1, and the ratio M_W^2/M_Z^2 = 5/8
#       once sin^2 theta_W = 3/8 (the charge-trace value).
#    B. finite field F_q (q=13,5): the SAME operator built with the Pauli
#       generators over F_{q^2} (i = sqrt(-1) in F_q); photon = exact
#       kernel mod q, the two W eigenvalues equal (rho=1 ingredient),
#       Z eigenvalue = (g^2+g'^2) v^2/4 -- all as field identities.
#    C. the drive picture (Theorem 6.4): Ad_delta on sl_2 has eigenvalues
#       {1, g^2, g^-2}; the unit eigenvalue is the propagating zero mode
#       (massless photon, transfer-operator eigenvalue 1 = winding rate 0);
#       g^{+-2} are the W^+- winding rates (mass = winding rate, realisation B6).
# =====================================================================

from fractions import Fraction as Fr
import sympy as sp

def hr(t): print("\n" + "="*70 + "\n" + t + "\n" + "="*70)

# ---------------------------------------------------------------------
# A. STRUCTURAL (exact rationals).  The gauged Higgs kinetic (hopping)
#    term is  S_H = sum_links [ phi^d phi - Re phi_x^d U_xy phi_y ].
#    With U = exp(i eps X), X = sum_a A_a X_a,  X_a = g_a T_a, the term
#    quadratic in the gauge fluctuation A is  eps^2 * (X phi0)^d (X phi0),
#    i.e. the mass operator  M2_{ab} = (X_a phi0)^d (X_b phi0)  (Hermitian).
#    This is the Hessian (second variation) of S_rho at the broken vacuum.
# ---------------------------------------------------------------------
hr("A. STRUCTURAL: mass operator = Hessian of the gauged-Higgs action over Q")

g, gp, v = sp.symbols('g gp v', positive=True)
I = sp.I
half = sp.Rational(1,2)
# doublet generators (T^a = sigma^a/2), hypercharge Y = 1/2 on the Higgs doublet
T1 = half*sp.Matrix([[0,1],[1,0]])
T2 = half*sp.Matrix([[0,-I],[I,0]])
T3 = half*sp.Matrix([[1,0],[0,-1]])
Yh = half*sp.eye(2)
# gauged generators (couplings g for SU(2), g' for U(1)_Y)
X = [g*T1, g*T2, g*T3, gp*Yh]
labels = ["W1","W2","W3","B"]
phi0 = sp.Matrix([0, v])            # neutral vacuum: Q=T3+Y kills it -> photon unbroken

# mass operator M2_{ab} = (X_a phi0)^dagger (X_b phi0), symmetrised (Hermitian)
def dag(M): return M.conjugate().T
Xphi = [Xa*phi0 for Xa in X]
M2 = sp.zeros(4,4)
for a in range(4):
    for b in range(4):
        M2[a,b] = sp.simplify((dag(Xphi[a])*Xphi[b])[0])
M2 = sp.re(sp.simplify(M2))         # Hermitian -> real symmetric mass matrix
print("mass operator M^2 (basis W1,W2,W3,B), units where it is exact in g,g',v:")
sp.pprint(M2)

eig = M2.eigenvals()
print("\neigenvalues (multiplicity):")
for val, mult in eig.items():
    print("   ", sp.simplify(val), "  x", mult)

# read off the physical masses
MW2 = sp.simplify(g**2 * v**2 / 4)
MZ2 = sp.simplify((g**2+gp**2) * v**2 / 4)
print("\n  M_W^2 =", MW2, "   (W1,W2 -> W^+-, degenerate => custodial)")
print("  M_Z^2 =", MZ2)
print("  photon: 0 eigenvalue present? ", sp.Integer(0) in [sp.simplify(k) for k in eig])

# photon eigenvector = stabiliser direction (Q = T3 + Y)
ns = M2.nullspace()
print("  photon eigenvector (kernel of M^2):", ns[0].T, " ~ (W3:g', B:g) = (sinW, cosW)")

# custodial rho = M_W^2 / (M_Z^2 cos^2 theta_W),  cos^2 = g^2/(g^2+g'^2)
cos2 = g**2/(g**2+gp**2)
rho = sp.simplify(MW2/(MZ2*cos2))
print("\n  custodial rho = M_W^2/(M_Z^2 cos^2 thetaW) =", rho, "  (exact, from doublet structure)")
# the clean float-free statement of rho=1: the (W3,B) block is rank-1 for ANY g,g'
WB = sp.Matrix([[g**2, -g*gp],[-g*gp, gp**2]])*v**2/4
print("  det of (W3,B) block =", sp.simplify(WB.det()), " == 0 identically  <=>  rho=1, photon massless")

# impose the charge-trace value sin^2 thetaW = 3/8  ->  g'^2/g^2 = 3/5
hr("A'. impose the charge-trace Weinberg angle sin^2 thetaW = 3/8")
sin2 = sp.Rational(3,8)
ratio_gp2_g2 = sin2/(1-sin2)          # g'^2/g^2 = sin^2/cos^2
print("  sin^2 thetaW = 3/8  =>  g'^2/g^2 =", ratio_gp2_g2)
MW2_over_MZ2 = sp.simplify(g**2/(g**2+gp**2)).subs(gp**2, ratio_gp2_g2*g**2)
print("  M_W^2/M_Z^2 = cos^2 thetaW =", sp.nsimplify(MW2_over_MZ2), " = 5/8 :", MW2_over_MZ2==sp.Rational(5,8))
print("  => M_W : M_Z  =  sqrt(5) : sqrt(8) = sqrt(5/8) =", sp.sqrt(sp.Rational(5,8)))

# ---------------------------------------------------------------------
# B. FINITE FIELD: build the SAME operator over F_q, exact modular arithmetic.
#    i = sqrt(-1) in F_q (exists for q = 1 mod 4).  We show, as field
#    identities mod q: the photon is the exact kernel, the two W
#    eigenvalues are equal (rho=1 ingredient), and the Z eigenvalue is
#    (g^2+g'^2) v^2/4.  Couplings g,gp are arbitrary field elements:
#    the mass-matrix STRUCTURE is a finite-field identity for ANY coupling.
# ---------------------------------------------------------------------
hr("B. FINITE-FIELD exact: photon kernel, W-degeneracy, Z mass over F_q")

def sqrt_minus1(q):
    for x in range(q):
        if (x*x) % q == (q-1) % q: return x
    return None

def weak_modq(q, gg, ggp, vv):
    iq = sqrt_minus1(q)
    assert iq is not None, f"no sqrt(-1) in F_{q}"
    inv2 = pow(2, q-2, q)
    def M(rows): return sp.Matrix(rows)
    # generators over F_q (entries are residues); use python ints mod q
    def mm(A,B):  # matrix mult mod q
        return [[ (A[r][0]*B[0][c]+A[r][1]*B[1][c]) % q for c in range(2)] for r in range(2)]
    def scal(s,A): return [[(s*A[r][c])%q for c in range(2)] for r in range(2)]
    T1=[[0,inv2],[inv2,0]]
    T2=[[0,(-iq*inv2)%q],[(iq*inv2)%q,0]]
    T3=[[inv2,0],[0,(-inv2)%q]]
    Yt=[[inv2,0],[0,inv2]]
    Xs=[scal(gg,T1),scal(gg,T2),scal(gg,T3),scal(ggp,Yt)]
    phi=[0,vv]
    def apply(A,ph): return [(A[0][0]*ph[0]+A[0][1]*ph[1])%q,(A[1][0]*ph[0]+A[1][1]*ph[1])%q]
    Xp=[apply(A,phi) for A in Xs]
    def herm(u,w):  # u^dagger w  in F_q (conjugation = i->-i; entries real here)
        # conjugate of a+ b*i represented? entries are pure residues incl. iq; do full conj
        return None
    # build M2 with explicit conjugation: represent each entry as residue (a + b*iq folded already)
    # Simplest: compute over the quadratic extension symbolically via sympy GF? Instead compute
    # the Hermitian product using the fact that conj(iq) = -iq = q-iq.
    # Xp entries are integers mod q already (iq folded in). Conjugate flips iq sign; we track via
    # recomputing with iq -> (q-iq).
    iq2 = (q-iq)%q
    T2c=[[0,(-iq2*inv2)%q],[(iq2*inv2)%q,0]]
    Xsc=[scal(gg,T1),scal(gg,T2c),scal(gg,T3),scal(ggp,Yt)]
    Xpc=[apply(A,phi) for A in Xsc]
    M2=[[ (Xpc[a][0]*Xp[b][0]+Xpc[a][1]*Xp[b][1])%q for b in range(4)] for a in range(4)]
    return sp.Matrix([[sp.Integer(M2[a][b]) for b in range(4)] for a in range(4)]), iq

for (q,gg,ggp,vv) in ((13,1,2,2),(5,1,1,2)):  # g^2+g'^2 != 0 mod q (5 over F_13, 2 over F_5): Z genuinely massive
    Mq, iq = weak_modq(q, gg=gg, ggp=ggp, vv=vv)
    Mq = Mq.applyfunc(lambda z: z % q)
    print(f"\n--- F_{q}  (g={gg}, g'={ggp}, v={vv}; i=sqrt(-1)={iq}) ---")
    print("M^2 mod q (W1,W2,W3,B):"); sp.pprint(Mq)
    # photon kernel direction (0,0,g',g)
    ph = sp.Matrix([0,0,ggp,gg])
    res = (Mq*ph).applyfunc(lambda z: z % q)
    print(f"  M^2 . (0,0,g',g)^T = M^2.(0,0,{ggp},{gg}) mod q =", res.T,
          " -> photon exact kernel:", all(x%q==0 for x in res))
    # (W3,B) block determinant == 0 mod q  (rank-1 => rho=1, exactly)
    WBq = Mq[2:4,2:4]; detWB = WBq.det() % q
    print(f"  det (W3,B) block mod q = {detWB}  == 0  <=> rho=1 (rank-1) exactly")
    # W1,W2 diagonal entries equal (custodial degeneracy)
    print(f"  M_W^2 = M^2[W1,W1]={Mq[0,0]%q} = M^2[W2,W2]={Mq[1,1]%q}:", Mq[0,0]%q==Mq[1,1]%q,
          f";  M_Z^2 = (g^2+g'^2)v^2/4 = {((gg*gg+ggp*ggp)*vv*vv*pow(4,q-2,q))%q} (!=0, massive)")

print("\n  (the mass-matrix STRUCTURE -- W-degeneracy + rank-1 (W3,B) block")
print("   + photon kernel -- is a finite-field identity for ANY couplings g,g'.)")

# ---------------------------------------------------------------------
# C. THE DRIVE PICTURE (Theorem 6.4) and the transfer-operator winding.
#    delta = diag(g, g^-1) in the split torus; Ad_delta on sl2 basis
#    H,E,F has eigenvalues 1, g^2, g^-2.  Unit eigenvalue = drive-invariant
#    = propagating zero mode = massless photon (transfer eigenvalue 1,
#    winding rate 0).  g^{+-2} = the W^+- per-chronon winding = the mass
#    (realisation B6: mass is winding rate).
# ---------------------------------------------------------------------
hr("C. DRIVE / transfer-operator winding: Ad_delta eigenvalues over F_q")
for q,gprim in ((13,2),(5,2)):
    g2 = (gprim*gprim) % q
    g2inv = pow(g2, q-2, q)
    print(f"  F_{q}, drive g={gprim}:  Ad_delta(H)=H  (eig 1, photon: winding 0, massless)")
    print(f"                         Ad_delta(E)=g^2 E = {g2} E   (W^+ winding rate)")
    print(f"                         Ad_delta(F)=g^-2 F = {g2inv} F (W^- winding rate)")
    # order of g^2 = period over which the winding mode returns = inverse mass scale
    from sympy import n_order
    try:
        per = n_order(g2, q)
        print(f"     winding period ord(g^2) in F_{q}^* = {per}  (finite => gapped, massive)")
    except Exception as e:
        print("     ord:", e)

hr("SUMMARY")
print("""CLOSED (exact, float-free, derived from the finite action):
  * the propagating mass operator = Hessian of the gauged-Higgs action S_rho
    at the drive-broken vacuum  (Theorem 4.7 quadratic form, Remark 6.5 step);
  * spectrum {0 (photon), M_W^2 x2, M_Z^2} with the photon the EXACT kernel
    = the vacuum stabiliser Q=T3+Y  (over Q and over F_13, F_5);
  * custodial rho = 1  exactly, forced by the doublet (spinor) structure;
  * M_W^2/M_Z^2 = cos^2 thetaW = 5/8  once sin^2 thetaW = 3/8 (charge trace);
  * the drive (Thm 6.4) and the Higgs-stabiliser pictures compose to the same
    spectrum; mass = transfer-operator winding rate (realisation B6).

RESIDUE (a single number):
  * the overall scale v -- the Higgs-vacuum amplitude on the non-split cycle.
    Fixes the absolute M_W; everything else (ratio, rho, photon kernel) forced.
    Tied to Omega via the same running that carries sin^2 thetaW 3/8 -> 0.231.
""")
