"""Exact checks of signed projections, composite dilations, and local variances.""" from collections import Counter from fractions import Fraction from functools import lru_cache from itertools import combinations from math import gcd, lcm, prod def signed_pairs(blocks, retained): depth = 4 * blocks radix = 10 * (depth + retained + 1) scale = 1 pairs = {(0, 0): 1} for first in range(0, depth, 4): steps = (2 * scale, 3 * scale, radix * scale, scale) for index, step in enumerate(steps, first + 1): expanded = Counter(pairs) for (u, v), sign in pairs.items(): expanded[u + step, v + index * step] -= sign pairs = {key: coeff for key, coeff in expanded.items() if coeff} scale *= radix**2 return pairs def check_projections(): total_shifts = 0 for blocks in range(1, 5): depth = 4 * blocks retained = depth**2 pairs = signed_pairs(blocks, retained) count = 12**blocks assert len(pairs) == len({u for u, _ in pairs}) == count assert set(pairs.values()) == {-1, 1} and sum(pairs.values()) == 0 for k in range(1, depth + 1): projected = Counter() for (u, v), sign in pairs.items(): projected[k * u - v] += sign assert all(value == 0 for value in projected.values()) for k in range(depth + 1, depth + retained + 1): assert len({k * u - v for u, v in pairs}) == count total_shifts += retained * count return total_shifts def prime_factors(value): factors = set() divisor = 2 while divisor * divisor <= value: if value % divisor == 0: factors.add(divisor) while value % divisor == 0: value //= divisor divisor += 1 if value > 1: factors.add(value) return factors @lru_cache(maxsize=None) def residue_tail(base, weights, prime, residue): """Sum c(k)b^(−k) for positive k in the given residue class mod prime.""" period = lcm(len(weights), prime) first = residue or prime numerator = sum(weights[k % len(weights)] * base ** (period - k) for k in range(first, period + 1, prime)) return Fraction(numerator, base**period - 1) def squared_crt(dilations, period): value, modulus = 0, period for q, offset in dilations: if q == 1: continue square = q * q assert gcd(modulus, square) == 1 value += modulus * (((offset - value) * pow(modulus, -1, square)) % square) modulus *= square return value, modulus def check_composite_tails(): # Small exact instance of the first radix place. Two dilations are prime # squares and another is a product of two different primes. digits = [(0, 0, 1), (1, 4, -1), (2, 2, -1), (4, 10, 1), (5, 8, 1), (6, 12, -1)] dilations = [(1 + 60 * u, 60 * v) for u, v, _ in digits] test_primes = {2, 3, 5, 13, 17, 23, 31}.union(*(prime_factors(q) for q, _ in dilations)) assert {121, 301, 361}.issubset(q for q, _ in dilations) residue, step = squared_crt(dilations, 12) checked = 0 phases = 0 for base in (2, 3, 5): for weights in ((1,), (1, -1), (0, 1, -2), (0, 0, 3, 0), (1, -1, 0, 2, 0, -2)): ordinary = residue_tail(base, weights, 1, 0) series = sum(residue_tail(base, weights, p, 0) for p in test_primes) denominator = series.denominator for index in (1, 2, 7, 15): n = residue + index * step phase_difference = Fraction(0) for (q, offset), (_, _, sign) in zip(dilations, digits): m = (n - offset) // q assert n - offset == q * m and m % lcm(q, len(weights)) == 0 undilated = sum(residue_tail(base, weights, p, -m % p) for p in test_primes) dilated = Fraction(0) fixed = Fraction(0) for p in test_primes: if q % p == 0: assert (n - offset) % p == 0 dilated += ordinary fixed += ordinary - residue_tail(base, weights, p, 0) else: k0 = ((offset - n) * pow(q, -1, p)) % p dilated += residue_tail(base, weights, p, k0) assert dilated - undilated == fixed assert (denominator * undilated).denominator == 1 phase_difference += sign * (dilated - fixed) checked += 1 assert (denominator * phase_difference).denominator == 1 phases += 1 return checked, phases def check_local_variances(): depth, retained = 4, 8 pairs = signed_pairs(1, retained) offsets = sorted(u for u, _ in pairs) # A multiple of all actual differences suffices in place of the factorial. multiplier = 60 * (depth + retained + 1) * prod(j - i for i, j in combinations(offsets, 2)) rows = [(h, sign, (depth + h) * (1 + multiplier * u) - multiplier * v) for (u, v), sign in pairs.items() for h in range(1, retained + 1)] selected = 0 checked = 0 for p in range(127, 10000): if prime_factors(p) != {p} or multiplier % p == 0: continue classes = [(-shift) % p for _, _, shift in rows] if len(set(classes)) != len(classes): continue for base in (2, 3, 7): for weights in ((1,), (1, -1), (0, 1, -2), (0, 0, 0, 3)): values = [0] * p for (h, sign, _), residue in zip(rows, classes): values[residue] += sign * base**(retained - h) * weights[(depth + h) % len(weights)] expected_squares = len(pairs) * sum((base**(retained - h) * weights[(depth + h) % len(weights)])**2 for h in range(1, retained + 1)) assert sum(values) == 0 and sum(x * x for x in values) == expected_squares > 0 checked += 1 selected += 1 if selected == 4: break assert selected == 4 return checked if __name__ == '__main__': retained_shifts = check_projections() tails, phases = check_composite_tails() variances = check_local_variances() print(f'Passed 40 early-depth cancellations, {retained_shifts:,} retained projections, ' f'{tails} exact infinite-tail identities, {phases} signed phases, and {variances} local variances.')