"""Verify the circle theorem at every length allowed by Zhang's bounds. Run with: uv run --with python-flint==0.9.0 verify.py """ from collections import Counter from math import comb from flint import acb, arb, ctx, fmpq, fmpq_poly, fmpz_poly X = fmpz_poly([0, 1]) # Each row gives q, weight divisor b, degree of the first generator, # degree ratio of the generators, their specializations at x=1 in Y=y^b, # and Zhang's first excluded period index in successive length classes. TYPES = { "I": (2, 2, 2, 4, 1 + X, X * (1 - X)**2, (4, 5, 6, 7)), "II": (2, 4, 8, 3, 1 + 14*X + X**2, X * (1 - X)**4, (154, 159, 164)), "III": (3, 3, 4, 3, 1 + 8*X, X * (1 - X)**3, (70, 75, 78)), "IV": (4, 2, 2, 3, 1 + 3*X, X * (1 - X)**2, (17, 20, 22)), } def zeta_polynomial(kind, length): q, b, step, ratio, first, second, _ = TYPES[kind] exponent = length // step terms = exponent // ratio distance = b * (terms + 1) genus = length // 2 + 1 - distance basis = first**exponent enumerator = basis divisor = first**ratio for j in range(1, terms + 1): quotient, remainder = divmod(basis, divisor) assert not remainder basis = quotient * second assert basis[j] == 1 enumerator = enumerator - enumerator[j] * basis assert enumerator[0] == 1 and enumerator[terms + 1] != 0 assert all(enumerator[j] == 0 for j in range(1, terms + 1)) # The polynomial W_y(1+v,1) gives a single binomial transform # containing all coefficients of P(T)/(1-qT), up to a common scalar. derivative = [0] * length for w in range(distance, length + 1, b): derivative[length - w] = w * enumerator[w // b] shifted = fmpz_poly(derivative)(1 + X) values = [] denominator = comb(length - 1, distance - 1) for i in range(2 * genus + 1): values.append(fmpq(shifted[length - distance - i], denominator)) denominator = denominator * (length - distance - i) // (distance + i) p = (fmpq_poly(values) * fmpq_poly([1, -q])).truncate(2 * genus + 1) assert p.degree() == 2 * genus and p[0] > 0 return p / p[0] def certify(kind, length): q = TYPES[kind][0] p = zeta_polynomial(kind, length) g = p.degree() // 2 assert all(p[2*g - i] == q**(g-i) * p[i] for i in range(g+1)) if g == 0: return 0 # The DFT evaluates the real, centered Laurent polynomial at a full # circle of mesh points. Both strict comparisons enclose the exact sign. ctx.prec = 96 + 2*g mesh = 1 << (8*g).bit_length() coefficients = [acb(0)] * mesh root_q = arb(q).sqrt() scale = (1 / root_q)**g for i in range(g, -1, -1): value = acb(arb(p[i]) * scale) coefficients[(i-g) % mesh] = coefficients[g-i] = value scale *= root_q values = acb.dft(coefficients) previous = 0 changes = 0 for value in values[:mesh//2 + 1]: assert value.imag.contains(0) real = value.real sign = int(real > 0) - int(real < 0) if sign: changes += int(previous != 0 and previous != sign) previous = sign if changes != g: raise ArithmeticError(f"{kind} length {length}: {changes} sign changes, expected {g}") return 2*g def main(): totals = Counter() for kind, (_, _, step, ratio, _, _, bounds) in TYPES.items(): lengths = sorted( step * (ratio * index + residue) for residue, stop in enumerate(bounds) for index in range(stop) if index or residue ) zeros = sum(certify(kind, length) for length in lengths) totals.update(lengths=len(lengths), zeros=zeros) print(f"Type {kind}: {len(lengths)} lengths, maximum {max(lengths)}, {zeros} zeros", flush=True) assert totals["lengths"] == 777 print(f"All {totals['lengths']} type-and-length pairs certified, {totals['zeros']} zeros.") if __name__ == "__main__": main()