#!/usr/bin/env python3
"""Parts (a) and (b): 2T quaternion Frobenius checks from pari-data.json (pure python + numpy).
Method A = actual quaternion Frobenius (idealfrobenius + explicit iso Gal -> 2T) and the 2T character table;
Method B = PARI lfunartin Dirichlet coefficients a_n (independent of the quaternion isomorphism)."""
import json, math, cmath, itertools
import numpy as np
D = json.load(open('pari-data.json'))
Y = cmath.exp(2j*math.pi/3)
def ev(s): return complex(eval(s.replace('^', '**'), {'y': Y}))
def qm(a, b):
    a0,a1,a2,a3 = a; b0,b1,b2,b3 = b
    return np.array([a0*b0-a1*b1-a2*b2-a3*b3, a0*b1+a1*b0+a2*b3-a3*b2, a0*b2-a1*b3+a2*b0+a3*b1, a0*b3+a1*b2-a2*b1+a3*b0])
def qconj(a): return np.array([a[0], -a[1], -a[2], -a[3]])
def qpow(a, k):
    r = np.array([1.,0,0,0])
    for _ in range(k): r = qm(r, a)
    return r
def key(q): return str(tuple(int(round(2*x)) for x in q))
T2 = [np.array(v)/2 for v in itertools.product([1,-1],repeat=4)] + [s*np.eye(4)[t] for t in range(4) for s in (1,-1)]
def primes_upto(n):
    s = bytearray([1])*(n+1); s[0]=s[1]=0
    for i in range(2, int(n**.5)+1):
        if s[i]: s[i*i::i] = bytearray(len(s[i*i::i]))
    return [i for i in range(n+1) if s[i]]
R = {}
# ---------- (b0) choice-free linear statistics: lambda(h g h^-1) = lambda(g) for all g,h in 2T ----------
rows = [qm(qm(h, g), qconj(h)) - g for g in T2 for h in T2]
sv = np.linalg.svd(np.array(rows), compute_uv=False)
_, _, Vt = np.linalg.svd(np.array(rows))
R['choice_free_linear'] = dict(singular_values=[float(x) for x in sv[:4]], nullspace=[float(x) for x in Vt[-1]],
    statement='nullspace of {h g h^-1 - g} is 1-dim, spanned by (1,0,0,0): the only conjugation-invariant linear functional is Re = trd/2')
