repair_checks.py

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

3032 bytes, as of the version from 2026-11-09 09:32 (current). Recorded here, not run.

"""Check T686's self-contained Programs snippet; no writes to NumberDB.
Run: sage -python repair_checks.py
Place program.py and document-before.json beside this file.
"""
import json,hashlib,platform,sys
from pathlib import Path
from decimal import Decimal
import mpmath as mp
from sage.version import version as sage_version
import numberdb.sage as numberdb
from importlib.metadata import version
w=Path(__file__).resolve().parent
scope={}; exec((w/'program.py').read_text(),scope)
W3=scope['W3']
d=json.loads((w/'document-before.json').read_text())
result={'skill_manifest':'agents/runs/20261109T092414Z-repair-skills/manifest.json',
        'skill_local_sha256':'886390324dd1971e381a43cf3b46b822ab0bc67d62f31d8481e29ec79556746b',
        'skill_live_sha256':'886390324dd1971e381a43cf3b46b822ab0bc67d62f31d8481e29ec79556746b',
        'versions':{'python':platform.python_version(),'sage':sage_version,'mpmath':mp.__version__,'numberdb':version('numberdb')},
        'document_sha256':hashlib.sha256((w/'document-before.json').read_bytes()).hexdigest(),
        'program_sha256':hashlib.sha256((w/'program.py').read_bytes()).hexdigest(),
        'source':'BBSW Theorem 4.1, equation (23); second-moment control from Theorem 5.4, equation (37).',
        'method':'mpmath derivatives at 150 and 180 decimal working digits, not rigorous enclosures. No numbers replaced.',
        'passes':[]}
values=[]
for precision in (150,180):
    mp.mp.dps=precision
    first=mp.im(mp.polylog(2,mp.exp(mp.j*mp.pi/3)))/mp.pi
    second=mp.pi**2/4-3/mp.pi*mp.quad(lambda t:mp.log(2*mp.sin(t/2))**2,[0,mp.pi/3,2*mp.pi/3])
    controls={'mass':abs(W3(mp.mpf(0))-1),'first_moment':abs(mp.diff(W3,0,1)-first),'second_moment':abs(mp.diff(W3,0,2)-second)}
    assert all(mp.isfinite(x) and x < mp.mpf('1e-140') for x in controls.values())
    print('controls',precision,{k:mp.nstr(v,8) for k,v in controls.items()},flush=True)
    current=[]; rows=[]
    for k in range(2,10):
        x=mp.diff(W3,0,k); assert mp.isfinite(x); current.append(x)
        row={'n':3,'k':k,'value':mp.nstr(x,precision)}
        if str(k) in d['Numbers']['3']:
            stored=d['Numbers']['3'][str(k)]
            ulp=mp.mpf(10)**Decimal(stored).as_tuple().exponent
            discrepancy=abs(x-mp.mpf(stored))/ulp
            assert discrepancy<=1
            row['stored_last_place_units']=mp.nstr(discrepancy,16)
        else: row['purpose']='one further order; not added to the table'
        rows.append(row); print('checked',precision,k,row.get('stored_last_place_units','extension-only'),flush=True)
    values.append(current)
    result['passes'].append({'working_digits':precision,'control_errors':{k:mp.nstr(v,20) for k,v in controls.items()},'rows':rows})
errors=[abs(a-b)/max(1,abs(b)) for a,b in zip(*values)]
assert max(errors)<mp.mpf('1e-145')
result['max_scaled_precision_difference']=mp.nstr(max(errors),20)
result['passed']=True
print('RESULT_JSON_START',flush=True)
print(json.dumps(result,indent=2),flush=True)
print('RESULT_JSON_END',flush=True)