import json, glob, random, sys
sys.path.insert(0, '.')
import batch
from ogf_check import E_of
from sympy import sympify, symbols, lambdify
import mpmath
mpmath.mp.dps = 60
x = symbols('x')
random.seed(1)
rows = []
for f in sorted(glob.glob('out/*.json')):
    d = json.loads(open(f).read().strip().splitlines()[-1])
    if 'error' in d or not d.get('terms_match_OEIS'):
        continue
    terms, off, conjs = batch.oeis(d['id'])
    for txt, r in zip(conjs, d['results']):
        if not r.get('E_is_polynomial'):
            rows.append((d['id'], 'undecided')); continue
        Ps, _ = batch.parse_rec(txt)
        Eraw = E_of(batch.GF[d['id']], Ps)
        poly = sympify(r['E'])
        g = lambdify(x, Eraw - poly, 'mpmath')
        worst = max(abs(g(mpmath.mpf(random.randint(1, 999)) / 100000)) for _ in range(6))
        rows.append((d['id'], r['verdict'], r['E'], float(worst)))
ok = all(len(t) < 4 or t[3] < 1e-40 for t in rows)
for t in rows: print(t)
print('ALL random-point checks < 1e-40:', ok, ' rows:', len(rows))
