"""Symbolic projection budgets and an explicit four-plane coincidence on pair BC."""
from itertools import product,combinations
from fractions import Fraction as F
from contacts27 import TRIPLE
import sympy as S,json
t=S.symbols('t')
def planes(laws,u,v):
    for row in laws:
        lower=[(0,0,-1,0,0)];upper=[(0,0,1,0,1)]
        for i,j,k,l in product((0,1),repeat=4):
            a,b,c=i-j,l-k,j-l;d,h=l-k,sum(x*y for x,y in zip((i,j,k,l),row))+t-l
            sign=a*u+b*v+c
            if sign<0:lower.append((a,b,c,d,h))
            elif sign>0:upper.append((a,b,c,d,h))
            elif d or h!=0:yield (0,d,0,h)
        for a,b,c,d,h in lower:
            for A,B,C,D,H in upper:
                yield tuple(map(S.expand,(h*A-H*a,d*C+h*B-D*c-H*b,d*A-D*a,h*C-H*c)))
def study():
    e=S.Rational(79,567);point=S.Matrix([S.Rational(1,10),S.Rational(10,27),S.Rational(1,27),1])
    hs=list(dict.fromkeys(h for h in planes(TRIPLE[1:],F(1,4),F(1,2)) if any(h[:3]) and (S.Matrix([h])*point)[0].subs(t,e)==0))
    event=None
    for group in combinations(hs,4):
        mat=S.Matrix(group);res=S.factor(mat.det())
        if not res or res.subs(t,e)!=0:continue
        ns=mat[:,:3].subs(t,e).T.nullspace()
        if len(ns)!=1 or not all(z>0 for z in ns[0]):continue
        # Strictly positive dependence forces four equalities at the threshold.
        comb=(ns[0].T*mat.subs(t,e));assert comb==S.zeros(1,4)
        event=dict(planes=[[str(z) for z in h] for h in group],positive=list(map(str,ns[0])),det=str(res));break
    assert event
    # Synthetic persistent contact: unit cube + -u,-v,-w,u+t-r >= 0.
    # Here G at the only possible vertex vanishes identically.
    r=S.Rational(2,7)
    X=S.symbols('X');orientation=S.factor(X**2-2*X-1,extension=S.sqrt(2))
    return dict(BC=event,persistent=dict(point=[0,0,0],slack=str(t-r),threshold=str(r)),
                orientation=str(orientation),degree=dict(pivot=1,tangent=4,order=4,endpoint=5,vertical=5))
if __name__=='__main__':print(json.dumps(study()))
