"""Enumerate implicit equations with a quartic earlier coordinate and two triple grids.""" from fractions import Fraction from functools import cache from itertools import combinations_with_replacement, product import sympy as sp marks=(-1,0,1) x,y,z=sp.symbols('x y z') def multisets(d): return list(combinations_with_replacement(marks,d)) def moment(r): return sum(a*a for a in r) def columns(deg,exc): full=multisets(deg) for s in range(-deg,deg+1): pool=[r for r in full if sum(r)==s] choices=[pool.copy() for _ in marks] if exc is not None: part=[] for e in range(1,deg+1): for r in multisets(deg-e): q=Fraction(s-sum(r),e) if q not in marks: part.append(tuple(sorted(r+(q,)*e))) choices[exc]=part for table in product(*choices): if moment(table[0])+moment(table[2])==2*moment(table[1]): yield table def rows(early,exc): full=multisets(early) if exc is None: for tab in product(full,repeat=3): if sum(tab[0])+sum(tab[2])==2*sum(tab[1]): yield tab return good=[i for i in range(3) if i!=exc] for r,t in product(full,repeat=2): slope=Fraction(sum(t)-sum(r),good[1]-good[0]) target=sum(r)+(exc-good[0])*slope for e in range(1,early+1): for part in multisets(early-e): q=Fraction(target-sum(part),e) if q not in marks: tab=[None]*3 tab[good[0]],tab[good[1]],tab[exc]=r,t,tuple(sorted(part+(q,)*e)) yield tuple(tab) def ev(r,t): import math return math.prod(t-q for q in r) def polys(table,root): return [sp.prod(root-sp.Rational(q) for q in row) for row in table] def interp(values,var): cm,c0,cp=values return c0+(cp-cm)*var/2+(cm+cp-2*c0)*var**2/2 @cache def column_polynomial(table): return sp.expand(interp(polys(table,x),y)) @cache def row_remainder(table): plane=sp.expand(interp(polys(table,y),x)) return sp.expand(plane-interp([plane.subs(y,j) for j in marks],y)) def enumerate_curves(early,deg): fixed=set() pencils=set() for er,ec in [(None,None)]+[(i,None) for i in range(3)]+[(None,i) for i in range(3)]: from collections import defaultdict rmap=defaultdict(list) for tab in rows(early,er): signature=tuple(marks[j] in tab[i] for i,j in product(range(3),repeat=2)) vals=tuple(ev(tab[i],marks[j]) for i,j in product(range(3),repeat=2)) rmap[signature].append((tab,vals)) for cols in columns(deg,ec): if any(marks[i] not in set().union(*map(set,cols)) for i in range(3)) and er is None: continue sig=tuple(marks[i] in cols[j] for i,j in product(range(3),repeat=2)) cv=tuple(ev(cols[j],marks[i]) for i,j in product(range(3),repeat=2)) for rs,rv in rmap[sig]: ratios={Fraction(u,v) for u,v in zip(cv,rv) if v} double=(early,deg)==(4,6) if len(ratios)>1 or (not double and any(u>=0 for u in ratios)): continue a=sp.Rational(ratios.pop()) if ratios else z itp=column_polynomial(cols) if double: cross=itp.coeff(x,3).coeff(y,2) if cross>=0 or (a!=z and a!=cross**2/4): continue a=cross**2/4 f=sp.expand(itp+a*row_remainder(rs)) (fixed if a!=z else pencils).add(f) return sorted(fixed,key=sp.default_sort_key),sorted(pencils,key=sp.default_sort_key) def odd_discriminant(f): disc=sp.discriminant(f,y) return sp.prod(q for q,n in sp.factor_list(disc)[1] if n%2 and q.has(x)) def special_values(f,early): odd=odd_discriminant(f) assert sp.degree(odd,x)>early-1 bad=sp.Poly(odd,x).LC()*sp.discriminant(odd,x) return [q for q in sp.polys.polytools.ground_roots(bad,z) if q<0] if __name__=='__main__': for early,d,expected in ((4,5,(46,102)),(4,6,(15,0))): fixed,pencils=enumerate_curves(early,d) assert (len(fixed),len(pencils))==expected print((early,d),'fixed',len(fixed),'pencils',len(pencils),flush=True) for f in fixed: for c,n in sp.factor_list(f)[1]: if sp.degree(c,y)!=early or sp.degree(c,x)!=d: continue odd=odd_discriminant(c) assert sp.degree(odd,x)>early-1, f for f in pencils: if not f.coeff(z,1).has(x) and not f.coeff(z,0).has(y): continue for val in special_values(f,early): g=f.subs(z,val) assert sp.degree(odd_discriminant(g),x)>early-1, (val,f)