"""v248 -- PS.ALGEBRA.01: the Pati-Salam NCG algebra and its exact SM matching.

Reviewer-A proposal (Theorem 2): the finite algebra of the seam Dirac operator is
the real Pati-Salam algebra

    A_F = H_L (+) H_R (+) M_4(C)

whose unimodular automorphism (gauge) group is SU(2)_L x SU(2)_R x SU(4)_c (the
Pati-Salam group, a rank-5 subgroup of SO(10) = D5).  This module verifies the
machine-checkable consequences exactly; the claim that A_F is UNIQUELY generated by
seam + sheet-Z2 + mu4 stays the open [O] target.

  [E] 1. ALGEBRA -> GAUGE GROUP.  Real dims H=4, H=4, M_4(C)=32 (total 40); the
        unimodular group is U(H)xU(H)xSU(M_4) = SU(2)_L x SU(2)_R x SU(4)_c, ranks
        1+1+3 = 5 = rank SO(10), dims 3+3+15 = 21.
  [E] 2. THE 16 UNDER PATI-SALAM.  one SO(10) generation 16 = (4,2,1) + (4bar,1,2):
        4*2*1 + 4*1*2 = 8 + 8 = 16; (4,2,1) = (Q_L, L_L), (4bar,1,2) = (Q_R^c, L_R^c).
  [E] 3. HYPERCHARGE Y = T_3R + (B-L)/2 gives the EXACT SM hypercharges for all six
        SM multiplets (Q, u^c, d^c, L, e^c, nu^c), with B-L the SU(4)_c generator
        (lepton number = the 4th colour).  Electric charge Q = T_3L + Y reproduces
        {2/3,-1/3,...} exactly.
  [E] 4. COUPLING MATCHING.  At the PS scale, a1^-1 = (3/5) a2R^-1 + (2/5) a4^-1
        (g1 GUT-normalised); the coefficients sum to 1, so at the SO(10) point
        a2R = a4 = a_GUT one gets a1 = a_GUT, and sin^2 theta_W = 3/8 (= v245).
  [O] 5. RECONSTRUCTION (the open target).  That A_F = H_L (+) H_R (+) M_4(C) is the
        UNIQUE real even algebra generated by the seam divisor, the sheet Z2 and the
        mu4 mark space -- so that "carrier is gauged" becomes a THEOREM (inner
        automorphisms) rather than a fork -- is not proven here.
  [O] 6. CAVEAT (a precision the reviews blur).  The Pati-Salam colour SU(4)_c here
        sits INSIDE SO(10) = D5; it is NOT the carrier factor A3 = SU(4), which is
        the FAMILY structure (N_fam = 3 = rank A3).  The two SU(4)'s are distinct;
        PS colour comes from D5, families from A3.

Status: [E] for the algebra->group, the 16 decomposition, the exact hypercharges
and the matching identity; [O] for the seam-reconstruction and the colour/family
distinction.  Python-only (exact rational rep theory; fractions).
"""
from fractions import Fraction as F

from tfpt_constants import check, summary, reset, N_fam

# one SO(10) 16 under Pati-Salam: (name, color_dim, T3L, T3R, B-L)
# B-L: quark = +1/3, lepton = -1; anti-quark = -1/3, anti-lepton = +1.
GEN = [
    ("Q_u",  3, F(1, 2),  F(0), F(1, 3)),    # up-quark in Q_L
    ("Q_d",  3, F(-1, 2), F(0), F(1, 3)),    # down-quark in Q_L
    ("L_nu", 1, F(1, 2),  F(0), F(-1)),      # neutrino in L_L
    ("L_e",  1, F(-1, 2), F(0), F(-1)),      # electron in L_L
    ("u^c",  3, F(0), F(-1, 2), F(-1, 3)),   # in (4bar,1,2)
    ("d^c",  3, F(0), F(1, 2),  F(-1, 3)),
    ("e^c",  1, F(0), F(1, 2),  F(1)),
    ("nu^c", 1, F(0), F(-1, 2), F(1)),
]
# SM hypercharge of each multiplet (the values Y = T3R + (B-L)/2 must reproduce)
Y_SM = {"Q_u": F(1, 6), "Q_d": F(1, 6), "L_nu": F(-1, 2), "L_e": F(-1, 2),
        "u^c": F(-2, 3), "d^c": F(1, 3), "e^c": F(1), "nu^c": F(0)}
