validate_offline.py

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

5329 bytes, as of the version from 2026-11-28 22:17 (current). Recorded here, not run.

"""Builder check of every certified stream packet, using distinct real formulas.
Run under Sage with generate.py, knotinfo-inputs.json, spectrum-checkpoints.json,
check.py and dry_run.py beside this source. It writes no website data.
It checks endpoint order statistics and stop inequalities with exact rationals;
then uses the retained SL(2,C) trace intervals in two independent real formulas.
These checks share SnapPy's shape enclosures with the builder. A separate
check_legacy.py compares independently enumerated Dirichlet-domain spectra.
"""
import numberdb.sage as numberdb
from sage.rings.real_mpfi import RealIntervalField
from sage.rings.complex_interval_field import ComplexIntervalField
from sage.rings.rational_field import QQ
from fractions import Fraction
from decimal import Decimal
import json,os,runpy,copy,ast,snappy
from pathlib import Path
from generate import Geodesics,KNOTS,ROOT,INPUT_HASH,ALGORITHM,from_endpoints,endpoints
import check
R=RealIntervalField(600);C=ComplexIntervalField(600)
from generate import load_checkpoints
p=load_checkpoints()
assert p['input_sha256']==INPUT_HASH and p['algorithm']==ALGORITHM
assert {(r['n'],r['k']) for r in p['knots']}==set(KNOTS)
single_components=0
for data in KNOTS.values():
 braid=ast.literal_eval(data['braid_notation'])
 if braid and isinstance(braid[0],list):braid=braid[0]
 assert len(snappy.Link(braid_closure=braid).link_components)==1
 assert len(snappy.Link(ast.literal_eval(data['pd_notation'])).link_components)==1
 single_components+=1
assert len(snappy.Link(braid_closure=[1,-2,1,-2]).link_components)==1
assert len(snappy.Link(braid_closure=[1,1]).link_components)==2

def real(e):return from_endpoints(e,R)
def complex_interval(e):return C(real(e[0]),real(e[1]))
def text_interval(s):
 q=QQ(Fraction(s));u=QQ(10)**Decimal(s).as_tuple().exponent
 return R(q-u,q+u)
def trace_formula(t):return ((abs(t)**2+abs(t*t-4))/4).arccosh()
def eigen_formula(t):
 d=(t*t-4).sqrt();z1=(t+d)/2;z2=(t-d)/2
 if abs(z1).lower()>1:z=z1
 elif abs(z2).lower()>1:z=z2
 else:raise ArithmeticError('Expanding eigenvalue not isolated')
 return 2*abs(z).log()
def good(x):return bool(x.lower()>0 and x.upper()<10**6 and 0<x.absolute_diameter()<QQ(1)/10**115)
def matches(a,b):return good(a) and good(b) and a.overlaps(b)
# Known nonzero hyperbolic and pure translation traces.
known=matches(trace_formula(C(3)),eigen_formula(C(3))) and matches(trace_formula(C(3)),2*((R(3)+R(5).sqrt())/2).log())
wrong=not matches(trace_formula(C(3)),2*eigen_formula(C(3)))
assert known and wrong
# Independently exercise stop rejection and the endpoint-order construction.
def stop_is_valid(row):
 return len(row['witnesses'])>=4 and QQ(row['stop']['real'][0])>sorted(QQ(t['real'][1]) for t in row['witnesses'])[3]
control_packet=copy.deepcopy(p['knots'][0])
control_packet['stop']['real'][0]=str(sorted(QQ(t['real'][1]) for t in control_packet['witnesses'])[3])
stop_control_rejected=not stop_is_valid(control_packet)
synthetic=[(QQ(1),QQ(4)),(QQ(2),QQ(3)),(QQ(5),QQ(6))]
assert sorted(a for a,b in synthetic)==[1,2,5]
assert sorted(b for a,b in synthetic)==[3,4,6]
assert stop_control_rejected
checks=[];failures=[];values={};max_width=QQ(0)
for row in p['knots']:
 n,k=row['n'],row['k'];w=row['witnesses'];assert len(w)>=4
 lower=sorted(QQ(t['real'][0]) for t in w);upper=sorted(QQ(t['real'][1]) for t in w)
 assert stop_is_valid(row)
 assert all(QQ(w[i]['real'][0])<=QQ(w[i+1]['real'][0]) for i in range(len(w)-1))
 f=[];e=[]
 for t in w:
  tr=complex_interval(t['trace']);a=trace_formula(tr);b=eigen_formula(tr);s=real(t['real'])
  assert matches(a,b) and matches(a,s),('trace mismatch',n,k,t['word'])
  f.append(a);e.append(b)
 fl=sorted(x.lower() for x in f);fu=sorted(x.upper() for x in f)
 el=sorted(x.lower() for x in e);eu=sorted(x.upper() for x in e)
 for j in range(4):
  v=real(row['values'][j]);assert endpoints(v)==[str(lower[j]),str(upper[j])]
  a=R(fl[j],fu[j]);b=R(el[j],eu[j]);stored=text_interval(row['numbers'][j])
  assert matches(v,a) and matches(v,b)
  assert v in stored and a in stored and b in stored,('serialization containment',n,k,j+1)
  assert type(v).__name__=='RealIntervalFieldElement'
  key=f'{n},{k},{j+1}';values[key]=v
  max_width=max(max_width,v.absolute_diameter().exact_rational())
  checks.append({'params':[n,k,j+1],'number':row['numbers'][j],'trace_interval':endpoints(a),'eigen_interval':endpoints(b),'stop_verified':True})
assert not check.exactness(values)
assert not check.exact_in_disguise(values)
assert not check.prose(values)
assert not check.names_its_rings(str(ROOT/'generate.py'))
size=check.measure(values)
print('RESULT_JSON '+json.dumps({'table':'T864','checked_entries':len(checks),'total_entries':972,'failures':failures,'known_answer_control':bool(known),'negative_control_rejected':bool(wrong and stop_control_rejected),'checks':checks,'input_single_components_checked':single_components,'max_source_width':str(R(max_width)),'size':size,'method':'Exact endpoint order/stop checks, absolute-trace arcosh and expanding-eigenvalue formulas, and complete serialization containment. Shared certified shapes acknowledged.'}),flush=True)
# Existing deterministic dry-run implementation, with the explicit cache opt-in.
os.environ['NUMBERDB_USE_CHECKPOINTS']='1'
import dry_run
assert dry_run.main([str(ROOT/'generate.py')])==0