"""Independent theta-integral/Li-coordinate check of the screw moments.

Run with SageMath, Arb/FLINT and numberdb; no production generator is imported.
  sage -python independent_check.py
  sage -python independent_check.py --stored stored-document.json
Checkpoint/result paths default to this file's directory; --resume uses an
existing theta-checkpoint.json, whose stored definition/version is checked.
Suzuki 2206.03682v4 (8.3); DLMF 25.5.13-14. See numerical-evidence.md
for the independently derived uniform theta and integration tail bounds.
The common dependencies are completed-xi theory and Arb/FLINT arithmetic.
"""
import sys,json,pathlib,time,argparse
from math import factorial,comb
from fractions import Fraction
import numberdb.sage as numberdb
from sage.rings.complex_arb import ComplexBallField
from sage.rings.real_arb import RealBallField
from sage.rings.rational_field import QQ

VERSION='theta-at-one-li-transform-v1'
BITS=896;M=14;LIMIT=192;NMAX=50
C=ComplexBallField(BITS);R=RealBallField(BITS)

def narrow(x,bits=400):
    return bool(x.is_finite() and not x.contains_zero() and x.accuracy() >= bits)

def compare(a,b,minimum=300):
    if not narrow(a,minimum) or not narrow(b,minimum):return False
    return bool((a-b).contains_zero())

def theta_coefficients(progress):
    pi=C.pi()
    # |log(x)/2|^k/k! <= sqrt(x) for x>=1, all k>=0.
    # Together with |sqrt(x)+(-1)^k| <= 2sqrt(x), this gives an
    # integrand bound 2*sum exp(-pi*m*m*x), uniform in every k.
    tail_x=2*(-R.pi()*LIMIT).exp()/(R.pi()*(1-(-3*R.pi()*LIMIT).exp()))
    a=M+1
    tail_m=2*(-R.pi()*a*a).exp()/(R.pi()*a*a*(1-(-R.pi()*(2*a+1)).exp()))
    tail=(tail_x+tail_m).upper()
    coeffs=[]
    for k in range(NMAX+1):
        def f(x,analytic):
            if analytic and x.real().lower() <=0:return C('nan')
            z=x.log();theta=sum(((-pi*m*m*x).exp() for m in range(1,M+1)),C(0))
            return (x.sqrt()+(-1)**k)*(z/2)**k*theta/(x*factorial(k))
        z=C.integral(f,1,LIMIT,abs_tol=R(2)**(-760),rel_tol=R(2)**(-760),eval_limit=100000,depth_limit=30)
        if not (z.real().is_finite() and z.imag().is_finite() and z.imag().contains_zero()):
            raise ArithmeticError('Theta quadrature failed at order '+str(k))
        value=z.real()+R(0,rad=tail)
        if not value.is_finite():raise ArithmeticError('Bad theta coefficient')
        coeffs.append(value)
        progress(coeffs,tail_x,tail_m)
        print('theta coefficient',k,'accuracy',value.accuracy(),flush=True)
    return coeffs,tail_x,tail_m

def li_from_theta(A):
    # xi(1+u)=1/2+(u+u^2)*A(u)/2.
    q=[R(QQ(1)/2)]
    for j in range(1,NMAX+2):
        q.append((A[j-1]+(A[j-2] if j>=2 else 0))/2)
    # Substitute u=w/(1-w) using binomial coefficients, no FLINT log series.
    b=[q[0]]+[sum((comb(n-1,j-1)*q[j] for j in range(1,n+1)),R(0)) for n in range(1,NMAX+2)]
    lam=[R(0)]
    for n in range(1,NMAX+2):
        lam.append((n*b[n]-sum((lam[k]*b[n-k] for k in range(1,n)),R(0)))/b[0])
    return lam

def moments_from_li(lam):
    values=[]
    for n in range(NMAX+1):
        v=R(0)
        for j in range(1,n+2):
            c=sum(k*2**(k-1)*comb(n-k+2,j) for k in range(1,n-j+3))
            v+=c*(-1)**(j+1)*lam[j]
        values.append(factorial(n)*v)
    return values

