"""Exact small-imbalance exclusion in both orientations and capacity cases. Uses the bundled boundary_base.py for shared rational matrix helpers. Run this file directly or use verify_global.py for the full upper bound. """ from fractions import Fraction as F from pathlib import Path import json,importlib.util ROOT=Path(__file__).resolve().parent helper=ROOT/'boundary_base.py' spec=importlib.util.spec_from_file_location('boundary_base',helper);v=importlib.util.module_from_spec(spec);spec.loader.exec_module(v) forms=[(n,f)for n,f in v.forms if n not in ['h-.3q','q-2w']]+[('.3m-h',v.V((F(3,10),v.q),(-1,v.h)))] def build(orient,capacity): assert orient in ['AB','CD'] and capacity in ['r_le_2w','r_ge_2w'] actualq=v.q if orient=='AB'else v.d t=v.r if capacity=='r_le_2w'else v.V((2,v.w)) fs=forms+[(capacity,v.V((2,v.w),(-1,v.r))if capacity=='r_le_2w'else v.V((1,v.r),(-2,v.w)))] g=v.A((1,v.sy(t,v.V((F(3,2),actualq),(-1,v.r),(-1,v.z),(2,v.y)))),(F(-7,8),v.out(t,t)),(F(1,2),v.out(actualq,actualq))) ts=[v.A((-1,v.K),(-1,v.fR)),v.A((-3,v.K),(-1,g)),v.A((-1,v.NN))]+[v.A((-1,v.sy(v.ell,f)))for n,f in fs]+[v.A((-1,v.out(v.ell,v.ell)))] return v.A((1,v.base),(1,v.K)),ts def check(row): base,terms=build(row['orientation'],row['capacity']);a=list(map(F,row['parameters']));br,bt=a[:2];p=1-br-bt assert len(a)==len(terms)and all(u>=0 for u in a) assert F(5,8)*br>=3*bt and p>=5*bt and p>=2*bt and p+bt>=F(3,2)*br assert a[-1]==1-2*F(row['gamma']) piv=v.ldl(v.A((1,base),*[(u,t)for u,t in zip(a,terms)])) print('PASS',row['orientation'],row['capacity'],'small imbalance gamma <=',row['gamma'],'weights',p,br,bt,'minimum LDL',float(min(piv))) return F(row['gamma']) if __name__=='__main__': data=json.loads((ROOT/'small-imbalance-certificate.json').read_text()) assert len(data['cases'])==4 and {(r['orientation'],r['capacity'])for r in data['cases']}=={(o,c)for o in ['AB','CD']for c in ['r_le_2w','r_ge_2w']} ans=[check(r)for r in data['cases']];print('PASS h<=.3m gamma <=',max(ans),float(max(ans)))