readback_checks.py

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

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

"""Read stored T616 entries and test their intervals against checked identities.

Run: sage -python readback_checks.py, with NUMBERDB_API_KEY set for draft reads.
The default reads the live database. READBACK_DOCUMENT may select a downloaded
API document for an offline repeat of exactly the same interval tests.
Requires numerical-evidence.json beside this script. All selected rows, exact
anchors, companion radius/mass/concentration relationships and the integrated
moment identity are checked. It writes no entries.
"""
import os,sys,json
from pathlib import Path
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.real_mpfr import RealField
from sage.rings.real_arb import RealBallField
from sage.rings.real_mpfi import RealIntervalField


def scalar(row):
    return str(row['number'] if isinstance(row,dict) else row)

def interval(text,F):
    if '/' in text or ('.' not in text and 'e' not in text.lower()):
        return F(QQ(text))
    # Decimal spelling denotes +/- one unit in the last printed place.
    if 'e' in text.lower():
        mantissa,exponent=text.lower().split('e');exponent=int(exponent)
    else:mantissa=text;exponent=0
    places=len(mantissa.split('.')[1]) if '.' in mantissa else 0
    unit=F(10)**(exponent-places)
    return F(text)+F(-1,1)*unit

def contains(outer,inner):
    return outer.lower() <= inner.lower() and outer.upper() >= inner.upper()

def overlaps(a,b):
    return a.lower() <= b.upper() and b.lower() <= a.upper()

def main():
    if os.environ.get('NUMBERDB_KEY_FROM_STDIN')=='1':
        token=sys.stdin.read().strip()
        if '=' in token and token.split('=',1)[0].replace('_','').isalnum():token=token.split('=',1)[1].strip().strip('\"\'')
        os.environ['NUMBERDB_API_KEY']=token
    root=Path(__file__).parent
    if os.environ.get('READBACK_DOCUMENT'):
        document=json.loads(Path(os.environ['READBACK_DOCUMENT']).read_text())
    else:document=numberdb.table('T616')
    rows=document['Numbers'];indices=['0','1/2','1','3/2','2','5/2','3','7/2','4','9/2']
    assert list(rows)==indices
    assert scalar(rows['0'])=='2/5'
    evidence=json.loads((root/'numerical-evidence.json').read_text())['values']
    companions={tid:numberdb.table(tid)['Numbers'] for tid in ['T613','T614','T615']}
    F=RealIntervalField(850);B=RealBallField(800);out={}
    for n in indices:
        v=interval(scalar(rows[n]),F);e=evidence[n]['210']
        x=F(e['radius']);q=F(e['mass']);J=F(e['density_moment']);H=F(e['enthalpy_moment'])
        direct=2*J/(3*q*x*x);parts=F(2)/3-4*H/(q*x*x)
        if n=='0':
            assert contains(v,F(QQ(2)/5))
            assert abs(direct-F(QQ(2)/5)).upper()<F('1e-180').lower()
            assert abs(parts-F(QQ(2)/5)).upper()<F('1e-180').lower()
        else:
            assert contains(v,direct),(n,'direct moment outside stored interval')
            assert contains(v,parts),(n,'parts identity outside stored interval')
        if n=='1':
            anchor=F(B(2)/3-4/B.pi()**2)
            assert contains(v,anchor)
        xr=interval(scalar(companions['T613'][n]),F)
        mu=interval(scalar(companions['T614'][n]),F)
        concentration=interval(scalar(companions['T615'][n]),F)
        assert contains(xr,x) and contains(mu,q)
        # Use overlap, since the companion values have independent rounding.
        c= xr**3/(3*mu)
        assert overlaps(c,concentration)
        companion_inertia=2*J/(3*mu*xr*xr)
        assert overlaps(v,companion_inertia)
        assert v.lower()>0 and v.upper()<(F(2)/3).lower()
        # A displaced 25-digit value must fail the 100-digit enclosure check.
        assert not contains(v,direct*(1+F('1e-25')))
        out[n]={'stored':scalar(rows[n]),'moment_identity':True,'companion_normalizations':True,'displaced_control_rejected':True}
    print('DATA '+json.dumps({'table':'T616','skill_manifest':'agents/runs/20261030T025528Z-build-skills/manifest.json','checks':out}),flush=True)
if __name__=='__main__':main()