repair-checks.py

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

4321 bytes, as of the version from 2026-10-25 12:37 (current). Recorded here, not run.

"""Verify the precision statement for numberdb.org/T486; never write to the API.

Download this file and the attached generate.py into the same directory.
With SageMath and numberdb installed, run:
    sage -python repair-checks.py
Set NUMBERDB_API_KEY to read the private draft. Alternatively pipe the key on
stdin and set NUMBERDB_KEY_FROM_STDIN=1. The key is never printed or saved.

Checks every stored decimal interval using Arb characteristic-function signs,
checks zero-mode dimensions by exact rational elimination, and reruns the
attached certified generator's verify(sample=None). These checks support the
precision sentence added in the repair; no numbers are changed.
"""
import os
import sys
import json
from decimal import Decimal
from fractions import Fraction
from pathlib import Path
import importlib.metadata
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.real_arb import RealBallField
from sage.env import SAGE_VERSION
from generate import BeamRoots

if os.environ.get('NUMBERDB_KEY_FROM_STDIN') == '1':
    numberdb.configure(api_key=sys.stdin.read().strip())
doc = numberdb.table('T486')
rb = RealBallField(768)

def q(x):
    f = Fraction(x)
    return QQ(f.numerator)/QQ(f.denominator)

def sign(x):
    assert x.is_finite(), 'nonfinite ball'
    assert x != 0 and not x.contains_zero(), 'uncertified sign'
    return 1 if x > 0 else -1

def characteristic(boundary, z):
    if boundary == 'clamped-free':
        return z.cos()*z.cosh()+1
    if boundary in ('clamped-clamped','free-free'):
        return z.cos()*z.cosh()-1
    if boundary in ('clamped-pinned','pinned-free'):
        return z.sin()*z.cosh()-z.cos()*z.sinh()
    assert boundary == 'pinned-pinned'
    return z.sin()

# Known-answer controls: a rational bracket for pi and a nonroot interval.
assert sign(rb(3).sin()) == 1
assert sign(rb(QQ(22)/7).sin()) == -1
assert sign(rb(QQ(31)/10).sin()) == 1
assert rb.pi().is_finite() and QQ(rb.pi().rad()) < QQ(1)/10**220

# At beta=0 the general solution is a cubic polynomial. At L=1 the rows
# represent X, X', X'', X''' on the coefficient vector (a,b,c,d).
endpoint_rows = {
    0: [[1,0,0,0],[0,1,0,0],[0,0,2,0],[0,0,0,6]],
    1: [[1,1,1,1],[0,1,2,3],[0,0,2,6],[0,0,0,6]],
}
conditions = {'clamped':(0,1), 'pinned':(0,2), 'free':(2,3)}
def nullity(boundary):
    ends = boundary.split('-')
    rows = [[QQ(x) for x in endpoint_rows[end][d]]
            for end in (0,1) for d in conditions[ends[end]]]
    rank = 0
    for col in range(4):
        pivot = next((i for i in range(rank,4) if rows[i][col]),None)
        if pivot is None:
            continue
        rows[rank],rows[pivot] = rows[pivot],rows[rank]
        a=rows[rank][col]
        rows[rank]=[x/a for x in rows[rank]]
        for i in range(4):
            if i != rank:
                a=rows[i][col]
                rows[i]=[x-a*y for x,y in zip(rows[i],rows[rank])]
        rank += 1
    return 4-rank

positive=0
zero=0
nullities={}
for boundary, rows in doc['Numbers'].items():
    nullities[boundary]=nullity(boundary)
    assert nullities[boundary] == {'free-free':2,'pinned-free':1}.get(boundary,0)
    expected_zeros=nullities[boundary]
    for n, entry in rows.items():
        s=entry['number'] if isinstance(entry,dict) else entry
        if s == '0':
            zero += 1
            assert int(n) <= expected_zeros
            continue
        assert int(n)>expected_zeros
        dec=Decimal(s)
        assert dec>0 and len(dec.as_tuple().digits)==100, (boundary,n,s)
        midpoint=q(s)
        ulp=QQ(10)**dec.as_tuple().exponent
        a=characteristic(boundary,rb(midpoint-ulp))
        b=characteristic(boundary,rb(midpoint+ulp))
        assert sign(a)*sign(b)==-1,(boundary,n,'no endpoint sign change')
        positive += 1
assert positive==69 and zero==3
assert BeamRoots.digits == 100
report=BeamRoots().verify(sample=None)
print(report)
assert report.ok
print(json.dumps({'table':'T486','positive_decimal_intervals_certified':positive,
                  'significant_digits_per_positive_entry':100,'exact_zero_entries':zero,
                  'zero_mode_dimensions':nullities,'generator_verify_ok':report.ok,
                  'arb_bits':768,'sage':SAGE_VERSION,
                  'numberdb':importlib.metadata.version('numberdb')},indent=2))