independent_check.py

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

4314 bytes, as of the version from 2026-11-18 04:29 (current). Recorded here, not run.

"""Independent numerical check for T743, not an independent review verdict.

Run with Sage and mpmath; entries.json is the stored-format record list.
Uses original complex polynomial coefficients and both y roots, rather
than the generator's real numerator logarithm. Contacts are solved directly
from b=+/-2a in the real torus coordinate. Shares Jensen's theorem and
mpmath with the primary computation. The algebraic input is recomputed.
q(5) is checked separately by Catalan's constant; symbolic expansion checks
the normalization and degeneration. The negative control perturbs q(2).
"""
import json,sys,math,time
from pathlib import Path
import numberdb.sage as numberdb
from sage.rings.rational_field import QQ
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from mpmath import mp


def exact_input(text):
    if 'sqrt' not in text:return mp.mpf(text)
    n=int(text.split('(')[1][:-1])
    return 2+(-1 if text[1]=='-' else 1)*mp.sqrt(n)


def direct_measure(k):
    # Solve b+2a=0 and b-2a=0 without importing generator contacts.
    roots=[]
    for sign in (1,-1):
        B=k+sign*2;C=2*k-6+sign*2
        D=B*B-4*C
        if D>=0:
            for s in ((-B+mp.sqrt(D))/2,(-B-mp.sqrt(D))/2):
                if -2<s<2:roots.append(mp.acos(s/2))
    cuts=sorted(set([mp.mpf(0),mp.pi,2*mp.pi/3]+roots))
    def integrand(theta):
        x=mp.exp(mp.j*theta)
        a=x*x+x+1
        b=x**4+k*x**3+(2*k-4)*x*x+k*x+1
        c=x*x*a
        disc=mp.sqrt(b*b-4*a*c)
        r1=(-b+disc)/(2*a);r2=(-b-disc)/(2*a)
        return mp.log(max(1,abs(r1)))+mp.log(max(1,abs(r2)))
    total=mp.mpf(0)
    for left,right in zip(cuts,cuts[1:]):
        if right-left<mp.eps*100:continue
        # An independent cubic endpoint map, rather than sin^2.
        def h(t):
            if t==0 or t==1:return mp.mpf(0)
            u=t*t*(3-2*t)
            return integrand(left+(right-left)*u)*(right-left)*6*t*(1-t)
        total+=mp.quad(h,[0,mp.mpf('0.5'),1])
    return total/mp.pi


def symbolic():
    R=PolynomialRing(QQ,names=('x','y','k'));x,y,k=R.gens()
    A=x*x+x+1;Q=A*y*y+(x**4+k*x**3+(2*k-4)*x*x+k*x+1)*y+x*x*A
    assert Q(k=5)==A*(y*y+(x*x+4*x+1)*y+x*x)
    # Multiplying transformed a*y^2+b*y+a by x^3 after y->y/x
    # gives exactly the original polynomial.
    z=y/x
    transformed=(x+1/x+1)*z*z+(x*x+1/(x*x)+k*(x+1/x)+2*k-4)*z+(x+1/x+1)
    assert Q==x**3*transformed
    S=PolynomialRing(QQ,names=('s','v'));s,v=S.gens()
    b=s*s+v*s+2*v-6
    assert b*b-4*(s+1)**2==(s+2)*(s+v-4)*(s*s+(v+2)*s+2*v-4)
    return True


def compare(record,value):
    center=mp.mpf(record['number']);last=mp.power(10,mp.floor(mp.log10(abs(center)))-99)
    return bool(mp.isfinite(value) and abs(value-center)<=last),abs(value-center)/last


def run(path='entries.json'):
    records=json.loads(Path(path).read_text());rows=[];failures=[]
    symbolic();print('SYMBOLIC passed',flush=True)
    with mp.workdps(135):
        control=4*mp.catalan/mp.pi
        q5=next(r for r in records if r['params']['k']=='5')
        known=compare(q5,control)[0]
        for record in records:
            t=time.time();k=exact_input(record['params']['k'])
            value=control if k==5 else direct_measure(k)
            ok,ulps=compare(record,value)
            row={'k':record['params']['k'],'value':mp.nstr(value,130),'ulps':mp.nstr(ulps,12),'pass':ok,'seconds':time.time()-t}
            rows.append(row)
            if not ok:failures.append(row)
            print('CHECK '+json.dumps(row),flush=True)
        wrong=dict(next(r for r in records if r['params']['k']=='2'))
        wrong['number']=mp.nstr(mp.mpf(wrong['number'])+mp.mpf('1e-90'),110)
        baseline=mp.mpf(next(r['value'] for r in rows if r['k']=='2'))
        negative=not compare(wrong,baseline)[0]
    result={'checked_entries':len(rows),'total_entries':len(records),'failures':failures,'known_answer_control':known,'negative_control_rejected':negative,'symbolic_checks':True,'working_dps':135,'rows':rows,'limitations':'Heuristic quadrature; Jensen theorem and mpmath shared, original complex coefficients and root selection separately implemented.'}
    print('RESULT '+json.dumps(result),flush=True)
    if failures or not known or not negative:raise SystemExit(1)
    return result


if __name__=='__main__':run()