def stored_bounds(s):
    # Exact rational endpoints of the interval claimed by NumberDB spelling.
    s=str(s)
    if '+/-' in s:
        c,r=s.split('+/-');c=Fraction(c.strip());r=Fraction(r.strip())
        return QQ(c-r),QQ(c+r)
    if s.startswith('['):
        lo,hi=s[1:-1].split(',');return QQ(Fraction(lo)),QQ(Fraction(hi))
    f=Fraction(s)
    if '.' not in s and 'e' not in s.lower():return QQ(f),QQ(f)
    mant,_,exponent=s.lower().partition('e');ex=int(exponent or 0)
    places=len(mant.split('.')[1]) if '.' in mant else 0
    ulp=Fraction(10)**(ex-places)
    return QQ(f-ulp),QQ(f+ulp)

def run():
    parser=argparse.ArgumentParser();parser.add_argument('--stored',type=pathlib.Path);parser.add_argument('--resume',action='store_true');a=parser.parse_args()
    work=pathlib.Path(__file__).parent;cp=work/'theta-checkpoint.json';start=time.time()
    def progress(values,tx,tm):
        data={'version':VERSION,'working_bits':BITS,'M':M,'limit':LIMIT,'coefficients':[str(v) for v in values],'tail_x':str(tx),'tail_m':str(tm)}
        # stdout is also a durable checkpoint transport for agents/sage.sh.
        print('THETA_CHECKPOINT_JSON '+json.dumps(data),flush=True)
        try:cp.write_text(json.dumps(data,indent=2))
        except OSError:pass
    if a.resume:
        p=json.loads(cp.read_text());assert p['version']==VERSION and p['working_bits']==BITS and p['M']==M and p['limit']==LIMIT and len(p['coefficients'])==51
        A=[R(v) for v in p['coefficients']];tx=R(p['tail_x']);tm=R(p['tail_m'])
    else:A,tx,tm=theta_coefficients(progress)
    lam=li_from_theta(A);values=moments_from_li(lam)
    # A nonzero closed-form control checks quadrature and factor conventions.
    pi=C.pi();quad=C.integral(lambda x,_:(-pi*x).exp(),1,LIMIT,abs_tol=R(2)**(-760),rel_tol=R(2)**(-760)).real()
    exact=((-R.pi()).exp()-(-R.pi()*LIMIT).exp())/R.pi()
    known=compare(quad,exact,650)
    # xi'/xi(1)=1+(gamma-log(4*pi))/2 is independent of this theta route.
    mu0=1+(R.euler_constant()-(4*R.pi()).log())/2
    known=bool(known and compare(values[0],mu0,600))
    wrong=[-values[0],4*values[0],values[1],values[2]/2,R('nan'),R('0 +/- 1e100')]
    rejected=[not compare(values[0] if i<3 or i>3 else values[2],v,300) for i,v in enumerate(wrong)]
    result={'method':VERSION,'working_bits':BITS,'checked_entries':0,'total_entries':51,'failures':[],'known_answer_control':known,'negative_control_rejected':all(rejected),'negative_controls':dict(zip(['wrong sign','wrong factor','index shift','factorial normalization','NaN','wide ball'],rejected)),'minimum_relative_bits':min(int(v.accuracy()) for v in values),'moments':[str(v) for v in values],'li_coefficients':[str(v) for v in lam[1:]],'tail_x':str(tx),'tail_m':str(tm),'seconds':time.time()-start}
    if a.stored:
        doc=json.loads(a.stored.read_text());numbers=doc['Numbers']
        if isinstance(numbers,dict):
            rows=[{'params':{'n':str(k)},'number':v['number'] if isinstance(v,dict) else v} for k,v in numbers.items()]
        else:rows=numbers
        assert [int(row['params']['n']) for row in rows]==list(range(51))
        comparisons=[]
        for row in rows:
            n=int(row['params']['n']);lo,hi=stored_bounds(row['number']);v=values[n]
            ok=bool(narrow(v,400) and v>R(lo) and v<R(hi) and v.rad_as_ball()<R(hi-lo)/1000)
            comparisons.append({'n':n,'stored_contains_independent_ball':ok,'independent_relative_bits':int(v.accuracy())})
            if not ok:result['failures'].append('Stored moment '+str(n))
        result['checked_entries']=51;result['comparisons']=comparisons
    if not known or not all(rejected):result['failures'].append('Control failed')
    if not all(narrow(v,400) for v in values):result['failures'].append('Insufficient moment precision')
    print('CHECK_RESULTS_JSON '+json.dumps(result),flush=True)
    try:(work/'check-results.json').write_text(json.dumps(result,indent=2))
    except OSError:pass
    if result['failures']:raise SystemExit('Independent checks failed')
if __name__=='__main__':run()
