checks.py

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

6150 bytes, as of the version from 2026-11-18 01:45 (current). Recorded here, not run.

"""Independent finite-part corroboration for T741, not a review verdict.

sage -python checks.py
Reads values.json, or stored-values.json if present, beside this file.

Tanh–sinh quadrature evaluates the original balanced integral, subtracts the
specified polynomial finite part, and adds its asymptotic tail. It never uses
the generating convergent sum rule. The two runs use (decimal precision,T,N)
=(150,256,120) and (190,384,160). Bessel factors use 1800-bit Arb midpoints;
this is heuristic quadrature and heuristic asymptotic truncation, not a
certified enclosure. The table's proven bounds come from generate.py instead.
Point Bessel evaluations are cached across the family; all ten finite parts
and both normalizations are checked against their entire stored intervals.
RESULTS JSON is printed for durable capture. Known-answer positive controls
and deliberately wrong shifts/signs check the checker.
"""
import sys,json,math,time
from pathlib import Path
from functools import lru_cache
from decimal import Decimal
import mpmath as mp
import numberdb.sage as numberdb
from sage.rings.complex_arb import ComplexBallField
from sage.rings.rational_field import QQ

C=ComplexBallField(1800)

@lru_cache(None)
def factors(key,prec):
    sign,mantissa,exponent,_=key
    z=C(-mantissa if sign else mantissa)*C(2)**exponent
    I=z.bessel_I(0);K=z.bessel_K(0)
    assert all(v.real().is_finite() and v.imag().is_finite() for v in [I,K])
    assert K.real().accuracy()>prec+16,('Bessel input enclosure too wide',key,K.real().accuracy(),prec)
    return mp.mpf(str(I.real().center())),mp.mpf(str(K.real().center()))

@lru_cache(None)
def coefficients(m,N):
    a=[QQ(1)];g=[QQ(1)];p=2*m
    for k in range(1,N+1):a.append(a[-1]*QQ((2*k-1)**3)/QQ(8*k))
    for n in range(1,N+1):g.append(sum(QQ((p+1)*k-n)*a[k]*g[n-k] for k in range(1,n+1))/QQ(n))
    assert all(type(x) is type(QQ(1)) for x in g)
    return g

def as_mp(q):return mp.mpf(int(q.numerator()))/mp.mpf(int(q.denominator()))

@lru_cache(None)
def defining_finite_part(n,j,working,T,N):
    start=time.time();m=n//4;q=2*j
    with mp.workdps(working):
        def f(t):
            if not t:return mp.mpf(0)
            I,K=factors(t._mpf_,mp.mp.prec)
            return (I*K)**(2*m)*t**q
        value=mp.quad(f,[0,1,4,16,64,128,T],method='tanh-sinh')
        c=coefficients(m,N)
        subtract=mp.fsum(as_mp(c[l])*mp.mpf(T)**(q-2*m-2*l+1)/mp.mpf(q-2*m-2*l+1) for l in range(j-m+1))/mp.mpf(2)**(2*m)
        tail=[as_mp(c[l])*mp.mpf(T)**(q-2*m-2*l+1)/mp.mpf(2*l+2*m-q-1)/mp.mpf(2)**(2*m) for l in range(j-m+1,N+1)]
        result=value-subtract+mp.fsum(tail)
        assert mp.isfinite(result) and result!=0
        return +result,+tail[-1],time.time()-start

def known_controls():
    out=[]
    with mp.workdps(150):
        for q,target in [(1,mp.mpf(1)/2),(3,mp.mpf(1)/3)]:
            def f(t):
                if not t:return mp.mpf(0)
                _,K=factors(t._mpf_,mp.mp.prec);return t**q*K**2
            value=mp.quad(f,[0,1,4,16,64,128,256],method='tanh-sinh')
            error=abs(value-target)
            assert mp.isfinite(value) and error<mp.mpf('1e-125')
            out.append({'q':q,'expected':mp.nstr(target,30),'absolute_error':mp.nstr(error,12)})
            print('CONTROL',json.dumps(out[-1]),flush=True)
    return out

def decimal_parts(text):
    d=Decimal(str(text));a,b=d.as_integer_ratio();return mp.mpf(a)/b,mp.mpf(10)**d.as_tuple().exponent

def main():
    here=Path(__file__).resolve().parent;p=here/'stored-values.json'
    if not p.exists():p=here/'values.json'
    records=json.loads(p.read_text())['records'];controls=known_controls();out=[];failures=[]
    for r in records:
        p=r['params'];n=int(p['n']);j=int(p['j']);m=n//4;norm=p['normalization']
        first,last,seconds=defining_finite_part(n,j,150,256,120)
        second,last2,seconds2=defining_finite_part(n,j,190,384,160)
        with mp.workdps(220):
            if norm=='period':
                factor=(-1)**m*mp.mpf(2)**(n-2*j)*mp.pi**(2*m)
                first*=factor;second*=factor;last*=factor;last2*=factor
            center,ulp=decimal_parts(r['number']);relative=abs(first-second)/abs(second)
            guard=2*abs(first-second)+abs(second)*mp.mpf('1e-125')
            assert mp.isfinite(first) and mp.isfinite(second) and relative<mp.mpf('1e-110')
            # Guard is heuristic and explicit; it is not called a certified ball.
            passed=abs(center-first)+guard<ulp and abs(center-second)+guard<ulp
            wrong=abs(center*mp.mpf('1.01')-second)+guard<ulp
            wrong_sign=abs(-center-second)+guard<ulp
            rejected=not (wrong or wrong_sign)
            if not passed:failures.append({'params':p,'reason':'finite-part discrepancy exceeds stored interval'})
            if not rejected:failures.append({'params':p,'reason':'negative control accepted'})
            result={'params':p,'passed':bool(passed),'relative_cutoff_and_precision_difference':mp.nstr(relative,12),'error_in_stored_ulps':mp.nstr(abs(center-second)/ulp,12),'heuristic_guard_in_stored_ulps':mp.nstr(guard/ulp,12),'last_tail_term_first':mp.nstr(last,12),'last_tail_term_second':mp.nstr(last2,12),'negative_controls_rejected':rejected,'seconds':seconds+seconds2}
        out.append(result);print('COMPARISON',json.dumps(result),flush=True)
    result={'checked_entries':len(records),'total_entries':20,'failures':failures,'known_answer_control':True,'negative_control_rejected':all(x['negative_controls_rejected'] for x in out),'controls':controls,'comparisons':out,'method':'Defining balanced integral via tanh-sinh at 150/190 working decimal digits, (T,N)=(256,120)/(384,160); 1800-bit Arb midpoint Bessel evaluations','independence':'No convergent sum rule; different integrand and quadrature, with shared Arb Bessel implementation','limitations':'Quadrature and asymptotic remainders in this independent corroboration are heuristic; generator enclosures are separately rigorous','versions':{'mpmath':mp.__version__,'numberdb':numberdb.__version__}}
    print('RESULTS',json.dumps(result),flush=True);assert not failures

if __name__=='__main__':main()