repair_checks.py

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

2566 bytes, as of the version from 2026-10-26 14:52 (current). Recorded here, not run.

"""Check T518's program and preserved values after its display repair.

Run with SageMath and the numberdb client installed:
    sage -python repair_checks.py < /path/to/numberdb-api-key
The key is read only from stdin and used only at NumberDB's API.
This checks 100-digit formatting, not 100-digit numerical accuracy.
SciPy supplies an independent double-precision agreement check on all rows.
"""
import json
import math
import sys
import urllib.request
from decimal import Decimal
import numberdb.sage as numberdb
from sage.rings.real_mpfr import RealField
import scipy
from scipy.special import pro_cv

key = sys.stdin.read().strip()
request = urllib.request.Request('https://numberdb.org/api/table?id=T518',
                                 headers={'Authorization': 'Bearer ' + key})
with urllib.request.urlopen(request, timeout=60) as response:
    doc = json.loads(response.read())
program = doc['Programs']['Python']
code = program['code'] if isinstance(program, dict) else program
namespace = {}
exec(compile(code, '<T518 Programs>', 'exec'), namespace)
chi = namespace['chi']
assert math.isclose(chi(1, 0), 0.7396372387275103, rel_tol=1e-13)
RR = RealField(400)
assert list(doc['Parameters']) == ['m', 'S', 'n']
assert list(doc['Numbers']) == ['0']
assert list(doc['Numbers']['0']) == ['1', '2', '4', '8']
for n in range(12):
    assert pro_cv(0, n, 0) == n * (n + 1)
records = []
for s, modes in doc['Numbers']['0'].items():
    assert list(modes) == [str(n) for n in range(12)]
    c = RR.pi() * RR(s) / 2
    last = RR(-1)
    for label, text in modes.items():
        n = int(label)
        assert len(Decimal(text).as_tuple().digits) == 100
        value = RR(text)
        assert value > last
        assert n * (n + 1) < value < n * (n + 1) + c**2
        approx = chi(int(s), n)
        assert math.isfinite(approx)
        error = abs(RR(float(approx)) - value) / max(RR(1), abs(value))
        assert error < RR('1e-12')
        records.append({'S': int(s), 'n': n, 'scaled_error': float(error)})
        last = value
print(json.dumps({'table': 'T518', 'entries_checked': len(records),
                  'significant_digits_per_entry': 100, 'zero_bandwidth_controls': 12,
                  'program_example': float(chi(1, 0)), 'scipy_version': scipy.__version__,
                  'all_bounds_and_ordering_pass': True,
                  'max_scaled_error': max(r['scaled_error'] for r in records),
                  'accuracy_scope': 'double precision only; no new high-precision accuracy claim',
                  'rows': records}, indent=2))