check_t466_repair.py

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

4923 bytes, as of the version from 2026-10-24 20:50 (current). Recorded here, not run.

"""Checks for the editorial repair of https://numberdb.org/T466.

Run with SageMath and numberdb installed:
    cat "$NUMBERDB_KEY_FILE" | agents/sage.sh check_t466_repair.py
or, on a contributor's laptop:
    sage -python check_t466_repair.py < /path/to/numberdb-key

Read-only: fetch the current document and pinned ancillary census. Check the
omitted labels, all stored decimal lengths, four wheel values with Arb,
and the live Sage example. No graph integral is independently integrated.
The example check uses its own PARI method and is not independent validation.
"""
import contextlib
from decimal import Decimal
import hashlib
import io
import json
import math
import re
import sys
import urllib.request

import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.real_arb import RealBallField

TID = 'T466'
SOURCE = 'https://arxiv.org/src/1603.04289v2/anc/PeriodsNonPhi4'
SOURCE_SHA256 = 'f735c62370e0e1561e0cdffc28d86226d6d0fb0be138729abd44f74191d1966a'


def rational_decimal(text):
    a, b = Decimal(text).as_integer_ratio()
    return QQ(a) / QQ(b)


def last_place(text):
    exponent = int(Decimal(text).as_tuple().exponent)
    return QQ(10) ** exponent


def main():
    key = sys.stdin.readline().strip()
    assert key, 'An API key must arrive on stdin.'
    request = urllib.request.Request(
        'https://numberdb.org/api/table?id=' + TID,
        headers={'Authorization': 'Bearer ' + key})
    with urllib.request.urlopen(request, timeout=45) as response:
        document = json.load(response)
    assert 'Numbers' in document, 'The authenticated API did not return the table.'
    with urllib.request.urlopen(SOURCE, timeout=45) as response:
        source_bytes = response.read()
    assert hashlib.sha256(source_bytes).hexdigest() == SOURCE_SHA256
    source = source_bytes.decode('utf8')
    records = re.findall(r'^PeriodNonPhi4\[(\d+),(\d+)\]:=(.*)$', source, re.M)
    assert len(records) == 629
    failed = {(l, k) for l, k, expression in records if 'FAIL' in expression}
    evaluated = {(l, k) for l, k, expression in records if 'FAIL' not in expression}
    stored = {(l, k) for l, entries in document['Numbers'].items() for k in entries}
    assert len(failed) == 118 and {l for l, k in failed} == {'8'}
    assert stored == evaluated and not stored.intersection(failed)
    counts = {l: len(entries) for l, entries in document['Numbers'].items()}
    assert counts == {'5': 1, '6': 3, '7': 50, '8': 457}
    digit_counts = [len(Decimal(entry['number']).as_tuple().digits)
                    for entries in document['Numbers'].values() for entry in entries.values()]
    assert min(digit_counts) == max(digit_counts) == 100

    ball = RealBallField(600)
    control = ball(2).zeta() - ball.pi() ** 2 / 6
    assert control.is_finite() and control.contains_zero()
    assert ball(control.rad()) < ball(QQ(10) ** -170)
    wheels = []
    for n, index in ((5, 1), (6, 1), (7, 21), (8, 162)):
        value = math.comb(2*n-2, n-1) * ball(2*n-3).zeta()
        decimal = document['Numbers'][str(n)][str(index)]['number']
        midpoint, ulp = rational_decimal(decimal), last_place(decimal)
        error = abs(value - ball(midpoint)) / ball(ulp)
        assert value.is_finite() and ball(value.rad()) < ball(ulp) / 10**60
        assert error < 1, 'The wheel enclosure is not inside the stored last-place interval.'
        wheels.append({'loops': n, 'index': index,
                       'error_in_last_place_units': str(error),
                       'enclosed_in_stored_interval': True})

    namespace = {}
    output = io.StringIO()
    with contextlib.redirect_stdout(output):
        exec(compile(document['Programs']['period-example']['code'],
                     'T466-live-period-example', 'exec'), namespace)
    example = namespace['P_7_2']
    decimal = document['Numbers']['7']['2']['number']
    error = abs(ball(example) - ball(rational_decimal(decimal))) / ball(last_place(decimal))
    assert error.is_finite() and error < 1
    result = {
        'table': TID,
        'source_sha256': SOURCE_SHA256,
        'source_records': len(records),
        'omitted_fail_labels': len(failed),
        'all_omitted_fail_labels_at_eight_loops': True,
        'stored_labels_equal_evaluated_source_labels': True,
        'stored_counts': counts,
        'significant_digits_every_entry': 100,
        'arb_precision_bits': 600,
        'zeta2_control_passed': True,
        'wheel_checks': wheels,
        'live_program_passed': True,
        'live_program_output': output.getvalue().strip(),
        'live_program_error_in_last_place_units': str(error),
        'scope': 'Four wheel formulas checked with Arb; one live PARI program executed. '
                 'No independent graph integration or full multiple-zeta recomputation.',
        'ok': True,
    }
    print(json.dumps(result, indent=2))


if __name__ == '__main__':
    main()