| | @@ -0,0 +1,240 @@ |
| 1 | +"""Exact tests for the projection bound and its two digit-box applications.""" |
| 2 | + |
| 3 | +from collections import Counter |
| 4 | +from fractions import Fraction as F |
| 5 | +from itertools import combinations, product |
| 6 | +from math import ceil, comb |
| 7 | +from pathlib import Path |
| 8 | +from runpy import run_path |
| 9 | + |
| 10 | +PRIOR = run_path(str(Path(__file__).resolve().parents[2] / "lower-middle-energy" / "check.py")) |
| 11 | + |
| 12 | + |
| 13 | +def occupancy_bound(n, p, m): |
| 14 | + """Lower bound for the mean number of occupied points of the plane.""" |
| 15 | + return n - F(comb(n, 2), p**2) + F(3 * comb(n, 3), p**4) / (m + F(n - 3, p**2)) |
| 16 | + |
| 17 | + |
| 18 | +def triple_span(triple, p): |
| 19 | + a, b, c = triple |
| 20 | + return { |
| 21 | + tuple((x + s * (y - x) + t * (z - x)) % p for x, y, z in zip(a, b, c)) |
| 22 | + for s, t in product(range(p), repeat=2) |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | +def profiles(total, minimum=1): |
| 27 | + """The integer partitions of total, listed in nondecreasing order.""" |
| 28 | + if total == 0: |
| 29 | + yield () |
| 30 | + return |
| 31 | + for size in range(minimum, total + 1): |
| 32 | + for rest in profiles(total - size, size): |
| 33 | + yield (size,) + rest |
| 34 | + |
| 35 | + |
| 36 | +def check_fibre_profiles(): |
| 37 | + for k in range(1, 25): |
| 38 | + assert comb(k - 1, 2) * (3 * comb(k, 3) + 4 * comb(k, 4)) == 3 * comb(k, 3)**2 |
| 39 | + count = 0 |
| 40 | + for n in range(25): |
| 41 | + for sizes in profiles(n): |
| 42 | + pairs, triples, quads = (sum(comb(k, j) for k in sizes) for j in (2, 3, 4)) |
| 43 | + surplus = len(sizes) - n + pairs |
| 44 | + assert surplus == sum(comb(k - 1, 2) for k in sizes) |
| 45 | + assert surplus * (3 * triples + 4 * quads) >= 3 * triples**2 |
| 46 | + count += 1 |
| 47 | + print(f"{count:,} fibre-size profiles with total at most 24") |
| 48 | + |
| 49 | + |
| 50 | +def check_all_maps(points, p, dimension, report=False): |
| 51 | + """Compare all linear projections with conditional counts from actual affine spans.""" |
| 52 | + n = len(points) |
| 53 | + encodings = [sum(x * (2 * p)**i for i, x in enumerate(a)) for a in points] |
| 54 | + forms = [ |
| 55 | + [sum(u * x for u, x in zip(coefs, a)) % p for a in points] |
| 56 | + for coefs in product(range(p), repeat=dimension) |
| 57 | + ] |
| 58 | + occupied = pairs = triples = quadruples = lifts = 0 |
| 59 | + most_selected = 0 |
| 60 | + for first, second in product(forms, repeat=2): |
| 61 | + labels = list(zip(first, second)) |
| 62 | + sizes = Counter(labels).values() |
| 63 | + o = len(sizes) |
| 64 | + c2, c3, c4 = (sum(comb(k, j) for k in sizes) for j in (2, 3, 4)) |
| 65 | + surplus = o - n + c2 |
| 66 | + assert surplus == sum(comb(k - 1, 2) for k in sizes) |
| 67 | + assert surplus * (3 * c3 + 4 * c4) >= 3 * c3**2 |
| 68 | + occupied += o |
| 69 | + pairs += c2 |
| 70 | + triples += c3 |
| 71 | + quadruples += c4 |
| 72 | + |
| 73 | + # Pick one integer per image. Vertical translates of the parabola partition the plane. |
| 74 | + parts = [[] for _ in range(p)] |
| 75 | + for (x, y), a in dict(zip(labels, encodings)).items(): |
| 76 | + parts[(y - x * x) % p].append(a) |
| 77 | + assert sum(map(len, parts)) == o |
| 78 | + assert all(PRIOR["is_sidon"](part) for part in parts) |
| 79 | + most_selected = max(most_selected, max(map(len, parts))) |
| 80 | + lifts += p |
| 81 | + |
| 82 | + maps = len(forms)**2 |
| 83 | + assert p**2 * pairs == maps * comb(n, 2) |
| 84 | + assert p * most_selected * maps >= occupied |
| 85 | + if n >= 3: |
| 86 | + expected_triples = expected_ordered_extras = F(0) |
| 87 | + m = 0 |
| 88 | + for triple in combinations(points, 3): |
| 89 | + span = triple_span(triple, p) |
| 90 | + inside = len(span.intersection(points)) |
| 91 | + hit = F(1, len(span)**2) |
| 92 | + m = max(m, inside) |
| 93 | + expected_triples += hit |
| 94 | + expected_ordered_extras += hit * (inside - 3 + F(n - inside, p**2)) |
| 95 | + assert triples == maps * expected_triples |
| 96 | + assert 4 * quadruples == maps * expected_ordered_extras |
| 97 | + total_surplus = occupied - maps * n + pairs |
| 98 | + assert total_surplus * (3 * triples + 4 * quadruples) >= 3 * triples**2 |
| 99 | + assert F(occupied, maps) >= occupancy_bound(n, p, m) |
| 100 | + if report: |
| 101 | + print(f"F_{p}^{dimension}, n={n}, M={m}: mean occupied {F(occupied, maps)}, " |
| 102 | + f"lower bound {occupancy_bound(n, p, m)}") |
| 103 | + return maps, lifts |
| 104 | + |
| 105 | + |
| 106 | +def check_projections(): |
| 107 | + plane = tuple(product(range(3), repeat=2)) |
| 108 | + counts = [check_all_maps(s, 3, 2) for s in PRIOR["all_subsets"](plane)] |
| 109 | + # A plane and a point outside it, a line with off-line points, and the field parabola. |
| 110 | + for case in [ |
| 111 | + (tuple((x, y, 0) for x, y in plane) + ((0, 0, 1),), 3, 3), |
| 112 | + (tuple((x, 0, 0) for x in range(5)) + ((0, 1, 0), (0, 0, 1), (1, 1, 1)), 5, 3), |
| 113 | + (tuple((x, x * x % 7) for x in range(7)), 7, 2), |
| 114 | + ]: |
| 115 | + counts.append(check_all_maps(*case, report=True)) |
| 116 | + print(f"{len(counts)} point sets, {sum(row[0] for row in counts):,} projections, " |
| 117 | + f"{sum(row[1] for row in counts):,} Sidon pullbacks") |
| 118 | + |
| 119 | + |
| 120 | +def check_scalar_parabola(): |
| 121 | + """Reproduce the October report's scalar example with a large exceptional fibre.""" |
| 122 | + cases = 0 |
| 123 | + for p in (3, 5, 7, 11): |
| 124 | + for n in range(1, p + 1): |
| 125 | + nonempty = sum( |
| 126 | + any((u*x + v*x*x + t) % p == 0 for x in range(n)) |
| 127 | + for u, v, t in product(range(p), repeat=3) |
| 128 | + ) |
| 129 | + assert nonempty == n*p**2 - p*comb(n, 2) + comb(n - 1, 2) |
| 130 | + cases += 1 |
| 131 | + print(f"Scalar parabola probability checked for {cases} choices of (p,n)") |
| 132 | + |
| 133 | + |
| 134 | +def check_digit_planes(): |
| 135 | + """Compute intersections on small boxes and all three-dimensional levels modulo 101.""" |
| 136 | + for side, dimension in [(8, 1), (3, 3)]: |
| 137 | + box = tuple(product(range(side), repeat=dimension)) |
| 138 | + maximum = max(len(triple_span(s, 11).intersection(box)) for s in combinations(box, 3)) |
| 139 | + assert maximum == side**min(dimension, 2) |
| 140 | + print(f"F_11, box of side {side} in dimension {dimension}: M={maximum}") |
| 141 | + square = set(product(range(8), repeat=2)) |
| 142 | + assert len(square.intersection(triple_span(((0, 0), (0, 7), (7, 0)), 11))) == len(square) == 64 |
| 143 | + print("F_11, box of side 8 in dimension 2: M=64") |
| 144 | + |
| 145 | + p = 101 |
| 146 | + levels = {} |
| 147 | + for a in product(range(8), repeat=3): |
| 148 | + levels.setdefault(sum(x*x for x in a), []).append(a) |
| 149 | + count = largest = 0 |
| 150 | + for points in levels.values(): |
| 151 | + for a, b, c in combinations(points, 3): |
| 152 | + u = [y - x for x, y in zip(a, b)] |
| 153 | + v = [z - x for x, z in zip(a, c)] |
| 154 | + normal = (u[1] * v[2] - u[2] * v[1], |
| 155 | + u[2] * v[0] - u[0] * v[2], |
| 156 | + u[0] * v[1] - u[1] * v[0]) |
| 157 | + assert any(entry % p for entry in normal) |
| 158 | + in_plane = sum( |
| 159 | + sum(h*(y - x) for h, x, y in zip(normal, a, d)) % p == 0 for d in points |
| 160 | + ) |
| 161 | + assert 3 <= in_plane <= 16 |
| 162 | + largest = max(largest, in_plane) |
| 163 | + count += 1 |
| 164 | + print(f"F_101 digit levels in dimension 3: {count:,} triple planes, maximum {largest} points") |
| 165 | + |
| 166 | + |
| 167 | +def check_constants(): |
| 168 | + a, b = F(7, 6), F(9, 7) |
| 169 | + assert F(457, 500) * b - a == F(89, 10500) |
| 170 | + for m, target, expected in [ |
| 171 | + (64, F(49, 90), [F(3030, 5551), F(25655, 47097)]), |
| 172 | + (16, F(11, 20), [F(114, 205), F(6671, 12105)]), |
| 173 | + ]: |
| 174 | + endpoints = [] |
| 175 | + for t in (a, b): |
| 176 | + pair_term = 1/t - 1/(2 * t**3) |
| 177 | + correction = 1/(2 * t**3 * (m * t**2 + 1)) |
| 178 | + endpoints.append(pair_term + correction) |
| 179 | + assert endpoints[-1] == 1/t - m/(2 * t * (m * t**2 + 1)) |
| 180 | + assert endpoints == expected |
| 181 | + assert min(endpoints) > target and target**2 > F(8, 27) |
| 182 | + # This sign makes the finite-n correction nonnegative throughout the window. |
| 183 | + assert m*a**2 >= 2 |
| 184 | + print(f"M={m}: endpoints {endpoints[0]}, {endpoints[1]}, " |
| 185 | + f"margin {min(endpoints)-target} over {target}") |
| 186 | + |
| 187 | + |
| 188 | +def level_counts(r): |
| 189 | + """Coefficients of (sum_{j=0}^7 z^(j^2))^r.""" |
| 190 | + counts = [1] |
| 191 | + for _ in range(r): |
| 192 | + updated = [0] * (len(counts) + 49) |
| 193 | + for t, count in enumerate(counts): |
| 194 | + for j in range(8): |
| 195 | + updated[t + j*j] += count |
| 196 | + counts = updated |
| 197 | + return counts |
| 198 | + |
| 199 | + |
| 200 | +def check_augmented_level(): |
| 201 | + # Cross-check the scaffold intersection formula by directly constructing two small boxes. |
| 202 | + for r in (2, 4): |
| 203 | + h = 4**r |
| 204 | + scaffold = set(range(h + 1)) | {h*j for j in range(h + 1)} |
| 205 | + assert len(scaffold) == 2*h |
| 206 | + half_counts = level_counts(r // 2) |
| 207 | + overlap = Counter() |
| 208 | + for a in product(range(8), repeat=r): |
| 209 | + if sum(x * 16**i for i, x in enumerate(a)) in scaffold: |
| 210 | + overlap[sum(x*x for x in a)] += 1 |
| 211 | + for t in range(2, 49*r + 1): |
| 212 | + half = half_counts[t] if t < len(half_counts) else 0 |
| 213 | + assert overlap[t] == 2*half |
| 214 | + |
| 215 | + r, p = 14, 206209 |
| 216 | + counts = level_counts(r) |
| 217 | + n = max(counts) |
| 218 | + t = counts.index(n) |
| 219 | + assert sum(counts) == 8**r and 8**r <= (49*r + 1)*n |
| 220 | + assert (t, n) == (239, 28347923604) |
| 221 | + assert p > 98 and PRIOR["is_prime"](p) |
| 222 | + assert F(7, 6)**2 * n < p*p <= F(9, 7)**2 * n |
| 223 | + h = 4**r |
| 224 | + # For even r, H = 16^(r/2). Neither 0 nor H has squared digit sum t > 1. |
| 225 | + size_augmented = n + 2*h - 2*level_counts(r // 2)[t] |
| 226 | + pair_only = ceil((n - F(comb(n, 2), p**2)) / p) |
| 227 | + with_triples = ceil(occupancy_bound(n, p, 16) / p) |
| 228 | + assert (size_augmented, pair_only, with_triples) == (28884793466, 91649, 93482) |
| 229 | + assert 20**2 * with_triples**2 > 11**2 * size_augmented |
| 230 | + print(f"r={r}, t={t}, n={n:,}, N={size_augmented:,}, p={p}: " |
| 231 | + f"guaranteed {with_triples:,} (pair bound {pair_only:,})") |
| 232 | + |
| 233 | + |
| 234 | +if __name__ == "__main__": |
| 235 | + check_fibre_profiles() |
| 236 | + check_projections() |
| 237 | + check_scalar_parabola() |
| 238 | + check_digit_planes() |
| 239 | + check_constants() |
| 240 | + check_augmented_level() |