print('choice-free linear functionals: singular values', np.round(sv[:4], 12), 'null vector', np.round(Vt[-1], 12))
for lab, F in D.items():
    out = {}
    cq = {k: v for k, v in F['class_of_quat'].items()}           # quaternion key (x2 ints) -> class index
    tab = [[ev(x) for x in row] for row in F['chartable']]       # tab[class][char]
    chars = F['chars']; NMAX = len(chars[0]['an'])
    def cls(q): return cq[key(q)]
    def chi(c, q): return tab[cls(q)][c]
    dims = [ch['dim'] for ch in chars]
    real2 = [c for c in range(7) if dims[c] == 2 and all(abs(tab[r][c].imag) < 1e-12 for r in range(7))]
    assert len(real2) == 1; RHO = real2[0]
    PSI3 = dims.index(3)
    g6 = np.array([.5,.5,.5,.5]); nvec = np.array([0,1,1,1])/math.sqrt(3); u4 = np.array([0,1.,0,0])
    OM = [c for c in range(7) if dims[c] == 1 and abs(chi(c, g6) - Y) < 1e-12][0]
    OMB = [c for c in range(7) if dims[c] == 1 and abs(chi(c, g6) - Y.conjugate()) < 1e-12][0]
    RW = [c for c in range(7) if dims[c] == 2 and c != RHO and all(abs(tab[r][c] - tab[r][RHO]*tab[r][OM]) < 1e-12 for r in range(7))][0]
    RWB = [c for c in range(7) if dims[c] == 2 and c not in (RHO, RW)][0]
    TRIV = [c for c in range(7) if dims[c] == 1 and c not in (OM, OMB)][0]
    names = {RHO: 'rho', RW: 'rho⊗ω', RWB: 'rho⊗ω̄', OM: 'ω', OMB: 'ω̄', PSI3: 'psi3', TRIV: '1'}
    out['characters'] = {names[c]: dict(pari_col=chars[c]['col'], conductor=chars[c]['conductor'], Vga=chars[c]['Vga'],
                         rootno=chars[c].get('rootno'), ordzero=chars[c].get('ordzero')) for c in range(7)}
    # check rho is the restriction of H^x: chi_rho(q) = 2 Re q on all 24 elements; FS indicator
    out['rho_is_2Re'] = max(abs(chi(RHO, q) - 2*q[0]) for q in T2)
    out['FS_indicator'] = {names[c]: round((sum(chi(c, qm(q, q)) for q in T2)/24).real, 12) for c in range(7)}
    # ---------- (a) Frobenius quaternions ----------
    an = {c: [complex(*x) for x in chars[c]['an']] for c in range(7)}
    def Lam(c):  # Lambda_c(n)/log n via a(n) log n = sum_{d|n} Lambda(d) a(n/d), only prime powers kept
        a = an[c]; L = [0j]*(NMAX+1)
        for n in range(2, NMAX+1):
            s = a[n-1]*math.log(n)
            d = 2
            # sum over prime powers d|n, d<n
            s -= sum(L[d]*a[n//d-1] for d in pp_div[n] if d < n)
            L[n] = s
        return L
    P = primes_upto(NMAX); ram = set(F['ramified'])
    pp = {}
    for p_ in P:
        q = p_; k = 1
        while q <= NMAX: pp[q] = (p_, k); q *= p_; k += 1
    pp_div = [[] for _ in range(NMAX+1)]
    for d in pp:
        for m in range(d, NMAX+1, d): pp_div[m].append(d)
    LAM = {c: Lam(c) for c in (RHO, RW, RWB, OM, OMB, PSI3, TRIV)}
    frob = {int(p_): v for p_, v in F['frob'].items()}
    nfull = 0; maxRe = 0; maxReB = 0; maxavg = 0; minspread = 9; classcover = True; aB = 0; nchecks = 0
    flips = []
    for p_, v in frob.items():
        qs = [np.array(x['q'])/2 for x in v['frobs']]
        assert len({x['cls'] for x in v['frobs']}) == 1
        assert all(cls(q) == v['frobs'][0]['cls'] for q in qs)
        q0 = qs[0]
        for c in range(7):  # method B: PARI a_p equals character of the quaternion's class
            aB = max(aB, abs(an[c][p_-1] - chi(c, q0)))
        for k in range(1, 13):
            maxRe = max(maxRe, abs(qpow(q0, k)[0] - 0.5*chi(RHO, qpow(q0, k)).real))
            if p_**k <= NMAX:
                maxReB = max(maxReB, abs(2*qpow(q0, k)[0] - (LAM[RHO][p_**k]/math.log(p_)).real)); nchecks += 1
        if len(qs) == v['nprimes'] and len(qs) > 1:
            nfull += 1
            csize = sum(1 for g in T2 if cls(g) == cls(q0))
            distinct = {key(q) for q in qs}
            classcover &= (len(distinct) == csize)
            for k in range(1, 7):
                pk = [qpow(q, k) for q in qs]
                avg = sum(pk)/len(pk)
                maxavg = max(maxavg, float(np.linalg.norm(avg[1:])), abs(avg[0] - 0.5*chi(RHO, pk[0]).real))
            if csize > 1:
                spread = max(np.linalg.norm(a[1:]-b[1:]) for a in qs for b in qs)
                minspread = min(minspread, spread)
            if p_ in (2, 3, 5, 7, 11, 13, 17, 19, 23, 29) or (len(flips) < 12 and csize > 1):
                flips.append(dict(p=p_, f=v['f'], nprimes=v['nprimes'], class_size=csize,
                                  frobs=[key(q) for q in qs][:8], chi_rho=chi(RHO, q0).real))
    out['part_a'] = dict(primes_checked=len(frob), primes_with_all_primes_above=nfull,
        max_abs_a_p_minus_character=aB, max_Re_qk_minus_half_chi_table=maxRe, max_2Re_qk_minus_LambdaB=maxReB,
        n_prime_power_checks_B=nchecks, every_class_member_occurs=bool(classcover),
        max_norm_class_average_pure_part_k1to6=maxavg, min_spread_pure_part_noncentral=float(minspread), examples=flips[:14])
    # ---------- (b) conventions and decomposition ----------
    clsets = {}
    for g in T2: clsets.setdefault(cls(g), []).append(g)
    sizes = {c: len(v) for c, v in clsets.items()}
    noncentral = [c for c in clsets if sizes[c] > 1]
    def coeffs(f):  # f: class -> R^3 ; a_pi = (1/24) sum |C| f(C) conj(chi_pi(C))
        return {names[c]: sum(sizes[C]*np.array(f[C])*np.conj(tab[C][c]) for C in clsets)/24 for c in range(7)}
    stats = dict(n=0, rho_zero=0, faithful_zero=0, min_faithful_norm=9, max_rho_norm=0)
    for choice in itertools.product(*[clsets[c] for c in noncentral]):
        f = {c: np.zeros(3) for c in clsets}
        for c, g in zip(noncentral, choice): f[c] = g[1:]
        a = coeffs(f)
        nr = np.linalg.norm(a['rho']); nf = math.sqrt(nr**2 + np.linalg.norm(a['rho⊗ω'])**2 + np.linalg.norm(a['rho⊗ω̄'])**2)
        stats['n'] += 1; stats['rho_zero'] += nr < 1e-12; stats['faithful_zero'] += nf < 1e-12
        stats['min_faithful_norm'] = min(stats['min_faithful_norm'], nf); stats['max_rho_norm'] = max(stats['max_rho_norm'], nr)
    out['all_conventions'] = {k: (float(v) if not isinstance(v, (int, np.integer)) else int(v)) for k, v in stats.items()}
    # aligned convention: reps of the 6 non-4a classes = powers of g6; 4a rep = u4 = i
    conv = {}
    for m in range(6): conv[cls(qpow(g6, m))] = qpow(g6, m)
    c4 = cls(u4); conv[c4] = u4
    assert len(conv) == 7
    f = {c: conv[c][1:] for c in clsets}
    a = coeffs(f)
    out['aligned_coeffs'] = {k: [[round(z.real, 12), round(z.imag, 12)] for z in v] for k, v in a.items()}
    # exact formula check on classes: Im(c_C^k) = Im chi_{rho⊗ω}(C^k)·n + eps_k 1_{4a}(C)[k odd]·u
    maxform = 0
    for C in clsets:
        for k in range(1, 13):
            ck = qpow(conv[C], k); Ck = cls(ck)
            pred = (tab[Ck][RW].imag)*nvec[1:]
            if C == c4 and k % 2 == 1: pred = pred + (1 if k % 4 == 1 else -1)*u4[1:]
            maxform = max(maxform, float(np.linalg.norm(ck[1:] - pred)))
            # Re part, and the slice-complex value psi(c^k) = 1/2 chi_rho + 1/2(chi_rw - chi_rwb) on non-4a classes
    ind4 = [0.25*(tab[C][TRIV] + tab[C][OM] + tab[C][OMB] - tab[C][PSI3]) for C in range(7)]
    out['indicator_4a_formula_err'] = max(abs(ind4[C] - (1 if C == c4 else 0)) for C in range(7))
    out['aligned_formula_max_err_all_k'] = maxform
    # prime sums: method A (quaternions) vs method B (PARI a_n / Lambda)
    res = []
    for T in (1.0, 1.5, 2.0, 3.0):
        g = lambda x: T/(2*math.sqrt(math.pi))*math.exp(-T*T*x*x/4)
        SA_n = SA_u = SB_n = SB_u = SA_re = SB_re = 0.0; SA_k1u = SB_k1u = 0.0; corr = 0.0
        for nn, (p_, k) in pp.items():
            if p_ in ram: continue
            w = -2*math.log(p_)*g(k*math.log(p_))/math.sqrt(nn)
            C = frob[p_]['frobs'][0]['cls']
            ck = qpow(conv[C], k)
            # coordinates of Im ck in basis (n, u4): solve
            M = np.array([nvec[1:], u4[1:]]).T
            al, be = np.linalg.lstsq(M, ck[1:], rcond=None)[0]
            assert np.linalg.norm(M @ [al, be] - ck[1:]) < 1e-12
            SA_n += w*al; SA_u += w*be; SA_re += w*ck[0]
            SB_n += w*(LAM[RW][nn]/math.log(p_)).imag
            SB_re += w*0.5*(LAM[RHO][nn]/math.log(p_)).real
            if k == 1:
                SA_k1u += w*be
                SB_k1u += w*0.25*(1 + an[OM][p_-1] + an[OMB][p_-1] - an[PSI3][p_-1]).real
            if k % 2 == 1 and C == c4:
                SB_u += w*(1 if k % 4 == 1 else -1)
        res.append(dict(T=T, n_dir_A=SA_n, n_dir_B_Im_rho_omega=SB_n, diff_n=abs(SA_n-SB_n),
                        u_dir_A_k1=SA_k1u, u_dir_B_k1_quarter_1_w_wb_minus_psi3=SB_k1u, diff_u_k1=abs(SA_k1u-SB_k1u),
                        u_dir_A_all=SA_u, u_dir_B_all_with_eps_k=SB_u, re_A=SA_re, re_B_half_rho=SB_re, diff_re=abs(SA_re-SB_re)))
    out['prime_sums'] = res
    # empirical rho-weight of the aligned pure part (Chebotarev average, X=NMAX)
    num = np.zeros(3); den = 0
    for p_ in P:
        if p_ in ram: continue
        C = frob[p_]['frobs'][0]['cls']
        num += conv[C][1:]*tab[C][RHO].real*math.log(p_); den += math.log(p_)
    out['empirical_rho_weight_aligned'] = (num/den/2).tolist()
    # Chebotarev class frequencies
    freq = {}
    for p_ in P:
        if p_ in ram: continue
        C = frob[p_]['frobs'][0]['cls']; freq[C] = freq.get(C, 0) + 1
    out['class_freq'] = {str(C): [freq.get(C, 0), sizes[C]/24*sum(freq.values())] for C in clsets}
    out['class_names'] = {str(C): dict(size=sizes[C], order=int(min(k for k in range(1, 13) if np.allclose(qpow(clsets[C][0], k), [1,0,0,0]))),
                          chi_rho=tab[C][RHO].real, rep_aligned=key(conv[C])) for C in clsets}
    R[lab] = out
    print('=====', lab); print(json.dumps({k: v for k, v in out.items() if k not in ('part_a',)}, indent=1, default=str)[:6000])
    print('part_a', json.dumps({k: v for k, v in out['part_a'].items() if k != 'examples'}, default=str))
    for e in out['part_a']['examples'][:8]: print('  ', e)
json.dump(R, open('results-ab.json', 'w'), indent=1, default=str)
