readback_checks.py

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

3037 bytes, as of the version from 2026-11-02 11:02 (current). Recorded here, not run.

"""Check T651 values read from the API, using independent results and companions.

Run with SageMath and NUMBERDB_API_KEY set, or key piped through the campaign
wrapper with NUMBERDB_KEY_FROM_STDIN=1. No value is sent or changed.
The interval comparison propagates the last-place intervals declared by T649
and T650; it is a compatibility check, not new certification of their digits.
"""
import json,sys,os
from pathlib import Path
from decimal import Decimal
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.real_mpfi import RealIntervalField
import mpmath as mp
from catalogue import items,exact
from check import agrees_with
if os.environ.get('NUMBERDB_KEY_FROM_STDIN')=='1':numberdb.configure(api_key=sys.stdin.read().strip())

def text(entry):return entry['number'] if isinstance(entry,dict) else entry

def interval(value):
    d=Decimal(value);sign,digits,exponent=d.as_tuple()
    n=QQ(int(''.join(map(str,digits))))*(-1 if sign else 1)
    unit=QQ(10)**int(exponent);mid=n*unit
    return RealIntervalField(800)(mid-unit,mid+unit)

def main():
    doc=numberdb.table('T651');stored=doc['Numbers']
    companions={tid:numberdb.table(tid)['Numbers'] for tid in ['T649','T650']}
    independent={r['t']:r for s in Path(__file__).with_name('integral-checks.jsonl').read_text().splitlines() if '"t"' in s for r in [json.loads(s)]}
    assert list(stored)==[i['key'] for i in items()]
    comparisons=[]
    for item in items():
        key=item['key'];periods=[]
        for conj in [False,True]:
            t=exact(item,conj);tid='T650' if -1<t<27 else 'T649'
            periods.append(interval(text(companions[tid][item['conjugate'] if conj else key])))
        determinant=item['a']*periods[0]**2+item['b']*periods[1]**2
        written=interval(text(stored[key]))
        assert not (determinant.upper()<written.lower() or written.upper()<determinant.lower())
        with mp.workdps(220):
            value=text(stored[key]);expected=mp.mpf(independent[key]['determinant'])
            ulp=mp.power(10,Decimal(value).as_tuple().exponent)
            error=abs(mp.mpf(value)-expected)/ulp
            assert error<=1,(key,error)
        comparisons.append({'t':key,'independent_error_in_written_ulps':mp.nstr(error,10),'stored_companion_interval_overlap':True})
    # Reuse the standard comparison check on independently rounded values.
    with mp.workdps(200):
        assert not agrees_with({k:text(v) for k,v in stored.items()},lambda k:mp.nstr(mp.mpf(independent[k]['determinant']),100,strip_zeros=False))
    arithmetic=json.loads(Path(__file__).with_name('lvalue-check.json').read_text())
    with mp.workdps(200):
        key='729+405*sqrt(3)';value=text(stored[key]);ulp=mp.power(10,Decimal(value).as_tuple().exponent)
        assert abs(mp.mpf(value)-mp.mpf(arithmetic['values'][-1]))<ulp
    print(json.dumps({'tid':'T651','entries':len(stored),'independent_integral_checks':comparisons,'lvalue_row16_inside_written_interval':True},indent=2))
if __name__=='__main__':main()