bounded_series_check.py

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

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

"""Alternate rigorous series check for T855, not an independent-actor verdict.

Usage: sage -python bounded_series_check.py offline-results.json
Prints SERIES_RESULT_JSON for durable capture. This code does not import the
generator. It replaces Arb modular_lambda and hypergeometric with explicit
positive theta and Gauss series and proved geometric tail bounds; elementary
ball arithmetic and gamma evaluation still share FLINT/Arb with the generator.
The mpmath quadrature checker provides a separate library and integral check.
"""
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=500


def positive_tail(bound):
    assert bound.is_finite() and bound>0
    return R(I(0,bound.upper()))


def alternative(r):
    r=QQ(r)
    if r==1:return QQ(1)/QQ(2),None
    s=max(r,1/r)
    q=(-R.pi()*R(s)).exp()
    assert q>0 and q<1
    n=THETA_TERMS
    # Sum n=0,...,L-1. Subsequent term ratios <=q^(2L+2).
    a=sum((q**(j*(j+1)) for j in range(n)),R(0))
    a+=positive_tail(q**(n*(n+1))/(1-q**(2*n+2)))
    th2=2*q**(QQ(1)/4)*a
    # Sum n=1,...,L-1. Subsequent term ratios <=q^(2L+1).
    b=sum((q**(j*j) for j in range(1,n)),R(0))
    b+=positive_tail(q**(n*n)/(1-q**(2*n+1)))
    th3=1+2*b
    x=(th2/th3)**4
    assert x>0 and x<1
    term=R(1);total=R(0)
    for j in range(HYP_TERMS):
        total+=term
        # This exact positive coefficient is <1 for every j>=0.
        coefficient=(QQ(j)+QQ(1)/3)*(QQ(j)+QQ(2)/3)/((QQ(j)+QQ(4)/3)*(j+1))
        assert 0<coefficient<1
        term*=coefficient*x
    # term is now t_N. For all later terms t_(n+1)/t_n < x<1.
    total+=positive_tail(term/(1-x))
    a=QQ(1)/3
    result=R(QQ(2)/3).gamma()/(R(QQ(4)/3).gamma()*R(a).gamma())*x**a*total
    if r<1:result=1-result
    assert result.is_finite() and result>0 and result<1 and result.accuracy()>450
    return result,x


def endpoints(value):
    if type(value).__name__=='Rational':return Fraction(str(value)),Fraction(str(value))
    it=I(value)
    return Fraction(str(it.lower().exact_rational())),Fraction(str(it.upper().exact_rational()))


def stored_interval(text):
    if '/' in text or ('.' not in text and 'e' not in text.lower()):
        return Fraction(text),Fraction(text)
    d=Decimal(text);e=d.as_tuple().exponent
    ulp=Fraction(10**e) if e>=0 else Fraction(1,10**(-e))
    center=Fraction(d)
    return center-ulp,center+ulp


def main():
    source=Path(sys.argv[1]);data=json.loads(source.read_text());rows=data['entries']
    failures=[];out=[]
    for row in rows:
        r=row['params']['r'];v,x=alternative(r);lo,hi=endpoints(v)
        slo,shi=stored_interval(row['number'])
        same=lo<=Fraction(row['upper']) and Fraction(row['lower'])<=hi
        contained=slo<=lo<=hi<=shi
        if not same or not contained:failures.append(r)
        out.append({'r':r,'alternate_lower':str(lo),'alternate_upper':str(hi),
                    'overlaps_generator_ball':same,'stored_contains_alternate':contained,
                    'accuracy_bits':int(v.accuracy()) if type(v).__name__!='Rational' else None})
    a,_=alternative('2');lo,hi=endpoints(a)
    # Known algebraic cross-ratio; check theta branch against it.
    _,x=alternative('2');target=17-12*R(2).sqrt()
    lambda_control=(x-target).contains_zero() and (x-target).is_finite()
    negative=(hi+Fraction(1,1000)<Fraction(data['entries'][0]['number']))
    # An intentionally shifted probability cannot overlap the tight r=2 ball.
    shifted_lo=lo+Fraction(1,1000);shifted_hi=hi+Fraction(1,1000)
    wrong_rejected=not (lo<=shifted_hi and shifted_lo<=hi)
    if not lambda_control or not wrong_rejected:failures.append('controls')
    from sage import env
    version_paths=Path(env.SAGE_LOCAL)/'var/lib/sage/installed'
    packages=[p.name for p in version_paths.iterdir() if p.name.startswith(('flint-','gmp-','mpfr-','mpc-','mpmath-'))] if version_paths.exists() else []
    res={'table':'T855','actor_role':'builder alternate rigorous series, not independent scientific review',
         'program_sha256':hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),
         'input_sha256':hashlib.sha256(source.read_bytes()).hexdigest(),
         'skill_manifest':'../20261128T123700Z-build-skills/manifest.json',
         'skill_bundle_sha256':'f20b09e3db5fd443660c0c6b29391ee369b7c710a43e22d8abb971ec3f54dd14',
         'checked_entries':len(rows),'total_entries':len(rows),'failures':failures,
         'known_answer_control':bool(lambda_control),'negative_control_rejected':bool(wrong_rejected),
         'working_bits':PREC,'theta_terms':THETA_TERMS,'hypergeometric_terms':HYP_TERMS,
         'minimum_accuracy_bits':min(t['accuracy_bits'] for t in out if t['accuracy_bits'] is not None),
         'installed_package_versions':packages,'entries':out}
    print('SERIES_RESULT_JSON:'+json.dumps(res))
    if failures:raise SystemExit(1)


if __name__=='__main__':main()