checks.py

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

6087 bytes, as of the version from 2026-11-18 20:44 (current). Recorded here, not run.

"""T760 builder checks, independent of the triangle/dilogarithm formula.

Run with Python + mpmath (or sage -python checks.py). Input is stored.json,
otherwise computed.json, beside this file. To choose a file pass its path.
Each CHECK row is a resumable checkpoint; RESULTS is the complete JSON record.
The controller must still obtain a fresh independent scientific critique.

Jensen in y gives the circle average of log(max(c,|a+b*exp(i*theta)|)).
On 0<=theta<=theta0, theta0=acos((c^2-a^2-b^2)/(2ab)), the maximum is
|a+b*exp(i*theta)|; on the rest it is c. Symmetry gives the smooth integral
used here. Mapping theta=theta0*t fixes the integration domain. mpmath
quadrature at 145 and 180 decimal digits is agreement evidence, not a certified
error bound; the independently coded production Arb formula supplies bounds.
No production formula or function is imported for the row comparisons.
"""
import json,sys,math,hashlib,platform
from pathlib import Path
from decimal import Decimal
from fractions import Fraction
import mpmath as mp


def jensen(a,b,c,dps):
    with mp.workdps(dps):
        if c >= a+b:
            return +mp.log(c)
        t0=mp.acos((mp.mpf(c*c)-a*a-b*b)/(2*a*b))
        def integrand(t):
            return mp.log(a*a+b*b+2*a*b*mp.cos(t0*t))
        val=t0*mp.quad(integrand,[0,mp.mpf(1)/2,1])/(2*mp.pi)+(1-t0/mp.pi)*mp.log(c)
        return +val


def decimal_interval(s):
    d=Decimal(str(s));assert d.is_finite()
    center=Fraction(d);unit=Fraction(10)**d.as_tuple().exponent
    return center,unit


def mpq(q):
    return mp.mpf(q.numerator)/q.denominator


def accepts(center,radius,expected):
    if not all(mp.isfinite(x) for x in (center,radius,expected)):return False
    if radius <= 0 or radius > max(1,abs(expected))*mp.mpf('1e-95'):return False
    return abs(center-expected)<=radius


def rows_of(data):
    if isinstance(data,list):return data
    block=data['Numbers']
    if isinstance(block,list):return [{'sides':r['params']['sides'],'number':r['number']} for r in block]
    return [{'sides':k,'number':v['number'] if isinstance(v,dict) else v} for k,v in block.items() if not k.startswith('_')]


def expected_selection():
    iso=set();right=set()
    for c in range(1,101):
        for b in range(1,c+1):
            for a in range(1,b+1):
                if a+b <= c or math.gcd(math.gcd(a,b),c)!=1:continue
                if c<=12 and (a==b or b==c):iso.add((a,b,c))
                if a*a+b*b==c*c:right.add((a,b,c))
    return iso,right


def main():
    root=Path(__file__).resolve().parent
    src=Path(sys.argv[1]) if len(sys.argv)>1 else root/('stored.json' if (root/'stored.json').exists() else 'computed.json')
    raw=src.read_bytes();rows=rows_of(json.loads(raw));mp.mp.dps=180
    iso,right=expected_selection();expected=iso|right
    identities=[tuple(map(int,r['sides'].split('-'))) for r in rows]
    assert len(identities)==len(set(identities))==84 and set(identities)==expected
    assert len(iso)==68 and len(right)==16 and not iso&right
    assert identities==sorted(expected,key=lambda t:(t[2],t[1],t[0]))
    failures=[];comp=[]
    for row,t in zip(rows,identities):
        a,b,c=t;lo=jensen(a,b,c,145);hi=jensen(a,b,c,180)
        center,ulp=decimal_interval(row['number']);center,ulp=mpq(center),mpq(ulp)
        stable=abs(lo-hi)<max(1,abs(hi))*mp.mpf('1e-138')
        passed=stable and accepts(center,ulp,hi)
        if not passed:failures.append(row['sides'])
        record={'sides':row['sides'],'passed':bool(passed),'jensen_180':mp.nstr(hi,160),'difference_ulps':mp.nstr(abs(center-hi)/ulp,12),'precision_difference':mp.nstr(abs(lo-hi),8)}
        comp.append(record);print('CHECK '+json.dumps(record),flush=True)
    polynomial_control=abs(mp.quad(lambda t:t*t,[0,1])-mp.mpf(1)/3)<mp.mpf('1e-170')
    anchor=jensen(1,1,1,180)
    lvalue=3*mp.sqrt(3)*mp.dirichlet(2,[0,1,-1])/(4*mp.pi)
    known=polynomial_control and abs(anchor-lvalue)<mp.mpf('1e-170') and abs(anchor)>mp.mpf('0.3')
    boundary=abs(jensen(1,1,2,180)-mp.log(2))<mp.mpf('1e-170') and abs(jensen(1,1,3,180)-mp.log(3))<mp.mpf('1e-170')
    scale=abs(jensen(6,8,10,180)-jensen(3,4,5,180)-mp.log(2))<mp.mpf('1e-170')
    permutation=abs(jensen(4,3,5,180)-jensen(3,4,5,180))<mp.mpf('1e-170')
    a,b,c=3,4,5;z=mp.mpc(0,mp.mpf(3)/4)
    D=mp.im(mp.polylog(2,z))+mp.arg(1-z)*mp.log(mp.mpf(3)/4)
    alpha=mp.acos(mp.mpf(4)/5);beta=mp.acos(mp.mpf(3)/5);gamma=mp.pi/2
    bad_conjugation=(-D+alpha*mp.log(3)+beta*mp.log(4)+gamma*mp.log(5))/mp.pi
    expected345=jensen(3,4,5,180)
    controls={
      'wrong_value':not accepts(anchor+mp.mpf('1e-50'),mp.mpf('1e-99'),anchor),
      'omitted_scale':not accepts(expected345,mp.mpf('1e-99'),jensen(6,8,10,180)),
      'conjugated_shape':not accepts(bad_conjugation,mp.mpf('1e-99'),expected345),
      'nan':not accepts(mp.nan,mp.mpf('1e-99'),anchor),
      'infinite':not accepts(mp.inf,mp.mpf('1e-99'),anchor),
      'vacuous_ball':not accepts(anchor,mp.mpf('1e10'),anchor)}
    if not all((known,boundary,scale,permutation,all(controls.values()))):failures.append('checker control')
    results={'checked_entries':len(comp),'total_entries':len(rows),'failures':failures,'known_answer_control':bool(known),'negative_control_rejected':bool(all(controls.values())),
      'controls':{'polynomial_integral':bool(polynomial_control),'smyth_L_value_difference':mp.nstr(abs(anchor-lvalue),8),'boundary_and_nontriangle':bool(boundary),'scale':bool(scale),'permutation':bool(permutation),'negative':controls},
      'enumeration':{'isotropic':1,'other_isosceles':67,'primitive_right':16,'independent_bruteforce':True},
      'method':'Jensen one-circle smooth finite integral, mpmath tanh-sinh quadrature at 145 and 180 decimal digits; independent of production triangle/dilogarithm formula; heuristic quadrature errors.',
      'input':src.name,'input_sha256':hashlib.sha256(raw).hexdigest(),'versions':{'python':platform.python_version(),'mpmath':mp.__version__},'comparisons':comp}
    print('RESULTS '+json.dumps(results),flush=True)
    assert not failures


if __name__=='__main__':main()