"""Exact half-sum, reflection, disk-count and central-residue checks. Python + SymPy.""" from functools import cache from math import isqrt import sympy as s t = s.Symbol("t") Q = s.Rational def poly(f): return s.Poly(f, t, domain=s.QQ) def disk_count(p): """Schur recursion. Strict comparison propagates absence of boundary roots.""" if p.degree() == 0: assert p.LC() return 0 p = p.clear_denoms(convert=True)[1].primitive()[1] a, b = p.LC(), p.TC() rev = s.Poly.from_list(p.all_coeffs()[::-1], t) if abs(a) == abs(b): raise ArithmeticError("Degenerate Schur step; use a different exact disk test.") if abs(a) < abs(b): return p.degree() - disk_count(rev) return 1 + disk_count((a*p-b*rev).exquo(s.Poly(t,t))) def check(q, P, n, bracket=None): q = Q(q) g = P.degree()//2 h, m = g-1, n-1 radius = Q(1, isqrt(int(q**n))) assert radius**2*q**n == 1 v = [1, P.eval(1)/(q-1)] for j in range(2, n): y = q**(-j) v.append(v[-1]*q**(h*j)*P.eval(y)/((1-y)*(1-q*y))) @cache def rows(j): if not j: return {0:Q(1)} return {a:v[a]*sum(w/(1-q**(a+b)) if b else w for b,w in rows(j-a).items()) for a in range(1,j+1)} factors = [poly(1-q**j*t) for j in range(n+1)] common = s.prod(factors) partial, full = poly(0), poly(0) for k in range(n): pk = P.compose(poly(q**k*t)) for a, u in rows(k).items(): for b, w in rows(m-k).items(): denom = factors[k]*factors[k+1] mult = poly(q**(-h*k)*u*w) if a: denom *= factors[k-a] mult *= poly(-q**(k-a)*t) if b: denom *= factors[k+b+1] term = common.exquo(denom)*pk*mult full += term if 2*k < m: partial += term elif 2*k == m: partial += term*Q(1,2) U = partial.compose(poly(radius*t)) cleared = full.compose(poly(radius*t)) degree = 2*g+n-1 reflected = s.Poly.from_list([U.nth(i) for i in range(degree+1)],t,domain=s.QQ) assert cleared == U+(-1)**(n+1)*reflected F = full.exquo(s.prod(factors[1:-1])).compose(poly(radius*t)) assert F.degree() == 2*g and F.TC() > 0 d = [poly(2),poly(t)] for j in range(2,g+1): d.append(poly(t)*d[-1]-d[-2]) trace = poly(F.nth(g))+sum((F.nth(g+j)*d[j] for j in range(1,g+1)),poly(0)) assert trace.eval(-2)*trace.eval(2) and trace.count_roots(-2,2)==g assert disk_count(U) == m//2 if n%2==0: j=n//2 central = q**(-h*j)*sum(x*y/(1-q**(a+b-1)) for a,x in rows(j).items() for b,y in rows(j).items()) assert central < 0 assert U.eval(1) == central*s.prod(1-q**(a-j) for a in range(n+1) if a!=j) if bracket is not None: low, high = (1+offset for offset in bracket) assert U.eval(low)*U.eval(high)<0 and U.count_roots(low, high)==1 residue = (1-radius)*(1-q**j)*central/sum(rows(m).values()) return residue, U if __name__ == "__main__": if not __debug__: raise RuntimeError("Run without -O so the checks are enabled.") real_pair = (1-3*t)*(1-Q(4,3)*t) quartet = (1+9*t*t)*(1+Q(16,9)*t*t) for name, P in [ ("real pairs",poly(real_pair*(1+3*t)*(1+Q(4,3)*t))), ("imaginary quartet",poly(quartet)), ("mixed genus three",poly(quartet*real_pair)), ("near boundary",poly((1-Q(399,100)*t)**2*(1-Q(400,399)*t)**2)) ]: for n in range(2,11): check(4,P,n) print(name, "ranks 2 through 10 pass") P = poly(quartet*real_pair) for n, k, lo, hi in [(10,28,13,14), (12,41,31,32)]: check(4, P, n, (Q(lo,10**k), Q(hi,10**k))) print(f"n={n}: exactly one half-sum root with {lo}e-{k} < z-1 < {hi}e-{k}")