readback_checks.py

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

4272 bytes, as of the version from 2026-10-30 06:27 (current). Recorded here, not run.

"""Check the written intervals, analytic anchors and sibling background constants.

Run with SageMath after filling T619. NUMBERDB_API_KEY is required for drafts.
Reads website documents through numberdb.table. The repository check.stored
requires a local Django database, which this build machine does not have.
This does not change the table.
The independent radial comparison is deliberately limited to its measured
accuracy; it does not certify the hundred stored digits.
"""
import json
from pathlib import Path
import mpmath as mp
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.real_mpfr import RealField
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing


def rational(text):
    if '/' in text:return QQ(text)
    from decimal import Decimal
    num,den=Decimal(text).as_integer_ratio()
    return QQ(num)/QQ(den)

def interval(text):
    from decimal import Decimal
    if '/' in text:return rational(text),QQ(0)
    d=Decimal(text);return rational(text),QQ(10)**d.as_tuple().exponent

def internal(evidence):
    F=RealField(numberdb.bits(240));errors={}
    # An exact polynomial identity converts the original-potential equation
    # to the modified-potential equation; density-derivative terms cancel.
    P=PolynomialRing(QQ,names=('Y','s','L'));Y,s,L=P.gens();y=Y-s
    assert L-Y*(Y+1)-3*s+s*s == L-y*(y+1)-2*s*(y+2)
    mp.mp.dps=140
    for ell in (2,3):
        # The common sqrt(pi/(2x)) prefactor cancels in the spherical ratio.
        bessel=mp.besselj(ell+mp.mpf('1.5'),mp.pi)/(2*mp.besselj(ell-mp.mpf('.5'),mp.pi))
        primary=F(evidence['models']['1']['runs']['210']['values'][ell-2])
        assert abs(primary-F(str(bessel)))<F('1e-135')
    for n,row in evidence['models'].items():
        v=row['runs']['210']['values'];ys=row['runs']['210']['meta']['surface_Y']
        for ell,k,Y in zip((2,3),v,ys):
            k=F(k);Y=F(Y)
            extY=(ell-2*(ell+1)*k)/(1+2*k)
            assert abs(extY-Y)<F('1e-200')
    return {'modified_field_polynomial_identity':True,'n1_independent_bessel_140_digits':True,'exterior_matching_all_rows':True}


def check_document(document,evidence,radial,companions):
    report=internal(evidence);F=RealField(numberdb.bits(240));count=0;maxrad=F(0)
    assert list(document['Parameters'])==['n','ell']
    expected=('0','1/2','1','3/2','2','5/2','3','7/2','4','9/2')
    assert tuple(document['Numbers'])==expected
    for n,rows in document['Numbers'].items():
        assert tuple(rows)==('2','3')
        for ell,item in rows.items():
            text=item['number'] if isinstance(item,dict) else item
            val,width=interval(text);high=F(evidence['models'][n]['runs']['210']['values'][int(ell)-2])
            if n=='0':assert val==QQ(3)/(4*(int(ell)-1)) and width==0
            else:
                assert abs(F(val)-high)<F(width)
                assert F(width)/abs(high)<F('1e-98')
            # Check the exterior-match identity using the stored decimal midpoint.
            Y=F(evidence['models'][n]['runs']['210']['meta']['surface_Y'][int(ell)-2])
            from_stored=(int(ell)-2*(int(ell)+1)*F(val))/(1+2*F(val))
            assert abs(from_stored-Y)<F('1e-96')
            independent=F(str(radial['models'][n]['finer'][0][int(ell)-2]))
            err=abs(independent-F(val))/abs(F(val));maxrad=max(maxrad,err)
            assert err<F('2e-8')
            count+=1
        for tid,field in [('T613','radius'),('T614','mass')]:
            row=companions[tid]['Numbers'][n];text=row['number'] if isinstance(row,dict) else row
            v,w=interval(text);actual=F(evidence['models'][n]['runs']['210']['meta'][field])
            assert abs(F(v)-actual)<F(w)
    report.update(rows=count,stored_intervals_contain_high_precision_results=True,
                  exact_incompressible_rows=True,sibling_radius_and_mass_intervals=20,
                  independent_radial_worst_relative_error=str(maxrad))
    return report

if __name__=='__main__':
    p=Path(__file__).parent
    d=numberdb.table('T619')
    print(json.dumps(check_document(d,json.loads((p/'numerical-evidence.json').read_text()),json.loads((p/'radial-evidence.json').read_text()),{t:numberdb.table(t) for t in ('T613','T614')}),indent=2))