#!/usr/bin/env python3
"""Galpol 2I field: L(3,1/2), L(3',1/2), L(4,1/2) with EXACT Z[phi] coefficients (icosa-zero1115/an_exact.py) at BITS,
GP-side evaluation (cypari method calls default to 64 bits). FLOAT numerics (high precision), not interval. Presupposes Artin holomorphy.
usage: galpol_exact.py NAME BITS NMAX"""
import sys, json, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent/'icosa-zero1115'))
import an_exact
from cypari2 import Pari
name, bits, NM = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
EO = {'three': [-1,1]*11, 'threep': [-1,1]*11, 'four': [0,1]*11}
E2 = {'three': [], 'threep': [], 'four': [1]*24}
dim = {'three':3,'threep':3,'four':4}[name]; N = 2**8*11**2*23**2
t0 = time.time()
pairs = an_exact.build_pairs(name, {2: E2[name], 11: EO[name], 23: EO[name]}, NM)
print('built', round(time.time()-t0), flush=True)
pari = Pari(); pari.allocatemem(3*10**9); pari.set_real_precision_bits(bits)
pari('w=(1+sqrt(5))/2;')
AV = pari([pari(f'{x}+({y})*w') if y else pari(x) for (x, y) in pairs])
pari('(v)->AVG=v')(AV)
vga = str([0]*dim)
out = dict(name=name, bits=bits, nan=NM, N=N)
for W in (1, -1): out[f'checkfeq_W{W}'] = int(pari(f'lfuncheckfeq(lfuncreate([AVG,0,{vga},1,{N},{W}]))'))
pari(f'LG=lfuncreate([AVG,0,{vga},1,{N},1]);')
out['cost'] = str(pari('lfuncost(LG,[0,0])'))
out['L_half'] = str(pari('lfun(LG,1/2)')); out['L2'] = str(pari('lfun(LG,2)'))
out['orderzero'] = int(pari('lfunorderzero(LG)'))
out['secs'] = round(time.time()-t0)
print(json.dumps(out), flush=True)
json.dump(out, open(f'galpol-exact-{name}-b{bits}-n{NM}.json', 'w'), indent=1)