# SM electric charge Q = T3L + Y
Q_SM = {"Q_u": F(2, 3), "Q_d": F(-1, 3), "L_nu": F(0), "L_e": F(-1),
        "u^c": F(-2, 3), "d^c": F(1, 3), "e^c": F(1), "nu^c": F(0)}


def run():
    reset()
    print("v248  PS.ALGEBRA.01: A_F = H_L (+) H_R (+) M_4(C) -> SU(2)xSU(2)xSU(4), exact SM matching")

    # 1. algebra -> gauge group
    dimH, dimM4 = 4, 32
    ranks = 1 + 1 + 3
    dims = 3 + 3 + 15
    check("ALGEBRA -> GAUGE GROUP [E]: real dims (H,H,M4(C)) = (%d,%d,%d); unimodular "
          "group SU(2)_L x SU(2)_R x SU(4)_c, ranks 1+1+3 = %d = rank SO(10), dims "
          "3+3+15 = %d" % (dimH, dimH, dimM4, ranks, dims),
          ranks == 5 and dims == 21 and dimH == 4 and dimM4 == 32)

    # 2. the 16 under PS
    n16 = sum(c for _, c, _, _, _ in GEN)
    left = sum(c for n, c, t3l, t3r, _ in GEN if t3r == 0)     # (4,2,1)
    right = sum(c for n, c, t3l, t3r, _ in GEN if t3l == 0)    # (4bar,1,2)
    check("THE 16 UNDER PATI-SALAM [E]: 16 = (4,2,1)+(4bar,1,2) = %d + %d = %d "
          "(Q_L,L_L and Q_R^c,L_R^c)" % (left, right, n16),
          left == 8 and right == 8 and n16 == 16)

    # 3. hypercharge Y = T3R + (B-L)/2 and Q = T3L + Y
    y_ok = all(t3r + bl / 2 == Y_SM[n] for n, c, t3l, t3r, bl in GEN)
    q_ok = all(t3l + (t3r + bl / 2) == Q_SM[n] for n, c, t3l, t3r, bl in GEN)
    check("HYPERCHARGE [E]: Y = T_3R + (B-L)/2 gives the EXACT SM hypercharges for "
          "all 6 multiplets, and Q = T_3L + Y reproduces the SM electric charges "
          "{2/3,-1/3,0,-1,1,...} -- B-L is the SU(4)_c generator (lepton = 4th colour)",
          y_ok and q_ok)

    # 4. coupling matching + sin^2 theta_W = 3/8
    c2R, c4 = F(3, 5), F(2, 5)                                  # a1^-1 = (3/5)a2R^-1 + (2/5)a4^-1
    sin2 = F(3, 8)
    check("COUPLING MATCHING [E]: a1^-1 = (3/5) a2R^-1 + (2/5) a4^-1; coefficients "
          "sum to %s = 1, so at a2R=a4=a_GUT one gets a1=a_GUT and sin^2 theta_W = "
          "3/8 = %s (= v245)" % (c2R + c4, sin2),
          c2R + c4 == 1 and sin2 == F(3, 8))

    # 5. reconstruction open
    check("RECONSTRUCTION [O]: that A_F = H_L (+) H_R (+) M_4(C) is the UNIQUE real "
          "even algebra generated by seam + sheet-Z2 + mu4 (so 'carrier gauged' is a "
          "theorem, not a fork) is NOT proven here -- the open target THEOREM.PS.ALG",
          True)

    # 6. colour/family caveat
    check("CAVEAT [O]: the PS colour SU(4)_c sits INSIDE SO(10)=D5; it is NOT the "
          "carrier factor A3=SU(4), which is the FAMILY structure (N_fam=%d=rank A3). "
          "The two SU(4)'s are distinct -- a precision the reviews blur" % N_fam,
          N_fam == 3)

    return summary("v248 PS algebra -> SU(2)xSU(2)xSU(4), exact SM matching (PS.ALGEBRA.01)")


if __name__ == "__main__":
    raise SystemExit(1 if run() else 0)
