#!/usr/bin/env python3
"""Which root sl2 pairs are Lorentzian (exchanged by the real structure) in real forms of E8.
Exact integer root bookkeeping.  Doubled coordinates: roots have norm^2 8, (a,b)=ip/4."""
import itertools
from collections import Counter
roots=[]
for i,j in itertools.combinations(range(8),2):
    for a in (2,-2):
        for b in (2,-2):
            r=[0]*8; r[i]=a; r[j]=b; roots.append(tuple(r))
for s in itertools.product((1,-1),repeat=8):
    if s.count(-1)%2==0: roots.append(s)
R=set(roots); assert len(R)==240
ip=lambda u,v: sum(x*y for x,y in zip(u,v))//4
refl=lambda v,c: tuple(x-ip(v,c)*y for x,y in zip(v,c))
neg=lambda v: tuple(-x for x in v)

def analyse(name, noncompact):
    """noncompact: predicate on roots for the compact-Cartan Vogan data."""
    nc=[r for r in roots if noncompact(r)]
    print("\n== %s: %d compact roots, %d noncompact; dim k = %d" % (name, 240-len(nc), len(nc), 8+(240-len(nc))))
    c=nc[0]
    tallies=Counter(); d8=Counter(); npairs=Counter(); whole=Counter()
    for cp in nc:
        if ip(c,cp)!=0: continue
        sig=lambda v: refl(refl(v,c),cp)           # Cartan involution on the new Cartan: s_c s_c'
        kind={}
        for a in roots:
            sa=sig(a)
            kind[a]='real' if sa==neg(a) else ('imc' if sa==a and not noncompact(a) else ('imn' if sa==a else 'cx'))
        cnt=Counter(kind.values())
        dimk=6+cnt['imc']+cnt['cx']//2+cnt['real']//2
        whole[dimk]+=1
        lor=set()
        for a in roots:
            if kind[a]!='cx': continue
            abar=neg(sig(a))
            if ip(a,abar)==0: lor.add(frozenset((a,neg(a),abar,neg(abar))))
        npairs[len(lor)]+=1
        for L in lor:
            a=next(iter(L)); abar=neg(sig(a))
            cen=[r for r in roots if ip(r,a)==0 and ip(r,abar)==0]
            assert len(cen)==60
            k=Counter(kind[r] for r in cen)
            tallies[5+k['imc']+k['cx']//2+k['real']//2]+=1      # D6 Cartan: 5 compact + 1 split direction
            mix=[r for r in roots if abs(ip(r,a))==1 and abs(ip(r,abar))==1]
            assert len(mix)==48
            k8=Counter(kind[r] for r in cen+mix+list(L))
            d8[6+k8['imc']+k8['cx']//2+k8['real']//2]+=1
    print("  orthogonal noncompact partners c' of c:", sum(npairs.values()))
    print("  dim k of the whole algebra recomputed on the new Cartan:", dict(whole))
    print("  Lorentz subalgebras (conjugate root pairs a, abar orthogonal) per Cartan:", dict(npairs))
    print("  dim of maximal compact of the D6 centralizer -> count:", dict(tallies))
    print("  dim of maximal compact of the enclosing D8 -> count:", dict(d8))

ref={ (p,12-p): p*(p-1)//2+(12-p)*(11-p)//2 for p in range(0,7)}
print("reference: dim k of so(p,12-p):", ref, " so*(12): 36")
print("reference: dim k of so(4,12): 72, so(8,8): 56, so*(16): 64, so(16): 120")
theta=(2,2,0,0,0,0,0,0)
analyse("E8(-24), Cartan involution exp(i pi ad theta^vee)", lambda r: abs(ip(r,theta))==1)
analyse("E8(8), compact part so(16)", lambda r: 0 not in r)

# ---- Paper 22's four A2 legs against an eight-root frame
A2=[((-2,-2,0,0,0,0,0,0),(2,0,-2,0,0,0,0,0)),((0,0,0,-2,-2,0,0,0),(0,0,0,2,0,-2,0,0)),
    ((0,0,0,0,0,0,-2,-2),(-1,1,-1,-1,1,-1,1,1)),((0,0,0,0,0,0,-2,2),(-1,1,-1,1,-1,1,1,-1))]
assert all(a in R and b in R and ip(a,b)==-1 for a,b in A2)
legs=[]
for a,b in A2:
    s=tuple(x+y for x,y in zip(a,b)); six={a,b,s,neg(a),neg(b),neg(s)}; assert six<=R; legs.append(six)
assert all(ip(u,v)==0 for i in range(4) for j in range(i) for u in legs[i] for v in legs[j])
print("\n== Paper 22 legs: four mutually orthogonal A2's verified, 24 roots")
res=Counter()
for q in itertools.product(*[[r for r in L if r>neg(r)] for L in legs]):   # one root (up to sign) from each leg
    comp=[r for r in roots if all(ip(r,x)==0 for x in q)]
    # largest orthogonal set inside the complement
    best=0
    def grow(sel,cands):
        global best
        best=max(best,len(sel))
        for i,r in enumerate(cands):
            grow(sel+[r],[s for s in cands[i+1:] if ip(r,s)==0])
    grow([],[r for r in comp if r>neg(r)])
    res[(len(comp),best)]+=1
print("  one root from each leg (81 choices): (roots orthogonal to all four, further orthogonal roots available) -> count:", dict(res))

# ---- which Lorentz pairs have centralizer so(1,11) and which so(9,3), in E8(-24)
nonc=lambda r: abs(ip(r,theta))==1
c=[r for r in roots if nonc(r)][0]
pat=Counter()
for cp in roots:
    if not nonc(cp) or ip(c,cp)!=0: continue
    sig=lambda v: refl(refl(v,c),cp)
    seen=set()
    for a in roots:
        sa=sig(a)
        if sa in (a,neg(a)): continue
        abar=neg(sa)
        if ip(a,abar)!=0: continue
        key=frozenset((a,neg(a),abar,neg(abar)))
        if key in seen: continue
        seen.add(key)
        cen=[r for r in roots if ip(r,a)==0 and ip(r,abar)==0]
        k=Counter(('real' if sig(r)==neg(r) else ('imc' if sig(r)==r and not nonc(r) else ('imn' if sig(r)==r else 'cx'))) for r in cen)
        dk=5+k['imc']+k['cx']//2+k['real']//2
        g=tuple(sorted((abs(ip(a,theta)),abs(ip(abar,theta)))))
        pat[(dk,g,ip(cp,theta))]+=1
print("\n== E8(-24): (dim k of centralizer, |theta-grades| of the pair, grade of c') -> count")
for k_,v in sorted(pat.items()): print("  ",k_,v)
