"""Standalone exact checker: reconstructs every graph constraint and polynomial. Uses standard library only; the JSON stores nonnegative-product witnesses. """ from fractions import Fraction as F from itertools import combinations_with_replacement from pathlib import Path import json CERT=json.loads(Path(__file__).with_name("singlequad-22-outside-certificate.json").read_text()) LOOKUP={(r["F"],r["eps"]):r for r in CERT} assert len(CERT)==len(LOOKUP)==14 def rref(A): M=[[F(x)for x in row]for row in A];piv=[];i=0 for j in range(len(M[0])-1): k=next((k for k in range(i,len(M))if M[k][j]),None) if k is None:continue M[k],M[i]=M[i],M[k];q=M[i][j];M[i]=[x/q for x in M[i]] for k in range(len(M)): if k!=i: q=M[k][j];M[k]=[a-q*b for a,b in zip(M[k],M[i])] piv.append(j);i+=1 if i==len(M):break return M,piv def solve(A,b): M,piv=rref([list(row)+[v]for row,v in zip(A,b)]);n=len(A[0]);assert len(piv)==n and all(any(r[:-1])or not r[-1]for r in M) v=[F(0)]*n for row,j in zip(M,piv):v[j]=row[-1] assert all(sum(a*x for a,x in zip(row,v))==z for row,z in zip(A,b));return v Fs=[('empty',[]),('edge',[(0,1)]),('path2',[(0,1),(1,2)]),('matching',[(0,1),(2,3)]),('path3',[(0,1),(1,2),(2,3)]),('triangle',[(0,1),(1,2),(0,2)]),('cycle',[(0,1),(1,2),(2,3),(0,3)])] records=[] for name,fe in Fs: for eps in [0,1]: # Variables c,r,s,t0,t1,t2,t3,theta; normalized boundary b=1. n=8;one=[F(1)]+[F(0)]*n def var(j):return[F(0)]+[F(i==j)for i in range(n)] def add(*ls):return[sum(x)for x in zip(*ls)] def sc(a,l):return[a*x for x in l] c,r,s=var(0),var(1),var(2);ts=[var(i)for i in range(3,7)];th=var(7);z=add(sc(3,one),sc(-1,th),sc(-1,c));W=add(*ts) eq=[add(sc(6,c),sc(-4,one),sc(-1,r),sc(-1,s),sc(-1,th)),add(sc(6,z),sc(-2,one),sc(-1,W),sc(-1,th))] ls=[th,add(c,sc(-1,one)),add(z,sc(-1,c))]+[add(y,sc(-1,z))for y in[r,s]+ts] deg=[sum(i in e for e in fe)for i in range(4)] loads=[1-eps,1-eps]+[2-d for d in deg];q=sum(loads) rows=[add(sc(6,r),sc(-1,c),sc(-1,W),sc(-eps,s),sc(-1,th)),add(sc(6,s),sc(-1,c),sc(-1,W),sc(-eps,r),sc(-1,th))] for i,t in enumerate(ts): row=add(sc(6,t),sc(-2,z),sc(-1,r),sc(-1,s),sc(-1,th)) for a,b in fe: if i==a:row=add(row,sc(-1,ts[b])) if i==b:row=add(row,sc(-1,ts[a])) rows.append(row) for row,l in zip(rows,loads): if l:ls.extend(add(row,sc(-l,y))for y in[r,s]+ts) else:eq.append(row) hmin=0 if q==0 else next(h for h in range(1,8)if q<=6*h and h*(7-h)<=q) if q:ls.append(add(sc(F(4,13+hmin),one),sc(-1,th))) else:eq.append(add(th,sc(F(-4,13),one))) rr,piv=rref([l[1:]+[-l[0]]for l in eq]);free=[i for i in range(n)if i not in piv];d=len(free) trans=[[F(0)]*(d+1)for _ in range(n)] for j,f in enumerate(free):trans[f][j+1]=1 for row,j in zip(rr,piv):trans[j]=[row[-1]]+[-row[f]for f in free] def sub(l):return[l[0]+sum(l[i+1]*trans[i][0]for i in range(n))]+[sum(l[i+1]*trans[i][j+1]for i in range(n))for j in range(d)] assert all(not any(sub(l)) for l in eq) inequalities=list(dict.fromkeys(tuple(sub(l))for l in ls));assert all(l[0]>=0 for l in inequalities if not any(l[1:]));inequalities=[l for l in inequalities if any(l[1:])] mon=[(i,j)for i in range(d+1)for j in range(i,d+1)] def prod(a,b):return[a[i]*b[j]+(a[j]*b[i]if i!=j else 0)for i,j in mon] def padd(*ps):return[sum(t)for t in zip(*ps)] def psc(a,p):return[a*x for x in p] bnd=sub(one);theta=sub(th);cc,rrr,sss=sub(c),sub(r),sub(s);zz=sub(z);tt=[sub(t)for t in ts] cap=[bnd]*4+[cc,zz,zz,rrr,sss]+tt edges={(i,j)for i in range(4)for j in range(i+1,4)}|{(i,4)for i in range(4)}|{(0,5),(1,5),(2,6),(3,6),(4,7),(4,8)}|{(i,j)for i in (5,6,7,8)for j in range(9,13)}|{(9+a,9+b)for a,b in fe} if eps:edges.add((7,8)) L=padd(*(psc(6,prod(v,v))for v in cap),*(psc(-2,prod(cap[i],cap[j]))for i,j in edges)) if q==0: assert d==0 # tau=sum old core values = b*sum(cap), and b=13/4. bval=F(13,4);tau=bval*sum(v[0]for v in cap);m=14 P4=F(m**3,24)-F(3*m*m,16)+F(211*m,336) margin=F(-19,14)-(tau-P4) print(name,eps,'closed head margin',margin,flush=True);assert margin>0 records.append(dict(F=name,eps=eps,q=q,closed=True,score_margin=str(margin)));continue k=4 if q in(2,8,10) else 2 S=[sum(l*v[j]for l,v in zip(loads,[rrr,sss]+tt))for j in range(d+1)] source=[4*bnd[j]-7*theta[j]for j in range(d+1)] off=psc(14 if k==4 else 10,prod(bnd,bnd)) off=padd(off,psc(-28 if k==4 else -15,prod(bnd,theta)),psc(20 if k==4 else F(19,2),prod(theta,theta))) G=padd(psc(-1,L),psc(F(1,q),prod(S,S)),psc(F(1,k),prod(source,source)),off) all_ls=[tuple(bnd)]+inequalities atoms=[prod(a,b)for a,b in combinations_with_replacement(all_ls,2)];atomlabels=list(combinations_with_replacement(range(len(all_ls)),2)) rec=LOOKUP[(name,eps)] assert rec['q']==q and rec['new_root_degree']==k assert rec['dimension']==d and rec['free']==free assert [[F(x)for x in row]for row in rec['transform']]==trans assert [[F(x)for x in row]for row in rec['inequalities']]==[list(l)for l in all_ls] assert list(map(F,rec['gain']))==G result=[F(0)]*len(mon) for term in rec['products']: a,b=term['factors'];coefficient=F(term['coefficient']);assert coefficient>=0 value=prod(all_ls[a],all_ls[b]) result=[x+coefficient*y for x,y in zip(result,value)] result[0]+=F(rec['margin']) assert F(rec['margin'])==F(1,100000) and result==G # The proposed six-vertex replacement realizes balanced port loads. new_edges={(i,j)for i in range(6)for j in range(i+1,6)} if q==10:new_edges-={(0,1),(1,2),(2,3),(0,3)};roots={0,1,2,3} elif q==8:new_edges-={(0,1),(1,2),(2,3)};roots={0,1,2,3} elif q==6:new_edges.remove((0,1));roots={0,1} elif q==4:roots={0,1} else:assert q==2;roots={0,1,2,3} portloads=[6-sum(i in e for e in new_edges)-int(i in roots)for i in range(6)] assert sum(portloads)==q and max(portloads)-min(portloads)<=1 and len(roots)==k print('PASS',name,eps,'q',q,'dimension',d,flush=True) records.append(rec) assert len(records)==14 print('ALL14 EXACT CERTIFICATES PASS; nonclosed margin1/100000, closed margin7719/280',flush=True)