eta_check.py

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

6389 bytes, as of the version from 2026-11-30 23:10. Recorded here, not run.

"""Builder check, not an independent review verdict.
Run: sage -python eta_check.py [--stored document.json]
With no --stored, reads production.json beside this script. Also needs the
selected-T230.json and selected-T835.json snapshots. Writes check-results.json
beside the script when writable, and always emits CHECK_JSON for artifact use.
Production uses logGamma; this checker uses only a bounded eta log-product.
For r=exp(-2*pi), -log(1-z)<=z/(1-z) implies the tail bound
sum_{n>N}-log(1-r^n) <= r^(N+1)/((1-r)*(1-r^(N+1))).
Multiplication by 4*pi bounds the omitted contribution to the finite part.
All constants and arithmetic, including r and that bound, are Arb balls.
The independent critic must still inspect sources, mathematics and this code.
"""
import argparse,json,math,sys
from fractions import Fraction
from pathlib import Path
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.real_arb import RealBallField
from sage.libs.pari import pari
base=Path(__file__).resolve().parent
R=RealBallField(640)
def exact(x):
 f=Fraction(x); return QQ(f.numerator)/f.denominator
def bounds(text):
 # All three inputs here are plain decimals, each uncertain by one last place.
 f=Fraction(text); a=text.lower().split('e'); mantissa=a[0]; exponent=int(a[1]) if len(a)>1 else 0
 places=len(mantissa.split('.')[1]) if '.' in mantissa else 0
 unit=Fraction(10)**(exponent-places)
 return exact(f-unit),exact(f+unit)
def decimal_ball(text):
 lo,hi=bounds(text); return R((lo+hi)/2).add_error(R((hi-lo)/2))
def finite_narrow(v,limit):
 return bool(v.is_finite() and v.rad()>0 and R(v.rad())<limit)
def contained(v,text):
 lo,hi=bounds(text)
 return bool(finite_narrow(v,R(10)**-110) and lo<=QQ(v.lower()) and QQ(v.upper())<=hi)
def eta_value(N=60):
 pi=R.pi(); gamma=R.euler_constant(); r=(-2*pi).exp()
 logs=sum(((1-r**n).log() for n in range(1,N+1)),R(0))
 partial=2*pi*(gamma-R(2).log())+pi*pi/3-4*pi*logs
 tail=4*pi*r**(N+1)/((1-r)*(1-r**(N+1)))
 assert tail.is_finite() and tail>0
 enclosed=partial.add_error(R(tail.upper()))
 return enclosed,tail,partial

def main():
 parser=argparse.ArgumentParser(description=__doc__); parser.add_argument('--stored',type=Path); opts=parser.parse_args()
 if opts.stored:
  doc=json.loads(opts.stored.read_text())
  assert list(doc['Parameters'])==['constant']
  assert list(doc['Numbers'])==['S']
  text=doc['Numbers']['S']
  assert isinstance(text,str)
 else: text=json.loads((base/'production.json').read_text())['text']
 value,tail,partial=eta_value(); pi=R.pi()
 failures=[]
 def require(test,name):
  if not test: failures.append(name)
 require(contained(value,text),'eta enclosure not inside full serialized claim')
 # Zero nome and a nonzero logarithmic control establish loop sign/coercion.
 zero=sum(((1-R(0)**n).log() for n in range(1,4)),R(0))
 one=-(1-R(QQ(1)/2)).log()
 known=zero.is_exact() and zero==0 and (one-R(2).log()).contains_zero() and one>R(QQ(1)/2)
 require(known,'known-answer controls')
 # Validate the positive tail bound against a finite exact rational product.
 rr=QQ(1)/2; rational_product=QQ(1)
 for n in range(4,24): rational_product*=1-rr**n
 finite_log=-R(rational_product).log()
 analytic=R(rr**4/((1-rr)*(1-rr**4)))
 require(finite_log>0 and finite_log<analytic,'tail-bound control')
 # Establish all-vector multiplicities in the Dirichlet-series identity.
 counts=[0]*201
 for m in range(-14,15):
  for n in range(-14,15):
   norm=m*m+n*n
   if 0<norm<=200: counts[norm]+=1
 for norm in range(1,201):
  divisor_sum=sum((0 if d%2==0 else (1 if d%4==1 else -1)) for d in range(1,norm+1) if norm%d==0)
  require(counts[norm]==4*divisor_sum,'r2 divisor identity '+str(norm))
 require((counts[1],counts[2],counts[3],counts[5])==(4,4,0,8),'r2 known answers')
 wrongs={'residue_normalized':value/pi,'units_quotient':value/4,'unsigned_half':value/2,'spectral_scale':(value-pi*(4*pi*pi).log())/(4*pi*pi),'last_place_perturbation':value+R(10)**-98}
 rejected={name:not contained(v,text) for name,v in wrongs.items()}
 rejected['nonfinite']=not contained(R('nan'),text)
 rejected['vacuously_wide']=not contained(value.add_error(R(100)),text)
 require(all(rejected.values()),'negative controls')
 external={}
 for tid in ('T230','T835'):
  row=json.loads((base/('selected-'+tid+'.json')).read_text()); source=decimal_ball(row['number'])
  transformed=pi*source if tid=='T230' else 2*pi*(R.euler_constant()-R(2).log())-pi*source
  # Compare exact dyadic endpoints, with the external decimal error propagated.
  good=finite_narrow(transformed,R(10)**-96) and QQ(transformed.lower())<=QQ(value.lower()) and QQ(value.upper())<=QQ(transformed.upper())
  require(good,tid+' propagated interval')
  external[tid]={'row':row,'transformed':str(transformed),'radius':str(transformed.rad()),'contains_independent_enclosure':bool(good)}
 # PARI lfun computes beta'(1) without the production Gamma expression.
 pari.set_real_precision(170)
 derivative=pari('lfun(-4,1,1)'); beta_one=pari('lfun(-4,1)')
 beta_test=pi*R.euler_constant()+4*R(str(derivative))
 pari_agrees=abs(beta_test-value)<R(10)**-145 and abs(R(str(beta_one))-pi/4)<R(10)**-150
 require(pari_agrees,'PARI beta derivative numerical agreement')
 result={'role':'builder numerical evidence; no independent review verdict','checked_entries':1,'total_entries':1,'failures':failures,'known_answer_control':bool(known),'negative_control_rejected':all(rejected.values()),'stored_text':text,'independent_method':'60-term eta logarithmic product with outward geometric tail','working_bits':640,'eta_enclosure':str(value),'eta_accuracy_bits':int(value.accuracy()),'eta_radius':str(value.rad()),'tail_bound':str(tail),'entire_enclosure_inside_stored':contained(value,text),'controls':rejected,'r2_norms_checked':'1..200, all signed ordered pairs','external_controls':external,'pari':{'version':str(pari.version()),'decimal_working_precision':170,'beta_prime_1':str(derivative),'beta_1':str(beta_one),'finite_part':str(beta_test),'agreement_145_digits':bool(pari_agrees),'rigour':'heuristic cross-check; not used to certify the stored interval'},'versions':{'numberdb':numberdb.__version__,'python':sys.version.split()[0]}}
 try: (base/'check-results.json').write_text(json.dumps(result,indent=2)+'\n')
 except OSError: pass
 print('CHECK_JSON '+json.dumps(result),flush=True)
 if failures: raise SystemExit(1)
if __name__=='__main__': main()