| | @@ -0,0 +1,235 @@ |
| 1 | +"""Exact checks for the two-representative Sidon lift in differences.md.""" |
| 2 | + |
| 3 | +from collections import Counter |
| 4 | +from fractions import Fraction as Q |
| 5 | +from itertools import combinations, combinations_with_replacement |
| 6 | +from math import ceil, comb, isqrt |
| 7 | +from pathlib import Path |
| 8 | +from runpy import run_path |
| 9 | + |
| 10 | + |
| 11 | +EXPERIMENTS = Path(__file__).resolve().parents[1] |
| 12 | +is_sidon = run_path(str(EXPERIMENTS / "lower-structured" / "check.py"))["is_sidon"] |
| 13 | +ruzsa = run_path(str(EXPERIMENTS / "upper-stability" / "verify_independent.py"))["ruzsa"] |
| 14 | + |
| 15 | + |
| 16 | +def gap_counts(points): |
| 17 | + return Counter(y - x for x, y in combinations(sorted(points), 2)) |
| 18 | + |
| 19 | + |
| 20 | +def additive_energy(points): |
| 21 | + ordered_sums = Counter(x + y for x in points for y in points) |
| 22 | + return sum(count**2 for count in ordered_sums.values()) |
| 23 | + |
| 24 | + |
| 25 | +def gap_weight(b, r): |
| 26 | + return min(Q(1, 3), Q(b, b + r - 1)) |
| 27 | + |
| 28 | + |
| 29 | +def energy_bound(n, energy, b, h): |
| 30 | + pairs = comb(n, 2) |
| 31 | + mean = Q(energy - 2 * n**2 + n, 2 * b * pairs) |
| 32 | + return b * (n * h - pairs * h**2 + pairs * h**2 / (3 + mean)) |
| 33 | + |
| 34 | + |
| 35 | +def check_translates(): |
| 36 | + cases = 0 |
| 37 | + for m in range(1, 8): |
| 38 | + subsets = [s for size in range(1, m + 1) for s in combinations(range(m), size)] |
| 39 | + for template in subsets: |
| 40 | + if not is_sidon(template, m): |
| 41 | + continue |
| 42 | + b = len(template) |
| 43 | + translates = [{(j + t) % m for j in template} for t in range(m)] |
| 44 | + for labels in subsets: |
| 45 | + n = len(labels) |
| 46 | + hits = [len(selected.intersection(labels)) for selected in translates] |
| 47 | + assert sum(hits) == b * n |
| 48 | + assert sum(x * (x - 1) for x in hits) <= n * (n - 1) |
| 49 | + assert sum(x > 0 for x in hits) * (b + n - 1) >= b**2 * n |
| 50 | + cases += 1 |
| 51 | + print(f"Translate inequality on {cases:,} template and label-set pairs, m = 1,...,7") |
| 52 | + |
| 53 | + |
| 54 | +def lift(fibres): |
| 55 | + """Take both endpoints in the first size-two fibre of each positive gap.""" |
| 56 | + kept = [] |
| 57 | + doubles = Counter() |
| 58 | + for fibre in fibres: |
| 59 | + if fibre: |
| 60 | + kept.append(fibre[0]) |
| 61 | + if len(fibre) == 2: |
| 62 | + gap = fibre[1] - fibre[0] |
| 63 | + if gap not in doubles: |
| 64 | + kept.append(fibre[1]) |
| 65 | + doubles[gap] += 1 |
| 66 | + return kept, doubles |
| 67 | + |
| 68 | + |
| 69 | +def field_outcomes(points, m, template, prime): |
| 70 | + assert prime > max(points) - min(points) |
| 71 | + assert prime > 1 and all(prime % i for i in range(2, isqrt(prime) + 1)) |
| 72 | + stop = m * (prime // (2 * m)) |
| 73 | + translates = [[(j + t) % m for j in template] for t in range(m)] |
| 74 | + for u in range(prime): |
| 75 | + for v in range(prime): |
| 76 | + fibres = [[] for _ in range(m)] |
| 77 | + for x in points: |
| 78 | + y = (u * x + v) % prime |
| 79 | + if y < stop: |
| 80 | + fibres[y % m].append(x) |
| 81 | + for labels in translates: |
| 82 | + yield 1, [fibres[j] for j in labels] |
| 83 | + |
| 84 | + |
| 85 | +def torus_outcomes(points, m, template): |
| 86 | + """Exact area of each phase cell. A uniform phi absorbs the template translate.""" |
| 87 | + # Edges in phi cross or wrap when 2m*(x-y)*theta is integral. |
| 88 | + # Include the stationary cut at phi=0 by including the coordinate zero. |
| 89 | + cuts = {Q(0), Q(1)} |
| 90 | + for d in gap_counts(set(points) | {0}): |
| 91 | + period = 2 * m * d |
| 92 | + cuts.update(Q(i, period) for i in range(1, period)) |
| 93 | + cuts = sorted(cuts) |
| 94 | + edges = [Q(j, 2 * m) for j in range(2 * m)] |
| 95 | + for left, right in zip(cuts, cuts[1:]): |
| 96 | + theta = (left + right) / 2 |
| 97 | + images = [x * theta % 1 for x in points] |
| 98 | + breaks = sorted({Q(0), Q(1)} | {(edge - z) % 1 for edge in edges for z in images}) |
| 99 | + # Each width is affine between theta cuts, so its midpoint value integrates exactly. |
| 100 | + for low, high in zip(breaks, breaks[1:]): |
| 101 | + phi = (low + high) / 2 |
| 102 | + fibres = [[] for _ in range(m)] |
| 103 | + for x, z in zip(points, images): |
| 104 | + slot = int(2 * m * ((z + phi) % 1)) |
| 105 | + if slot % 2 == 0: |
| 106 | + fibres[slot // 2].append(x) |
| 107 | + yield (right - left) * (high - low), [fibres[j] for j in template] |
| 108 | + |
| 109 | + |
| 110 | +def check_law(name, points, m, template, h, outcomes, expected_mass): |
| 111 | + assert tuple(sorted(set(points))) == points and is_sidon(template, m) |
| 112 | + b, n = len(template), len(points) |
| 113 | + gaps = gap_counts(points) |
| 114 | + mass = occupied = surplus = extras = 0 |
| 115 | + trials = peak = repeated_gap_trials = 0 |
| 116 | + point_mass, pair_mass, bonus_mass = Counter(), Counter(), Counter() |
| 117 | + for weight, fibres in outcomes: |
| 118 | + kept, doubles = lift(fibres) |
| 119 | + assert is_sidon(kept) # Includes all diagonal pair sums. |
| 120 | + sizes = [len(fibre) for fibre in fibres if fibre] |
| 121 | + used = len(sizes) |
| 122 | + gamma = sum(comb(k - 1, 2) for k in sizes) |
| 123 | + assert used == sum(sizes) - sum(comb(k, 2) for k in sizes) + gamma |
| 124 | + assert len(kept) == used + len(doubles) |
| 125 | + for fibre in fibres: |
| 126 | + for x in fibre: |
| 127 | + point_mass[x] += weight |
| 128 | + for x, y in combinations(fibre, 2): |
| 129 | + pair_mass[y - x] += weight |
| 130 | + for gap in doubles: |
| 131 | + bonus_mass[gap] += weight |
| 132 | + mass += weight |
| 133 | + occupied += weight * used |
| 134 | + surplus += weight * gamma |
| 135 | + extras += weight * len(doubles) |
| 136 | + peak = max(peak, len(doubles)) |
| 137 | + repeated_gap_trials += any(count > 1 for count in doubles.values()) |
| 138 | + trials += 1 |
| 139 | + |
| 140 | + assert mass == expected_mass |
| 141 | + assert all(point_mass[x] == mass * b * h for x in points) |
| 142 | + for d, r in gaps.items(): |
| 143 | + assert pair_mass[d] == mass * b * h**2 * r |
| 144 | + assert bonus_mass[d] <= min(pair_mass[d], Q(mass, m)) |
| 145 | + base = b * (n * h - comb(n, 2) * h**2) |
| 146 | + spectrum = sum(r * gap_weight(b, r) for r in gaps.values()) |
| 147 | + lower = base + b * h**2 * spectrum |
| 148 | + mean_lift = Q(occupied + extras, mass) |
| 149 | + assert occupied == mass * base + surplus |
| 150 | + assert mean_lift >= lower >= energy_bound(n, additive_energy(points), b, h) |
| 151 | + print(f"{name}: {trials:,} lifts checked, exact mean size {mean_lift}," |
| 152 | + f" lower bound {lower}, largest number of doubled fibres {peak}") |
| 153 | + return peak, repeated_gap_trials |
| 154 | + |
| 155 | + |
| 156 | +def check_lifts(): |
| 157 | + points, m, template = (0, 1, 3, 7, 8, 17), 7, (0, 1, 3) |
| 158 | + # The Park map u=1, v=0, P=43 selects these fibres for t=0. |
| 159 | + kept, doubles = lift([(0, 7), (1, 8), (3, 17)]) |
| 160 | + assert doubles == {7: 2, 14: 1} |
| 161 | + assert set(kept) == {0, 1, 3, 7, 17} and is_sidon(kept) |
| 162 | + assert not is_sidon(points) and 0 + 8 == 1 + 7 |
| 163 | + ruzsa_template, ruzsa_modulus = ruzsa(3) |
| 164 | + for a, modulus, s, prime in ( |
| 165 | + (points, m, template, 43), |
| 166 | + (tuple(range(-5, 7)), ruzsa_modulus, ruzsa_template, 29), |
| 167 | + ): |
| 168 | + peak, repeats = check_law( |
| 169 | + f"Prime field P={prime}, m={modulus}", a, modulus, s, Q(prime // (2 * modulus), prime), |
| 170 | + field_outcomes(a, modulus, s, prime), prime**2 * modulus, |
| 171 | + ) |
| 172 | + if a == points: |
| 173 | + assert peak >= 2 and repeats > 0 |
| 174 | + |
| 175 | + for a in ((0, 1, 2, 3), (-1, 0, 2, 3, 4)): |
| 176 | + check_law("Haar phases, m=3", a, 3, (0, 1), Q(1, 6), |
| 177 | + torus_outcomes(a, 3, (0, 1)), 1) |
| 178 | + |
| 179 | + |
| 180 | +def check_constants(): |
| 181 | + c = Q(3, 4) |
| 182 | + assert (Q(1, 2) - 1 / (8 * c))**2 / c == Q(4, 27) |
| 183 | + assert 1 / (64 * c**3) == Q(1, 27) # Square of the coefficient of the weighted gain. |
| 184 | + scale = 10**12 |
| 185 | + root = isqrt(3 * scale**2) |
| 186 | + lo, hi = Q(root, scale), Q(root + 1, scale) |
| 187 | + assert lo**2 < 3 < hi**2 |
| 188 | + n, q = 10**6, 863 |
| 189 | + assert all(q % i for i in range(2, isqrt(q) + 1)) |
| 190 | + b, m = q - 1, q * (q - 1) |
| 191 | + h = Q(1, 2 * m) |
| 192 | + assert ceil(b * (n * h - comb(n, 2) * h**2)) == 385 |
| 193 | + for cap, limit_floor, guaranteed in ((14, 39494150, 395), (100, 38652464, 387)): |
| 194 | + lower = 2 / (3 * hi) + 1 / (9 * hi + 6 * cap) |
| 195 | + upper = 2 / (3 * lo) + 1 / (9 * lo + 6 * cap) |
| 196 | + assert limit_floor < lower * 10**8 < upper * 10**8 < limit_floor + 1 |
| 197 | + bound = energy_bound(n, cap * n**2 * isqrt(n), b, h) |
| 198 | + assert guaranteed - 1 < bound <= guaranteed |
| 199 | + print(f"C={cap}: limiting coefficient {limit_floor / 10**8:.8f}...," |
| 200 | + f" finite bound {float(bound):.8f}, guarantees {guaranteed}") |
| 201 | + |
| 202 | + |
| 203 | +def check_families(): |
| 204 | + for n, length in ((8, 3), (17, 11), (37, 25)): |
| 205 | + interval = tuple(range(1, length + 1)) |
| 206 | + points = interval + tuple(3 * length * 2**i for i in range(n)) |
| 207 | + counts = gap_counts(points) |
| 208 | + assert all(r == 1 for d, r in counts.items() if d >= length) |
| 209 | + simple_pairs = comb(len(points), 2) - comb(length, 2) |
| 210 | + assert sum(r for d, r in counts.items() if d >= length) == simple_pairs |
| 211 | + expected = len(points)**2 + 2 * (sum(j**2 for j in range(1, length)) + simple_pairs) |
| 212 | + assert additive_energy(points) == expected |
| 213 | + |
| 214 | + for k in range(2, 8): |
| 215 | + length = k**2 |
| 216 | + packed = {2 * length * 2**i + j: i * length + j for i in range(k) for j in range(length)} |
| 217 | + counts = gap_counts(packed) |
| 218 | + assert len(counts) == length - 1 + comb(k, 2) * (2 * length - 1) |
| 219 | + assert 3 * additive_energy(packed) == (2 * k**2 - k) * (2 * length**3 + length) |
| 220 | + sums = {} |
| 221 | + for x, y in combinations_with_replacement(packed, 2): |
| 222 | + image_sum = packed[x] + packed[y] |
| 223 | + assert sums.setdefault(x + y, image_sum) == image_sum |
| 224 | + template, m = ruzsa(17) |
| 225 | + assert m <= len(packed) and is_sidon(template, m) |
| 226 | + witness = [x for x, image in packed.items() if image in template] |
| 227 | + assert len(witness) == 16 and is_sidon(witness) |
| 228 | + print("Mixed family and six block families verified, including a 16-point pullback for k=7") |
| 229 | + |
| 230 | + |
| 231 | +if __name__ == "__main__": |
| 232 | + check_translates() |
| 233 | + check_lifts() |
| 234 | + check_constants() |
| 235 | + check_families() |