independent_check.py

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

6776 bytes, as of the version from 2026-12-01 00:22 (current). Recorded here, not run.

"""Independent exact rational interval verification of every stored exponent.

python3 independent_check.py --document stored.json --primary numerical-evidence.json --output check-results.json
Uses only Python's standard library, no Sage, Arb, numerical log, or generator.

For 1<=y<=2, z=(y-1)/(y+1) is in [0,1/3]. Integrating the
geometric series gives log(y)=2 sum_{j=0}^{J-1} z^(2j+1)/(2j+1)+R,
0<=R<=2*z^(2J+1)/((2J+1)*(1-z*z)). Range reduction x=2^m*y
is exact. All intermediate series terms and sums are rounded OUTWARD to
600-bit dyadic rational endpoints. The tail uses the upper bound on the
next power. Thus rounding enlarges, never invalidates, the enclosure.
Logs of interval inputs are enclosed using endpoint monotonicity.

The primary uses u*log(u)-u+1 and Arb. This checker independently assembles
1+(d-1)/log(d)*(log(d-1)-log(log(d))-1) with rational endpoint operations.
Both formulas share the inspected mathematical source; this is a builder's
independent implementation, not an independent-session scientific verdict.
"""
import argparse,json,re,time
from fractions import Fraction as F
from functools import lru_cache
from pathlib import Path
J=180
SCALE=2**600
ZERO=(F(0),F(0))
ONE=(F(1),F(1))
def outward(a):
    return (F((a[0]*SCALE).__floor__(),SCALE),F((a[1]*SCALE).__ceil__(),SCALE))
def add(a,b):return (a[0]+b[0],a[1]+b[1])
def neg(a):return (-a[1],-a[0])
def sub(a,b):return add(a,neg(b))
def mul(a,b):
    p=[x*y for x in a for y in b];return (min(p),max(p))
def reciprocal(a):
    assert 0<a[0]<=a[1];return (1/a[1],1/a[0])
def point(x):x=F(x);return (x,x)
def contains(a,b):return a[0]<=b[0]<=b[1]<=a[1]
def overlaps(a,b):return max(a[0],b[0])<=min(a[1],b[1])
@lru_cache(None)
def log_reduced(y):
    assert F(1)<=y<=F(2)
    z=(y-1)/(y+1);z2=z*z;power=outward(point(z));total=ZERO
    for j in range(J):
        total=outward(add(total,mul(power,point(F(1,2*j+1)))))
        power=outward(mul(power,point(z2)))
    tail=2*power[1]/((2*J+1)*(1-z2))
    return outward((2*total[0],2*total[1]+tail))
LOG2=log_reduced(F(2))
@lru_cache(None)
def log_rational(x):
    x=F(x);assert x>0;m=0
    while x<1:x*=2;m-=1
    while x>=2:x/=2;m+=1
    return outward(add(log_reduced(x),mul(point(m),LOG2)))
def log_interval(x):
    assert 0<x[0]<=x[1]
    return (log_rational(x[0])[0],log_rational(x[1])[1])
def q_interval(u):return add(sub(mul(u,log_interval(u)),u),ONE)
def exponent(d):
    assert type(d) is int and d>=2
    L=log_rational(F(d))
    bracket=sub(sub(log_rational(F(d-1)),log_interval(L)),ONE)
    return add(ONE,mul(mul(point(d-1),reciprocal(L)),bracket))
def decimal_interval(value):
    text=str(value).strip()
    if text.startswith('[') and text.endswith(']'):
        a,b=text[1:-1].split(',');return F(a),F(b)
    if '+/-' in text:
        a,b=text.split('+/-');return F(a)-F(b),F(a)+F(b)
    assert re.fullmatch(r'-?\d+\.\d+(?:[eE][+-]?\d+)?',text)
    decimal,_,exponent=text.lower().partition('e')
    unit=F(10)**(int(exponent or '0')-len(decimal.split('.')[1]));center=F(text)
    return center-unit,center+unit
