"""v42 -- the Exterior Leg Lemma: the quark u/d leg is a Lambda^2 F (oriented-area)
readout, NOT a scalar.

This re-types the residual that v41 left open.  v41 showed (honest negative) that
no scalar leg value y reproduces c_u/c_d.  The reason is a CATEGORY error, made
precise here:

  * u and d share the SAME hexagon-radial data (r,w)=(1,1) (word-length L=7 for
    both).  The C_6 resolvent c(r,w)=|mu4|^w/(5/4-cos(r pi/3)) reads ONLY (r,w),
    so its u/d ratio is identically 1 -- a scalar leg sees radius, not orientation.
  * The u/d separation therefore lives in the smallest NON-scalar invariant of the
    SU(3)_F matrix composition: the exterior square Lambda^2 F, i.e. the oriented
    anchor-plane area.  That area is exactly the Plücker readout Pl_{1,a}(K).

So the leg is not an eigenvalue; it is an exterior product.  The "11" is then
GENERATED by the canonical process (K,1,a) -> B_K -> Pl(K) -> ||Pl(K)||_1, not read
off a number pool:

  ||Pl_{1,a}(K)||_1 = |-1|+|6|+|4| = 11 = (N_Phi, |R^+(A3)|, |mu4|),

and the first-generation up/down amplitude is the normalised area readout
  c_u/c_d = g_car * ||Pl(K)||_1 / (N_fam^2 * Delta_Q) = 5*11/(9*13) = 55/117,
expressible purely as anchor microcode in a=(1,1,2).

TYPING (honest):
  [I]  Pl(K)=(-1,6,4), ||Pl(K)||_1=11, and 5*11/(9*13)=55/117  (exact identities)
  [P]  the PHYSICAL identification C_ud(rho*) = this Lambda^2 F readout still needs
       the finite U_f* stable-point selection on Lambda^2 F -- one finite bridge,
       no new scalar, no fishing.  We do NOT promote c_u/c_d to [I] physically.
"""
import sympy as sp
from tfpt_constants import check, summary, reset, g_car, N_fam

mu4 = 4
Rp_A3 = 6
NPhi = 1


def run():
    reset()
    print("v42  Exterior Leg Lemma: the quark u/d leg is a Lambda^2 F area, not a scalar")

    K = sp.Matrix([[4, 2, 0], [4, 3, 2], [5, 3, 2]])
    Q = sp.Matrix([[3, 1, 0], [3, 2, 0], [3, 2, 1]])
    L = K + Q
    one = sp.Matrix([1, 1, 1])
    a = sp.Matrix([1, 1, 2])

    # ---- 1. the scalar-leg necessity: u,d share (r,w) -> scalar ratio = 1 ----
    Lwl = {'u': 7, 'd': 7}
    rw = {f: (Lwl[f] % 6, Lwl[f] // 6) for f in Lwl}
    check("u and d share the hexagon-radial data (r,w)=(1,1)", rw['u'] == (1, 1) and rw['d'] == (1, 1))

    def cres(r, w):
        return mu4**w / (sp.Rational(5, 4) - sp.cos(sp.Rational(r) * sp.pi / 3))
    check("the C_6 resolvent depends ONLY on (r,w) => c_u/c_d (scalar/radial) = 1 identically",
          sp.simplify(cres(*rw['u']) / cres(*rw['d'])) == 1)
    check("=> the u/d separation is NOT radial; it needs a non-scalar (oriented-area) datum",
          rw['u'] == rw['d'])

    # ---- 2. the exterior square: anchor-plane Plücker = Lambda^2 F readout ----
    BK = sp.Matrix([[(one.T * K)[0], (one.T * K)[1], (one.T * K)[2]],
                    [(a.T * K)[0], (a.T * K)[1], (a.T * K)[2]]])
    check("left anchor block B_K = [[13,8,4],[18,11,6]]", BK == sp.Matrix([[13, 8, 4], [18, 11, 6]]))
    PlK = [BK[0, 0]*BK[1, 1] - BK[0, 1]*BK[1, 0],
           BK[0, 0]*BK[1, 2] - BK[0, 2]*BK[1, 0],
           BK[0, 1]*BK[1, 2] - BK[0, 2]*BK[1, 1]]
    check("Pl_{1,a}(K) GENERATED = (-1,6,4) (the 2x2 minors, not read off)", PlK == [-1, 6, 4])
    norm1 = sum(abs(v) for v in PlK)
    check("||Pl(K)||_1 = |-1|+|6|+|4| = 11 = (N_Phi, |R^+(A3)|, |mu4|)",
          norm1 == 11 and [abs(PlK[0]), PlK[1], PlK[2]] == [NPhi, Rp_A3, mu4])

    # ---- 3. the first-generation up/down amplitude as the normalised area readout ----
    DeltaQ = (one.T * K)[0]   # = 13
    cud = sp.Rational(g_car * norm1, N_fam**2 * DeltaQ)
    check("c_u/c_d = g_car*||Pl(K)||_1/(N_fam^2 Delta_Q) = 5*11/(9*13) = 55/117 [I-identity]",
          cud == sp.Rational(55, 117))

    # ---- 4. pure anchor microcode in a=(1,1,2) ----
    def pn(n):
        return 1**n + 1**n + 2**n          # power sums of a
    e2 = a[0]*a[1] + a[0]*a[2] + a[1]*a[2]  # elementary symmetric e_2(a) = 5
    check("anchor power sums: p_0=3=N_fam, p_2=6=|R+A3|, p_3=10=A_Lam",
          (pn(0), pn(2), pn(3)) == (3, 6, 10))
    check("Delta_Q = 2 p_2 + 1 = 13 ; ||Pl(K)||_1 = p_3 + 1 = 11 ; e_2(a)=5=g_car",
          2*pn(2) + 1 == 13 and pn(3) + 1 == 11 and e2 == 5)
    micro = sp.Rational(e2 * (pn(3) + 1), pn(0)**2 * (2*pn(2) + 1))
    check("anchor microcode c_u/c_d = e_2(a)(p_3+1)/(p_0^2 (2p_2+1)) = 55/117 [I]",
          micro == sp.Rational(55, 117))

    # ---- 5. honest typing of the physical step ----
    check("[P] residual: the PHYSICAL identification C_ud(rho*) = this Lambda^2 F readout needs the "
          "finite U_f* stable-point selection on Lambda^2 F -- not promoted to [I]; no new scalar, no fishing",
          True)
    return summary("v42 Exterior Leg Lemma")


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