#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
The quark Koide amplitude excess r - sqrt2 (ledger lead).  Leptons (colourless) have r=sqrt2;
the quarks have r_d=1.55, r_u=1.76 > sqrt2.  Test that this excess is COLOUR-structural:
 (down) the Georgi-Jarlskog colour Clebsch (N_c=3) dressing the lepton spectrum into the down
        spectrum shifts the Koide value Q from 2/3 to ~0.745 ~ observed Q_d -> r_d-sqrt2 IS the
        colour (GJ) dressing, the same N_c=3 of M7;
 (up)   r_u^2 ~ 3, the cube-root (colour/cubic) diagonal, vs the lepton r^2=2, the quarter-turn
        (four-fold) diagonal.  NATIVE (framed-rational): r^2 = N(1-zeta_n), the finite-field norm
        = the INTEGER 2 (n=4) or 3 (n=3) in F_p; the sqrt2,sqrt3 = 2 sin(pi/n) are continuum
        readings, one rung up.
"""
import numpy as np, sympy as sp
PASS,FAIL="PASS","FAIL"; res=[]
def ck(t,c,d=""): res.append((t,bool(c))); print(f"[{PASS if c else FAIL}] {t}: {d}")
def Qr(m):
    m=np.array(m,float); sm=np.sqrt(m); Q=m.sum()/sm.sum()**2; return Q, np.sqrt(6*Q-2)

# data
me,mmu,mtau=0.51099895,105.6583755,1776.86
md,ms,mb=4.67,93.4,4180.0
mu,mc,mt=2.16,1270.0,172570.0
s2,s3=np.sqrt(2),np.sqrt(3)

print("="*70); print("(0) the amplitudes"); print("="*70)
Ql,rl=Qr([me,mmu,mtau]); Qd,rd=Qr([md,ms,mb]); Qu,ru=Qr([mu,mc,mt])
Qu_pole,ru_pole=Qr([mu,1670.0,mt])   # pole charm mass (T23: the scheme band)
print(f"   scheme (T23): Q_u = {Qu:.4f} in the working MSbar scheme (m_c(m_c)), {Qu_pole:.4f} with the pole charm mass; band 0.83-0.89; 5/6 = 0.8333 is the framed value")
print(f"   leptons: Q={Ql:.4f} r={rl:.4f} (=sqrt2={s2:.4f})")
print(f"   down   : Q={Qd:.4f} r={rd:.4f}   (excess r-sqrt2={rd-s2:.3f})")
print(f"   up     : Q={Qu:.4f} r={ru:.4f}   (excess r-sqrt2={ru-s2:.3f})")

print("\n"+"="*70); print("(1) DOWN: the excess is the GJ colour Clebsch (N_c=3) on the leptons")
print("="*70)
# Georgi-Jarlskog at unification: m_e=m_d/3, m_mu=3 m_s, m_tau=m_b  =>  down = leptons dressed
# by the colour Clebsch (3, 1/3, 1).  Compute the Koide value of the dressed lepton spectrum.
Qd_gj,rd_gj=Qr([3*me, mmu/3, mtau])
print(f"   GJ-dressed leptons (3 m_e, m_mu/3, m_tau): Q={Qd_gj:.4f}, r={rd_gj:.4f}")
print(f"   observed down                             : Q={Qd:.4f}, r={rd:.4f}")
ck("1.gj_colour", abs(Qd_gj-Qd)<0.03,
   f"colour (GJ, N_c=3) dressing shifts Q 2/3 -> {Qd_gj:.3f} ~ observed Q_d={Qd:.3f} "
   f"({100*abs(Qd_gj-Qd)/Qd:.0f}%): r_d-sqrt2 is the colour dressing")

print("\n"+"="*70); print("(2) UP: r_u ~ sqrt3 = 2 sin(pi/3), the cube-root/colour diagonal")
print("="*70)
print(f"   r_u = {ru:.4f}  vs  sqrt3 = 2 sin(pi/3) = {s3:.4f}  ({100*abs(ru-s3)/s3:.1f}%)")
# robustness to the (uncertain) light up-quark mass
rng=np.random.default_rng(0); rus=[]
for _ in range(5000):
    mU=rng.uniform(1.5,2.7); mC=rng.uniform(1180,1350); mT=rng.uniform(171000,174000)
    rus.append(Qr([mU,mC,mT])[1])
print(f"   over m_u,m_c,m_t ranges: r_u = {np.median(rus):.3f} "
      f"[{np.percentile(rus,16):.3f},{np.percentile(rus,84):.3f}] -- robust (m_u barely enters)")
ck("2.up_sqrt3", abs(ru-s3)/s3<0.03, f"r_u={ru:.3f} ~ sqrt3={s3:.3f}: the cube-root colour diagonal")

print("\n"+"="*70)
print("(3) the diagonal over FRAMED RATIONALS: r^2 = N(1-zeta_n) = 2, 3 [integers in F_p]")
print("="*70)
# the NATIVE statement (1-algebra): the amplitude r^2 is the finite-field norm N(1-zeta_n) of
# (1 - finite n-th root): quarter-turn (i, i^2=-1) -> 2 (Q=2/3); cube root (omega^2+omega+1=0)
# -> 3 (Q=5/6).  Integers in F_p.  The 'sqrt2, sqrt3, 2 sin(pi/n)' are continuum readings.
for p in [17,53,89]:
    i_=next(x for x in range(1,p) if (x*x+1)%p==0)
    N1mi=((1-i_)*(1+i_))%p
    nq=next(m for m in range(2,p) if pow(m,(p-1)//2,p)==p-1)
    mul=lambda A,B:((A[0]*B[0]+A[1]*B[1]*nq)%p,(A[0]*B[1]+A[1]*B[0])%p)
    def order(A):
        o=1;X=A
        while X!=(1,0):X=mul(X,A);o+=1
        return o
    om=next((u,v) for u in range(p) for v in range(1,p) if order((u,v))==3)
    a1=((1-om[0])%p,(-om[1])%p); a2=((1-om[0])%p,(om[1])%p)
    N1mw=mul(a1,a2)
    print(f"   p={p}: N(1-i)={N1mi}=2 (four-fold, r^2=2, Q=2/3, sqrt2); "
          f"N(1-omega)={N1mw[0]}=3 (three-fold/colour, r^2=3, Q=5/6, sqrt3)")
    res.append((f"3.framed_p{p}", N1mi==2%p and N1mw==(3%p,0)))
ck("3.pattern", all(c for t,c in res if t.startswith("3.framed")),
   "r^2 = N(1-zeta_n) = 2 (four-fold/colourless), 3 (three-fold/colour): integers in F_p")

print("\n"+"="*70)
print("CONCLUSION: the quark amplitude excess r-sqrt2 is COLOUR, not a free parameter.")
print("  Down: the GJ colour Clebsch (N_c=3, = M7) dresses the lepton spectrum, Q 2/3->0.745.")
print("  Up:   r_u ~ sqrt3 = 2 sin(pi/3), the cube-root colour diagonal (cf lepton sqrt2=2sin(pi/4)).")
print("  So r folds into the colour structure; r-sqrt2 is forced (down) / a sqrt3 lead (up).")
n=sum(1 for _,c in res if c); print(f"\nchecks: {n}/{len(res)} pass")
for t,c in res:
    if not c: print("  FAILED",t)
