"""Exact cyclic convolutions for the finite arithmetic certificate (Droll, (7)).""" from fractions import Fraction as Q from itertools import product from math import gcd, prod from design import CERT, MONOMIALS, squarefree_factors def design_rows(residue, s): """Integer weights with common denominator 25*10^6*(2s)^4.""" sign = 1 if residue == 2 else -1 W = [0, 5+sign, 5-sign] # five times W_kappa for a, b, c, d, gamma in MONOMIALS: if residue == 1: a, b, c, d = b, a, d, c r = (residue+c-d-a+b) % 3 local = sum(W[t]*W[(r-t) % 3] for t in range(3)) scaled = gamma*10**6 if scaled.denominator != 1: raise ArithmeticError("polynomial coefficient scale") yield (a, b, c, d), scaled.numerator*local*(2*s)**(4-a-b-c-d) def residue_tables(modulus): """Return three signed numerator arrays and their common denominator.""" subgroup = [1 % modulus] while True: v = (4*subgroup[-1]) % modulus if v == subgroup[0]: break subgroup.append(v) s = len(subgroup) denom = 25*10**6*(2*s)**4 # The absolute sum in any bin is <14*10^6 * 52 * (2s)^4. offset = 14*10**6*52*(2*s)**4+1 byte_width = ((2*offset).bit_length()+7)//8 bits = 8*byte_width length = bits*modulus mask = (1 << length)-1 repeated_one = mask//((1 << bits)-1) # One digit per residue in a base strictly greater than twice the offset. # Multiplication modulo base^d-1 is cyclic convolution with no carries. steps = [sum(1 << (bits*(mult*v % modulus)) for v in subgroup) for mult in (1, 2, -1, -2)] polys = {(0, 0, 0, 0): 1} def polynomial(pattern): if pattern not in polys: i = next(j for j, k in enumerate(pattern) if k) prev = list(pattern) prev[i] -= 1 raw = polynomial(tuple(prev))*steps[i] folded = (raw & mask)+(raw >> length) polys[pattern] = folded-mask if folded >= mask else folded return polys[pattern] tables = [] for r in range(3): value = offset*repeated_one for pattern, weight in design_rows(r, s): value += weight*polynomial(pattern) if not 0 < value < mask: raise ArithmeticError("signed packing range") blob = value.to_bytes(modulus*byte_width, "little") tables.append([int.from_bytes(blob[i:i+byte_width], "little")-offset for i in range(0, len(blob), byte_width)]) return tables, denom def verify_series(ck): # A direct, unpacked enumeration tests orientation, signs and carry bounds. ck.at("(2.2)") for d in (5, 7, 13): group = {pow(4, j, d) for j in range(d)} rows, denom = residue_tables(d) for r in range(3): direct = [Q(0)]*d kappa = Q(1 if r == 2 else -1, 5) W = (Q(0), 1+kappa, 1-kappa) for a, b, c, n, gamma in MONOMIALS: if r == 1: a, b, c, n = b, a, n, c target = (r+c-n-a+b) % 3 lam = sum(W[v]*W[(target-v) % 3] for v in range(3)) steps = (1,)*a + (2,)*b + (-1,)*c + (-2,)*n bins = [0]*d for digits in product(group, repeat=len(steps)): bins[sum(x*y for x, y in zip(steps, digits)) % d] += 1 weight = gamma*lam/(2*len(group))**len(steps) for j, count in enumerate(bins): direct[j] += count*weight ck.eq("series", f"direct signed orbit enumeration, d={d}, class={r}", direct, [Q(x, denom) for x in rows[r]]) joint = {d for d in range(1, 5006) if 5005 % d == 0} extra = {d for d in range(1, 2002) if gcd(d, 6) == 1 and squarefree_factors(d) is not None} - joint joint_values = [[Q(0)]*5005 for _ in range(3)] extra_total = [Q(0)]*3 for d in sorted(joint | extra): rows, denom = residue_tables(d) denom *= prod(p-2 for p in squarefree_factors(d)) for i, row in enumerate(rows): if d in joint: terms = [Q(a, denom) for a in row] pooled = joint_values[i] for j in range(5005): pooled[j] += terms[j % d] else: extra_total[i] += Q(min(row), denom) for r in range(3): value = min(joint_values[r])+extra_total[r] ck.lt("series", f"coupled minimum and extra minima, N mod 3 = {r}", CERT[int(r != 0)], value)