bounded_series_check.py

back to table · edit · history · where entries came from · files · download

4806 bytes, as of the version from 2026-12-02 03:42 (current). Recorded here, not run.

"""Alternate rigorous theta and positive-series check for T857.

sage -python bounded_series_check.py offline-results.json
No generator import. Replaces modular_lambda and hypergeometric library
calls by explicit positive series; wide means use N=C+positive residual series with geometric tail bounds at 512 bits.
Elementary balls and gamma still share FLINT; check_values.py uses an alternate
library and integral formula. This is builder evidence, not a critic's verdict.
"""
import sys,json,hashlib
from pathlib import Path
from decimal import Decimal
from fractions import Fraction
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.real_arb import RealBallField
from sage.rings.real_mpfi import RealIntervalField

PREC=512
R=RealBallField(PREC);I=RealIntervalField(PREC)
THETA_TERMS=20
HYP_TERMS=550


def tail(bound):
    if not bound.is_finite() or not bound>0:raise ArithmeticError('Invalid positive tail')
    return R(I(0,bound.upper()))


def alternative(r):
    r=QQ(r);assert r>0
    s=r if r>=1 else 1/r
    q=(-R.pi()*R(s)).exp();assert 0<q<1
    n=THETA_TERMS
    s2=sum((q**(j*(j+1)) for j in range(n)),R(0))
    s2+=tail(q**(n*(n+1))/(1-q**(2*n+2)))
    th2=2*q**(QQ(1)/4)*s2
    s3=sum((q**(j*j) for j in range(1,n)),R(0))
    s3+=tail(q**(n*n)/(1-q**(2*n+1)))
    th3=1+2*s3;y=(th2/th3)**4;assert 0<y<1
    t2=R(1);h2=R(0)
    for j in range(HYP_TERMS):
        h2+=t2
        c2=(QQ(j)+QQ(1)/3)*(QQ(j)+QQ(2)/3)/((QQ(j)+QQ(4)/3)*(j+1))
        assert 0<c2<1
        t2*=c2*y
    h2+=tail(t2/(1-y))
    c=R(QQ(2)/3).gamma()/R(QQ(1)/3).gamma()**2*3*y**(QQ(1)/3)*h2
    # f(y)=y*3F2= sum_{n>=1} a_{n-1}*y^n,
    # a_j=(4/3)_j/((j+1)*(5/3)_j).
    # For n>=2, 0<a_{n-1}<1/n, so -log(1-y)-f(y)
    # has positive coefficients (1/n-a_{n-1}). Bound its omitted
    # tail above by y^(M+1)/((M+1)*(1-y)).
    coeff=QQ(1);power=y;f=R(0);residual=R(0)
    for n in range(1,HYP_TERMS+1):
        f+=coeff*power
        delta=QQ(1)/n-coeff
        assert delta>=0 and (n==1 or delta>0)
        residual+=delta*power
        j=n-1
        ratio=(QQ(j)+1)*(QQ(j)+QQ(4)/3)/((QQ(j)+2)*(QQ(j)+QQ(5)/3))
        assert 0<ratio<1
        coeff*=ratio;power*=y
    f+=tail(coeff*power/(1-y))
    residual+=tail(power/((HYP_TERMS+1)*(1-y)))
    k=R(3).sqrt()/(4*R.pi())
    v=c+k*residual if r>=1 else R(QQ(1)/2)-k*f-k*y.log()
    cardy=c if r>=1 else 1-c
    assert v.is_finite() and v>cardy>0 and cardy<1 and v.accuracy()>470
    return v,y


def bounds(v):
    b=I(v)
    return Fraction(str(b.lower().exact_rational())),Fraction(str(b.upper().exact_rational()))


def stored(text):
    d=Decimal(text);u=Fraction(10)**d.as_tuple().exponent;c=Fraction(d)
    return c-u,c+u


def narrow_overlap(a,b):
    if not a.is_finite() or not b.is_finite():return False
    if a.accuracy()<330 or b.accuracy()<330:return False
    return a.overlaps(b)


def main():
    source=Path(sys.argv[1]) if len(sys.argv)>1 else Path(__file__).with_name('offline-results.json')
    data=json.loads(source.read_text());out=[];failures=[]
    square=R(QQ(3)/8)+R(3).sqrt()/(8*R.pi())*(3*R(3).log()-2*R(2).log())
    av,_=alternative('1');_,x2=alternative('2')
    known=narrow_overlap(av,square) and narrow_overlap(x2,17-12*R(2).sqrt())
    bad=not narrow_overlap(av,square+R(QQ(1)/1000))
    bad=bad and not narrow_overlap(av,R('nan')) and not narrow_overlap(av,R(I(0,1)))
    if not known or not bad:failures.append('controls')
    for row in data['entries']:
        r=row['params']['r'];v,x=alternative(r);lo,hi=bounds(v);sl,sh=stored(row['number'])
        overlap=lo<=Fraction(row['upper']) and Fraction(row['lower'])<=hi
        containment=sl<=lo<=hi<=sh
        if not overlap or not containment:failures.append(r)
        out.append({'r':r,'lower':str(lo),'upper':str(hi),'accuracy_bits':int(v.accuracy()),
                    'overlaps_generator_ball':bool(overlap),'stored_contains_alternate':bool(containment)})
    result={'table':'T857','stage':'builder explicit-series check; not an independent verdict',
      'program_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),
      'input_sha256':hashlib.sha256(source.read_bytes()).hexdigest(),
      'skill_manifest':'../20261128T140524Z-build-skills/manifest.json',
      'skill_bundle_sha256':'f20b09e3db5fd443660c0c6b29391ee369b7c710a43e22d8abb971ec3f54dd14',
      'checked_entries':len(out),'total_entries':len(data['entries']),'failures':failures,
      'known_answer_control':bool(known),'negative_control_rejected':bool(bad),
      'working_bits':PREC,'theta_terms':THETA_TERMS,'hypergeometric_terms':HYP_TERMS,
      'minimum_accuracy_bits':min(t['accuracy_bits'] for t in out),'entries':out}
    print(json.dumps(result,indent=2))
    if failures:raise SystemExit(1)


if __name__=='__main__':main()