"""Algebra controls for812; no certification of analytic asymptotics."""
from pathlib import Path
import json
import sympy as S
x,h,r,t,d,e,lam=S.symbols('x h r t d e lam')
f=3*x*x*h+3*x*h*h+h**3-r*h-d
g=3*x*x*lam*h+3*x*lam**2*h*h+lam**3*h**3-t*lam*h-e
q=S.Poly(S.expand(g-lam*f),x,h)
assert q.coeff_monomial(x*h*h)==3*lam*(lam-1)
assert q.coeff_monomial(x*x*h)==0
assert S.expand((g-lam*f).subs(lam,1))==d-e+h*r-h*t
z=S.symbols('z')
D=lambda rr,dd:4*rr**3-27*(x**3-rr*x+dd)**2
assert S.Poly(D(z,e),z).degree()==3
assert S.Poly(D(z,e),z).LC()==4
controls=[]
for rr in (S.Rational(1),S.Rational(2,3),S.Rational(-2)):
 for dd in (1,2):
  for ee in (1,2,-1,-2):
   R=S.Poly(S.resultant(D(rr,dd),D(z,ee),x),z)
   assert R.degree()==18 and R.LC()==108**6
   controls.append({'r':str(rr),'d':dd,'e':ee,'degree':18,'leading':str(R.LC())})
assert 12+2*14*3==96 and 96*(6+28)==3264
out={'status':'PASS','scope':'polynomial identities and bounded-partner arithmetic only; no analytic audit',
     'coincidence_difference':str(S.factor(g-lam*f)),'resultant_controls':controls,
     'anchor_locus_envelope':96,'partner_envelope':3264}
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2)+'\n')
print(json.dumps({k:v for k,v in out.items() if k!='resultant_controls'},indent=2))
print('resultant controls:',len(controls))