def read_rows(doc):
    assert list(doc['Parameters'])==['d']
    block=doc['Numbers']
    if isinstance(block,list):
        assert all(isinstance(r,dict) and set(r['params'])=={'d'} for r in block)
        return [(int(r['params']['d']),r['number']) for r in block]
    assert isinstance(block,dict)
    return [(int(d),v['number'] if isinstance(v,dict) else v) for d,v in block.items()]
def run(doc,primary=None):
    started=time.perf_counter();rows=read_rows(doc);assert [d for d,v in rows]==list(range(2,101))
    bounds={int(p['d']):(F(p['lower']),F(p['upper'])) for p in primary['primary_enclosures']} if primary else {}
    known=(log_rational(F(1))==ZERO and q_interval(ONE)==ZERO and
           overlaps(q_interval(point(2)),sub(mul(point(2),LOG2),ONE)))
    prefix=decimal_interval('0.086071332055934206887573');known=known and contains(prefix,exponent(2))
    failures=[];evidence=[];previous=None
    for d,value in rows:
        checked=exponent(d);stored=decimal_interval(value)
        narrow=0<checked[1]-checked[0]<F(1,10**150) and 0<stored[1]-stored[0]<abs(checked[0])*F(3,10**99)
        ok=narrow and contains(stored,checked) and checked[0]>0
        if previous is not None:ok=ok and previous<checked[0]
        previous=checked[1]
        if d in bounds:ok=ok and contains(stored,bounds[d]) and overlaps(checked,bounds[d]) and 0<bounds[d][1]-bounds[d][0]<F(1,10**145)
        if not ok:failures.append('Enclosure/width/monotonicity failure at d=%d'%d)
        evidence.append({'d':d,'stored_value':value,'rational_enclosure':[str(x) for x in checked],
                         'enclosure_width':str(checked[1]-checked[0]),'stored_width':str(stored[1]-stored[0]),
                         'contained':contains(stored,checked),'primary_overlap':overlaps(checked,bounds[d]) if d in bounds else None})
    correct=exponent(2);stored=decimal_interval(rows[0][1]);shifted=q_interval(mul(point(2),reciprocal(LOG2)))
    wrong={'wrong_sign':neg(correct),'missing_additive_one':sub(correct,ONE),'base2_logs':ZERO,'using_d_instead_of_d_minus_one':shifted}
    rejected={name:not overlaps(stored,v) for name,v in wrong.items()}
    rejected['perturbed_stored_interval']=not overlaps(add(stored,point(F(1,1000))),correct)
    if not known:failures.append('Known-answer control failed')
    if not all(rejected.values()):failures.append('Wrong-answer control accepted')
    return {'checked_entries':len(rows),'total_entries':99,'failures':failures,'known_answer_control':known,
            'negative_control_rejected':all(rejected.values()),'negative_controls':rejected,
            'known_controls':['log(1)=0','Q(1)=0','Q(2)=2 log(2)-1','OEIS A074738 short prefix'],
            'method':'Exact Fraction intervals; outward dyadic rational bounded log series; expanded exponent formula',
            'series_terms':J,'dyadic_rounding_bits':600,'entries':evidence,'elapsed_seconds':time.perf_counter()-started,
            'skill_manifest':'20261201T001607Z-build-skills/manifest.json',
            'skill_bundle_sha256':'f20b09e3db5fd443660c0c6b29391ee369b7c710a43e22d8abb971ec3f54dd14'}
if __name__=='__main__':
    p=argparse.ArgumentParser(description=__doc__);p.add_argument('--document',type=Path,required=True);p.add_argument('--primary',type=Path);p.add_argument('--output',type=Path,required=True);a=p.parse_args()
    report=run(json.loads(a.document.read_text()),json.loads(a.primary.read_text()) if a.primary else None)
    a.output.write_text(json.dumps(report,indent=2)+'\n')
    print(json.dumps({k:report[k] for k in ['checked_entries','total_entries','failures','known_answer_control','negative_control_rejected','elapsed_seconds']}))
    raise SystemExit(bool(report['failures']))