check_values.py

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

7256 bytes, as of the version from 2026-11-17 06:17. Recorded here, not run.

"""Independent mathematical check for T733 (does not import generate.py).

Run: sage -python check_values.py [numbers.json [check-results.json]]
Input numbers.json consists of the API readback records {alpha,number}, or
build rows with {alpha,value,stored}. Each decimal is interpreted as its full
NumberDB interval, one last-place unit on either side. Sage 10.9/FLINT certified
zeta_zeros gives 150 consecutive globally indexed zeros starting with index 1;
zeros 138 and 139 enclose T=300. Its isolation/counting algorithms certify
completeness of that prefix, rather than just 138 sign changes.

For u>=300, Hasanalizade-Shen-Wong, Corollary 1.2, (1.5), implies N(u)<=u^2:
log(u/(2*pi*e))<=log u<=u, log log u<=log u<=u, and therefore
U(u)<=u^2/(2*pi)+0.3611*u+9.3675<=u^2. In the last step pi>3, u>=300.
The unconditional omitted two-sided sum is bounded by Stieltjes integration:
4*t*exp(t/4)*integral_T^inf u^3*exp(-t*u^2)du
 =2*exp(t/4-t*T^2)*(T^2+1/t).
Only 0<Re(rho)<1 is used above T. Finite zero errors are propagated.
Shared dependency with the prime route: Sage Arb/FLINT ball arithmetic;
independent mathematics and data: certified zeros, no prime enumeration,
archimedean quadrature, or production generator code.
"""
import os,sys,json,time,math
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.rings.complex_arb import ComplexBallField

R=RealBallField(512);C=ComplexBallField(512)

def qdecimal(s):
    f=Fraction(s)
    return QQ(f.numerator)/f.denominator

def decimal_ball(s):
    s=str(s).strip()
    if s.startswith('[') and '+/-' in s:
        return R(s)
    if '+/-' in s:
        mid,rad=s.split('+/-')
        return R(qdecimal(mid.strip())).add_error(R(qdecimal(rad.strip())))
    if s.startswith('['):
        lo,hi=s.strip('[]').split(',')
        lo=qdecimal(lo.strip());hi=qdecimal(hi.strip())
        return R((lo+hi)/2).add_error(R((hi-lo)/2))
    # A plain integer here is exact; a decimal mantissa gives one last-place.
    low=s.lower();parts=low.split('e');mant=parts[0]
    exp=int(parts[1]) if len(parts)>1 else 0
    if '.' not in mant and len(parts)==1:return R(qdecimal(s))
    places=len(mant.split('.')[1]) if '.' in mant else 0
    ulp=QQ(10)**(exp-places)
    return R(qdecimal(s)).add_error(R(ulp))

def finite_narrow(x,rel='1e-99'):
    return x.is_finite() and not x.contains_zero() and R(x.rad())<abs(x)*R(rel)

def compare(claim,truth):
    if not (finite_narrow(claim,'1e-97') and finite_narrow(truth,'1e-115')):
        return False
    # Endpoints of the independent ball must lie strictly inside claimed ball.
    return claim.lower() <= truth.lower() and truth.upper() <= claim.upper()

