"""Finite exact checks of variance stopping and the cyclotomic application.""" from fractions import Fraction from itertools import combinations, product from math import isqrt, prod from check_prime_periodic_masks import prime_tail from check_signed_tails import signed_pairs def primes_through(limit): flags = bytearray([1]) * (limit + 1) flags[:2] = bytes(2) for divisor in range(2, isqrt(limit) + 1): if flags[divisor]: count = (limit - divisor * divisor) // divisor + 1 flags[divisor * divisor::divisor] = bytes(count) return (p for p in range(2, limit + 1) if flags[p]) def first_crossing(base, residue_modulus, active_class): depth, retained, period = 4, 16, 4 pairs = signed_pairs(1, retained) coordinates = sorted(u for u, v in pairs) multiplier = period * (depth + retained + 1) * prod( right - left for left, right in combinations(coordinates, 2) ) dilations = [1 + multiplier * u for u in coordinates] shifts = [ ((depth + h) * (1 + multiplier * u) - multiplier * v, h, sign) for (u, v), sign in pairs.items() for h in range(1, retained + 1) ] variance_scale = Fraction(len(pairs), base ** (2 * depth)) denominator = base ** (2 * (depth + retained)) target = Fraction(1, 12) total = reciprocal_mass = Fraction(0) checked = 0 # The integer amplitude puts the small model's crossing within the sieve. amplitude = 2 * base ** (depth + 1) for prime in primes_through(150_000): if prime % residue_modulus != active_class: continue if period % prime == 0 or any(q % prime == 0 for q in dilations): continue residues = [-shift % prime for shift, _, _ in shifts] if len(set(residues)) < len(shifts): continue row = [amplitude * (((prime // 3 + r * prime + r * r) % 5) - 2) for r in range(period)] assert any(row) numerators = [ sign * row[(depth + h) % period] * base ** (retained - h) for _, h, sign in shifts ] # Enumerate the values occupied in the uniform residue distribution. # The other residues have zero value. distribution = dict(zip(residues, numerators)) assert sum(distribution.values()) == 0 direct = Fraction(sum(value * value for value in distribution.values()), prime * denominator) weight = sum(Fraction(row[(depth + h) % period] ** 2, base ** (2 * h)) for h in range(1, retained + 1)) assert direct == variance_scale * weight / prime assert weight >= Fraction(1, base ** (2 * period)) previous = total total += direct reciprocal_mass += Fraction(1, prime) checked += 1 if total >= target: assert previous < target and total - target < direct assert reciprocal_mass <= total * base ** (2 * period) / variance_scale return checked, prime raise AssertionError("the test did not reach the target variance") def quotient_by_monic(dividend, divisor): """Exact polynomial division with coefficients in ascending order.""" remainder = list(dividend) quotient = [0] * (len(dividend) - len(divisor) + 1) assert divisor[-1] == 1 for i in reversed(range(len(quotient))): coefficient = remainder[i + len(divisor) - 1] quotient[i] = coefficient for j, value in enumerate(divisor): remainder[i + j] -= coefficient * value assert not any(remainder) return quotient def evaluate(coefficients, x): value = 0 for coefficient in reversed(coefficients): value = x * value + coefficient return value def check_cyclotomic_columns(): # Phi_1, Phi_2, Phi_3, Phi_4, Phi_6, and Phi_12. polynomials = [(-1, 1), (1, 1), (1, 1, 1), (1, 0, 1), (1, -1, 1), (1, 0, -1, 0, 1)] period = 12 common = [-1] + [0] * (period - 1) + [1] cofactors = [quotient_by_monic(common, phi) for phi in polynomials] columns = masks = 0 for weights in product((-1, 0, 1), repeat=len(cofactors)): numerator = [0] * period for weight, cofactor in zip(weights, cofactors): for i, value in enumerate(cofactor): numerator[i] += weight * value assert bool(any(numerator)) == bool(any(weights)) columns += 1 for base, prime in ((2, 5), (2, 7), (3, 11), (5, 13)): x = base ** prime original = sum(Fraction(weight, evaluate(phi, x)) for weight, phi in zip(weights, polynomials)) assert original == Fraction(evaluate(numerator, x), x ** period - 1) inverse = pow(prime, -1, period) row = [numerator[-inverse * r % period] for r in range(period)] assert original == prime_tail(prime, row, 0, base) masks += 1 return columns, masks if __name__ == "__main__": for base, modulus, residue in ((2, 1, 0), (2, 13, 1), (3, 11, 3)): count, cutoff = first_crossing(base, modulus, residue) print(f"Base {base}, p = {residue} mod {modulus}: {count} exact prime " f"variances, first crossing at p = {cutoff}.") columns, masks = check_cyclotomic_columns() print(f"{columns} signed cyclotomic columns and {masks} exact expansions " "as periodic prime weights.")