"""Outside-family arithmetic control for T651, row 16.

Run with SageMath; uses PARI elliptic-curve L-functions, not period integrals.
Samart--Tao v3 Table 4 factors L(C_t,s) at t=729+405sqrt(3) into the
weight-two newforms 36.2.a.a and 144.2.a.a. They correspond to y^2=x^3+1
and y^2=x^3-1. The relation is D=2187*L(E36,2)*L(E144,2)/pi^4.
The Tao--Guo determinant uses m_3=3N and is nine times this D.
"""
import json
from pathlib import Path
import numberdb.sage as numberdb
from sage.libs.pari import pari
import mpmath as mp
pari.set_real_precision(190)
with mp.workdps(200):
    assert abs(mp.mpf(str(pari.lfun(1,2,precision=700)))-mp.pi**2/6)<mp.mpf("1e-180")
results=[]
for working in (140,190):
    pari.set_real_precision(working)
    E36=pari.ellinit([0,0,0,0,1]);E144=pari.ellinit([0,0,0,0,-1])
    assert int(E36.ellglobalred()[0])==36 and int(E144.ellglobalred()[0])==144
    assert [int(E36.ellap(q)) for q in [7,13,19,31,37,43]]==[-4,2,8,-4,-10,8]
    assert [int(E144.ellap(q)) for q in [7,13,19,31,37,43]]==[4,2,-8,4,-10,-8]
    a=pari.lfun(E36,2,precision=numberdb.bits(working));b=pari.lfun(E144,2,precision=numberdb.bits(working))
    with mp.workdps(working):
        v=2187*mp.mpf(str(a))*mp.mpf(str(b))/mp.pi**4
        results.append(mp.nstr(v,working-5))
row=next(json.loads(s) for s in Path(__file__).with_name('computed.jsonl').read_text().splitlines() if json.loads(s)['row']==16)
with mp.workdps(210):
    agreement=-mp.log10(abs((mp.mpf(results[1])-mp.mpf(row['high']))/mp.mpf(row['high'])))
    assert agreement>120
    # Known nonzero Dirichlet control for PARI lfun before reporting a result.
    zeta2=pari.lfun(1,2,precision=700)
    assert abs(mp.mpf(str(zeta2))-mp.pi**2/6)<mp.mpf('1e-180')
print(json.dumps({'row':16,'formula':'2187 L(y^2=x^3+1,2)L(y^2=x^3-1,2)/pi^4','working_digits':[140,190],'values':results,'agreement_digits':str(agreement),'pari_zeta2_control':True},indent=2))