def controls():
    t=R(3)/7
    mock=sum((C(-t)*C(z)**2).exp() for z in [-1,1]).real()
    known=2*(-t).exp()
    assert (mock-known).contains_zero() and mock>0
    t=2*R.pi();I=C.gen(0)
    quartet=sum((-C(t)*(C(s)+I*R(h)/4)**2).exp()
                for s in [-1,1] for h in [-1,1]).real()
    exact=4*(-15*t/16).exp()*(t/2).cos()
    assert (quartet-exact).contains_zero() and quartet<0
    wrong_real=4*(-t).exp();wrong_magnitude=4*(-17*t/16).exp()
    assert not quartet.overlaps(wrong_real) and not quartet.overlaps(wrong_magnitude)
    # Validate the actual comparator against nonzero known truth, wrong,
    # NaN, vacuous intervals, and a deliberately missing multiplicity factor.
    narrow=R('1.25').add_error(R('1e-120'))
    claim=decimal_ball('1.250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')
    assert compare(claim,narrow)
    assert not compare(claim,2*narrow)
    assert not compare(R('nan'),narrow)
    assert not compare(R(0).add_error(R(100)),narrow)
    # Gaussian Fourier transform, directly integrated on [-12,12];
    # validates the angular-frequency scale, both poles, and y=1 benchmark.
    checks=[]
    for alpha in [QQ(1)/12,QQ(1)]:
        a=R(alpha)
        for z in [C(1),I/2,-I/2]:
            f=lambda x,analytic:(-R.pi()*x*x/a-C.gen(0)*z*x).exp()/a.sqrt()
            v=C.integral(f,-12,12,abs_tol=R('1e-130'),rel_tol=R('1e-130'))
            # For |Im z|<=1/2, each Gaussian tail <=
            # exp(-c*L^2+L/2)/(2*c*L-1/2)/sqrt(a).
            c=R.pi()/a;tail=2*(-144*c+6).exp()/((24*c-R(1)/2)*a.sqrt())
            v=v.real().add_error(tail)
            target=(-a*z*z/(4*R.pi())).exp().real()
            assert (v-target).contains_zero() and R(v.rad())<R('1e-120')
            checks.append({'alpha':str(alpha),'z':str(z),'difference':str(v-target)})
    return {'known_answer_control':True,'negative_control_rejected':True,
            'quartet':str(quartet),'gaussian_transform_checks':checks}

def run(rows):
    start=time.time();con=controls();print('CONTROLS passed',flush=True)
    zs=C.zeta_zeros(150)
    assert all(z.real()==R(QQ(1)/2) and z.imag().is_finite() for z in zs)
    assert all(zs[k].imag().upper()<zs[k+1].imag().lower() for k in range(149))
    prefix=[z.imag() for z in zs if z.imag().upper()<300]
    assert len(prefix)==138 and zs[138].imag().lower()>300
    print('ZEROS complete prefix 138, T=300',flush=True)
    out=[];failures=[]
    for row in rows:
        alpha=QQ(row['alpha']);a=R(alpha);t=a/(4*R.pi())
        finite=2*sum((-t*g*g).exp() for g in prefix)
        tail=2*(t/4-90000*t).exp()*(90000+1/t)
        value=finite.add_error(tail)
        claim=decimal_ball(row.get('number',row.get('stored','')))
        ok=compare(claim,value)
        assert not compare(claim,finite/2), 'missing factor two was accepted'
        if alpha==1:
            omitted=R(2).log()*(-R.pi()*R(4).log()**2).exp()
            assert not compare(claim,value+omitted), 'missing prime power accepted'
            if 'arch' in row:
                wrong=R(row['pole'])+R(row['arch'])-R(row['prime'])
                assert not compare(claim,wrong), 'archimedean sign error accepted'

        if not ok:failures.append({'alpha':str(alpha),'claim':str(claim),'independent':str(value)})
        out.append({'alpha':str(alpha),'zero_value':str(value),'zero_tail':str(tail),
                    'claim':str(claim),'passed':bool(ok),'relative_radius':str(R(value.rad())/abs(value))})
        print('CHECK '+str(alpha)+' '+str(bool(ok)),flush=True)
    return {'checked_entries':len(rows),'total_entries':len(rows),'failures':failures,
            **con,'method':'certified 138-zero prefix through T=300 plus unconditional polynomial-majorant tail',
            'work_bits':512,'zero_count':138,'next_zero':str(zs[138]),
            'seconds':time.time()-start,'rows':out}

if __name__=='__main__':
    root=Path(__file__).parent
    src=Path(sys.argv[1]) if len(sys.argv)>1 else root/'numbers.json'
    dst=Path(sys.argv[2]) if len(sys.argv)>2 else root/'check-results.json'
    result=run(json.loads(src.read_text()))
    try: dst.write_text(json.dumps(result,indent=2))
    except OSError: pass # read-only builder mounts; full result also on stdout
    print('RESULT '+json.dumps({k:v for k,v in result.items() if k!='rows'}),flush=True)
    for row in result['rows']: print('CHECKROW '+json.dumps(row),flush=True)
    if result['failures']:raise SystemExit(1)