"""Stress-test the Euler lemma and independently count binary roots from Duursma's identity. Run with: uv run --with python-flint==0.9.0 audit_binary.py """ from math import comb from flint import fmpq, fmpq_mat, fmpq_poly, fmpz_poly def extremal_coefficients(kind, n): """Solve the weight-gap equations in the binary Gleason basis, with Y=y^b.""" y = fmpz_poly([0, 1]) if kind == "I": b, step, period = 2, 2, 8 first, second = 1 + y, y * (1-y)**2 else: b, step, period = 4, 8, 24 first, second = 1 + 14*y + y**2, y * (1-y)**4 k = n // period basis = [first**((n-period*j)//step) * second**j for j in range(k+1)] matrix = fmpq_mat([[poly[i] for poly in basis] for i in range(k+1)]) target = fmpq_mat([[1]] + [[0]]*k) weights = matrix.solve(target) w = sum(weights[j, 0] * fmpq_poly(poly) for j, poly in enumerate(basis)) assert w[0] == 1 and all(w[j] == 0 for j in range(1, k+1)) and w[k+1] != 0 return b, b*(k+1), w def zeta_from_weight_equations(kind, n): """Invert the triangular coefficient equations for U=P/(1-2T).""" b, d, w = extremal_coefficients(kind, n) u = [] for i in range(n-d+1): weight = d + i a = w[weight//b] / comb(n, weight) if weight % b == 0 else 0 # a = [T^i] (1-T)^(d+i-1) U(T). earlier = sum((-1)**(i-j) * comb(weight-1, i-j) * c for j, c in enumerate(u)) u.append(a - earlier) p = (fmpq_poly(u) * fmpq_poly([1, -2])).truncate(n-d+1) assert p.degree() == n+2-2*d and p[0] != 0 return p def trace_polynomial(p): """Write P(T)=T^g H(2T+1/T), using the Dickson polynomial recurrence.""" g = p.degree() // 2 assert all(p[g+j] == 2**j * p[g-j] for j in range(g+1)) x = fmpq_poly([0, 1]) h = fmpq_poly([p[g]]) previous, current = fmpq_poly([2]), x for j in range(1, g+1): h += p[g-j] * current previous, current = current, x*current - 2*previous assert h.degree() == g return h def sturm_count(h, radius): """Count distinct zeros in (-radius,radius) by rational signed remainders.""" left, right = [], [] assert h(-radius) and h(radius) p, q = h, h.derivative() while p: for point, signs in ((-radius, left), (radius, right)): value = p(point) if value: signs.append(1 if value > 0 else -1) if not q: break r = -(p % q) # A positive rescaling preserves the signs and limits coefficient growth. if r: numer = r.numer() r = fmpq_poly(numer // numer.content()) p, q = q, r def variations(signs): return sum(a != b for a, b in zip(signs, signs[1:])) return variations(left) - variations(right) def check_euler_boundaries(): # Substitute x=sqrt(2)*t so the numerator computation is rational. t = fmpq_poly([0, 1]) denominator = 1 - 2*t + 2*t**2 def apply(p, s, a): return (t*p.derivative() + a*p)*denominator - s*t*p*denominator.derivative() def all_roots_real(p): distinct = p // p.gcd(p.derivative()) radius = 2 + sum(abs(c) for c in distinct.coeffs()) / abs(distinct.leading_coefficient()) return sturm_count(distinct, radius) == distinct.degree() # Repeated roots on both half-lines and at zero; s=4 gives decay one. p = t**2 * (t-1)**3 * (t+2)**2 for a in (fmpq(1, 2), 1): result = apply(p, 4, a) assert result[0] == result[1] == 0 and result[2] != 0 and all_roots_real(result) # Three consecutive equality cases. The missing fourth Laurent term # causes the last decay order to jump from three to five. p = t for a, next_decay in ((1, 2), (2, 3), (3, 5)): assert 2*a - p.degree() == a p = apply(p, a, a) assert 2*(a+1) - p.degree() == next_decay and all_roots_real(p) # Exceeding the decay bound really can produce a nonreal pair. outside = apply(fmpq_poly([1]), 1, 3) assert outside == 2*(t-1)**2 + 1 and sturm_count(outside, 4) == 0 print("Euler stress tests passed at repeated roots, degree drops, and an out-of-range shift.") def main(): check_euler_boundaries() # This rational interval lies strictly inside the trace of the critical circle. radius = fmpq(2828427, 1000000) assert radius**2 < 8 for kind, step, period in (("I", 2, 8), ("II", 8, 24)): cases = zeros = 0 for n in range(step, 9*period, step): p = zeta_from_weight_equations(kind, n) h = trace_polynomial(p) assert sturm_count(h, radius) == h.degree(), (kind, n) cases += 1 zeros += p.degree() print(f"Type {kind}: {cases} lengths through {n}, {zeros} zeta zeros certified.") if __name__ == "__main__": main()