| | @@ -0,0 +1,217 @@ |
| 1 | +"""Exact finite checks for the two families and the rank-two window proof.""" |
| 2 | + |
| 3 | +from collections import Counter, defaultdict |
| 4 | +from fractions import Fraction as F |
| 5 | +from itertools import combinations_with_replacement, product |
| 6 | +from math import gcd, isqrt |
| 7 | +from pathlib import Path |
| 8 | +from runpy import run_path |
| 9 | +import sys |
| 10 | + |
| 11 | + |
| 12 | +EXPERIMENTS = Path(__file__).resolve().parents[2] |
| 13 | +DICHOTOMY = EXPERIMENTS / "erdos530" / "upper-fibre-dichotomy" |
| 14 | +sys.path[:0] = [str(DICHOTOMY), str(EXPERIMENTS / "erdos30" / "b3_slack")] |
| 15 | +from constructions import erdos_turan |
| 16 | + |
| 17 | +accepted = run_path(str(DICHOTOMY / "check.py")) |
| 18 | +host = accepted["host"] |
| 19 | +deletion, costs = accepted["DELETION"], accepted["COST"] |
| 20 | +retention = tuple(1 - d for d in deletion) |
| 21 | + |
| 22 | + |
| 23 | +def check_constants(): |
| 24 | + accepted["check_costs"]() |
| 25 | + mean, energy = host.block_moments(deletion, F(1)) |
| 26 | + assert mean == F(1, 2) and energy == F(10709, 40000) |
| 27 | + beta_sq = F(9, 32) |
| 28 | + assert max(F(1, 8), energy / 2) < F(183, 500)**2 |
| 29 | + assert (F(183, 500) + F(4, 25) + F(1, 250))**2 < beta_sq |
| 30 | + a = F(1999, 2000) |
| 31 | + excess = a * (1 - a*a) / (2*a*a - 1) |
| 32 | + assert excess == F(7994001, 7984004000) |
| 33 | + upper = F(52578, 100000) + 2 * max(excess, F(97, 100000)) |
| 34 | + assert (upper + F(1, 500) + F(1, 2000))**2 < beta_sq |
| 35 | + print("Squared cyclic and Erdős–Turán mean bounds:", F(1, 8), energy / 2) |
| 36 | + print("Container mean coefficient upper bound before the vanishing error:", upper) |
| 37 | + |
| 38 | + |
| 39 | +def check_cyclic_windows(): |
| 40 | + """Check the finite centered inequality underlying the equidistribution estimate.""" |
| 41 | + cases = 0 |
| 42 | + for construction, p in product((host.singer, host.bose, host.ruzsa), (5, 11, 17)): |
| 43 | + period, marks = construction(p) |
| 44 | + host.positive_differences(sorted(marks), period) |
| 45 | + weights = (*retention, F(0)) |
| 46 | + width = isqrt(isqrt(period**3)) + 1 |
| 47 | + sizes = {m for m in range(2, period, 2) if 2*m < period and 9*period <= 32*m} |
| 48 | + for size in sorted(sizes): |
| 49 | + lengths = Counter(8*x // size if x < size else 8 for x in range(period)) |
| 50 | + padded = [lengths[i] + width for i in range(9)] |
| 51 | + total = sum(padded) |
| 52 | + average = sum(length*w for length, w in zip(padded, weights)) / total |
| 53 | + variance = sum(length*(w-average)**2 for length, w in zip(padded, weights)) |
| 54 | + bound = 1 + F(len(marks)-1, width) - F(len(marks)**2, total) |
| 55 | + for unit in (1, 2, period - 1): |
| 56 | + if gcd(unit, period) != 1: |
| 57 | + continue |
| 58 | + for shift in (0, size // 4, size - 1, period - 1): |
| 59 | + residues = [(shift + unit*x) % period for x in marks] |
| 60 | + counts = Counter(8*x // size if x < size else 8 for x in residues) |
| 61 | + mu = sum(w * counts[i] for i, w in enumerate(weights)) |
| 62 | + moment = sum(F(counts[i]**2, length) for i, length in enumerate(padded)) |
| 63 | + assert moment <= 1 + F(len(marks)-1, width) |
| 64 | + assert (mu - len(marks)*average)**2 <= bound * variance |
| 65 | + cases += 1 |
| 66 | + print("Cyclic template, window and affine-image checks:", cases) |
| 67 | + |
| 68 | + |
| 69 | +def check_erdos_turan_windows(): |
| 70 | + cases = 0 |
| 71 | + for p in (2, 3, 5, 11, 29, 101): |
| 72 | + natural_size, marks = erdos_turan(p) |
| 73 | + assert len({x + y for x, y in combinations_with_replacement(marks, 2)}) == p*(p+1)//2 |
| 74 | + for size, step, sign in product((18, natural_size-4, natural_size, 3*p*p+1), (1, 2, 5), (-1, 1)): |
| 75 | + width = 2*step*p |
| 76 | + for start in (-width*p//3, -1, 0, size//2, size-1, width*p): |
| 77 | + left, right = sorted((start, start + sign*width*p)) |
| 78 | + points = [start + sign*step*x for x in marks] |
| 79 | + counts = Counter(8*x // size for x in points if 0 <= x < size) |
| 80 | + assert sum(counts.values()) <= min(p, F(size, width) + 2) |
| 81 | + average = F(0) |
| 82 | + for i, weight in enumerate(retention): |
| 83 | + length = max(0, min(right, F(size*(i+1), 8)) - max(left, F(size*i, 8))) |
| 84 | + fraction = F(length, width) |
| 85 | + assert abs(counts[i] - fraction) <= 2 |
| 86 | + average += weight * fraction |
| 87 | + mu = sum(weight*counts[i] for i, weight in enumerate(retention)) |
| 88 | + assert abs(mu - average) <= 2 * sum(retention) |
| 89 | + assert average**2 <= F(size*10709, 80000*step) |
| 90 | + cases += 1 |
| 91 | + print("Erdős–Turán translations, reflections, dilations and clips:", cases) |
| 92 | + |
| 93 | + |
| 94 | +def check_box(size, lengths, steps, radii, modulus, directions): |
| 95 | + """Check the shift cover, cost majorant and Sidon second moment on one box.""" |
| 96 | + def group_sum(x, y): |
| 97 | + return ((x[0] + y[0]) % modulus, (x[1] + y[1]) % modulus) |
| 98 | + |
| 99 | + def shift(i, j): |
| 100 | + a, b = directions |
| 101 | + return ((i*a[0] + j*b[0]) % modulus, (i*a[1] + j*b[1]) % modulus) |
| 102 | + |
| 103 | + def site(i, j): |
| 104 | + return 1 + i*steps[0] + j*steps[1] |
| 105 | + |
| 106 | + box = list(product(*(range(length) for length in lengths))) |
| 107 | + shifts = list(product(*(range(r) for r in radii))) |
| 108 | + sites = {site(*u) for u in box} |
| 109 | + assert len(sites) == len(box) and max(sites) < size |
| 110 | + sums = {} |
| 111 | + for u, v in combinations_with_replacement(box, 2): |
| 112 | + label_sum = group_sum(shift(*u), shift(*v)) |
| 113 | + if sums.setdefault(site(*u) + site(*v), label_sum) != label_sum: |
| 114 | + return Counter() |
| 115 | + |
| 116 | + labels = {shift(*u) for u in box} |
| 117 | + K = {shift(*r) for r in shifts} |
| 118 | + delta = {shift(i, j) for i in range(1-lengths[0], lengths[0]) |
| 119 | + for j in range(1-lengths[1], lengths[1])} |
| 120 | + cover_count = ((2*lengths[0]-2)//radii[0]+1) * ((2*lengths[1]-2)//radii[1]+1) |
| 121 | + assert len(delta) <= cover_count * len(K) |
| 122 | + |
| 123 | + missed = defaultdict(lambda: F(1)) |
| 124 | + enlarged = defaultdict(lambda: F(1)) |
| 125 | + predecessors = defaultdict(set) |
| 126 | + original_cost = F(0) |
| 127 | + for i, j in box: |
| 128 | + block = 8*site(i, j) // size |
| 129 | + missed[shift(i, j)] *= deletion[block] |
| 130 | + original_cost += costs[block] |
| 131 | + for a, b in shifts: |
| 132 | + predecessors[i+a, j+b].add(block) |
| 133 | + different = 0 |
| 134 | + expanded_cost = F(0) |
| 135 | + for u, blocks in predecessors.items(): |
| 136 | + chosen = min(blocks, key=lambda b: (deletion[b], b)) |
| 137 | + enlarged[shift(*u)] *= deletion[chosen] |
| 138 | + expanded_cost += costs[chosen] |
| 139 | + if u[0] < lengths[0] and u[1] < lengths[1]: |
| 140 | + different += int(8*site(*u) // size != chosen) |
| 141 | + distance = sum(d*(r-1) for d, r in zip(steps, radii)) |
| 142 | + assert different <= 7*(distance+1) |
| 143 | + extra = len(predecessors) - len(box) |
| 144 | + assert expanded_cost <= original_cost + (different + extra)*max(costs) |
| 145 | + w = {t: 1 - miss for t, miss in missed.items()} |
| 146 | + majorant = {t: 1 - miss for t, miss in enlarged.items()} |
| 147 | + energy = sum(value**2 for value in majorant.values()) |
| 148 | + assert energy <= expanded_cost |
| 149 | + assert all(w[t] <= majorant[group_sum(t, b)] for t, b in product(labels, K)) |
| 150 | + |
| 151 | + report = Counter(boxes=1, repeated_labels=int(len(labels) < len(box)), |
| 152 | + repeated_shifts=int(len(K) < len(shifts)), |
| 153 | + folded_expansions=int(len({site(*u) for u in predecessors}) < len(predecessors))) |
| 154 | + if modulus == 4: |
| 155 | + base = {(0, 0), (0, 1), (1, 0)} |
| 156 | + else: |
| 157 | + base = {(j, j*j % modulus) for j in range(modulus)} |
| 158 | + pair_sums = {group_sum(x, y) for x, y in combinations_with_replacement(sorted(base), 2)} |
| 159 | + assert 2*len(pair_sums) == len(base)*(len(base)+1) |
| 160 | + for offset in product(range(modulus), repeat=2): |
| 161 | + template = {group_sum(mark, offset) for mark in base} |
| 162 | + active = labels & template |
| 163 | + n = len(active) |
| 164 | + assert n*(n-1) <= len(delta)-1 |
| 165 | + counts = Counter(group_sum(t, b) for t, b in product(active, K)) |
| 166 | + mu = sum(w[t] for t in active) |
| 167 | + assert sum(c*c for c in counts.values()) <= len(K)*(n + len(K)-1) |
| 168 | + assert mu*len(K) <= sum(c*majorant[t] for t, c in counts.items()) |
| 169 | + assert mu**2*len(K) <= (n + len(K)-1)*energy |
| 170 | + report["templates"] += 1 |
| 171 | + return report |
| 172 | + |
| 173 | + |
| 174 | +def check_boxes(): |
| 175 | + report = Counter() |
| 176 | + for lengths in ((1, 6), (2, 2), (3, 4), (4, 5)): |
| 177 | + height, width = lengths |
| 178 | + for steps in ((width, 1), (2*width-1, 1), (2*width+3, 1), (3, 5)): |
| 179 | + size = 7 + sum(step*(length-1) for step, length in zip(steps, lengths)) |
| 180 | + radii = {(1, 1), (1, min(3, width)), (min(2, height), min(2, width))} |
| 181 | + directions = (((1, 0), (0, 1)), ((0, 0), (1, 0)), ((1, 0), (1, 0)), |
| 182 | + ((2, 1), (1, 2)), ((steps[0], 0), (steps[1], 0))) |
| 183 | + for r, modulus, g in product(sorted(radii), (3, 4, 5), directions): |
| 184 | + report.update(check_box(size, lengths, steps, r, modulus, g)) |
| 185 | + assert all(report[key] for key in ("repeated_labels", "repeated_shifts", "folded_expansions")) |
| 186 | + print("Finite rank-two checks:", dict(sorted(report.items()))) |
| 187 | + |
| 188 | + |
| 189 | +def check_integer_descriptions(): |
| 190 | + """Each affine-basis record must specify one complete list of in-range fibres.""" |
| 191 | + box = list(product(range(4), range(5))) |
| 192 | + slopes = (-10**6, -7, -2, -1, 0, 1, 3, 8, 10**6) |
| 193 | + basis_sizes = Counter() |
| 194 | + for interval in (1, 4, 11): |
| 195 | + descriptions = {} |
| 196 | + for a, b, origin in product(slopes, slopes, (-10**6, -2, 0, 4, 10**6)): |
| 197 | + fibres = tuple(((i, j), value) for i, j in box |
| 198 | + if 0 <= (value := origin + a*i + b*j) < interval) |
| 199 | + basis = fibres[:2] |
| 200 | + if len(basis) == 2: |
| 201 | + ((i0, j0), _), ((i1, j1), _) = basis |
| 202 | + for (i, j), value in fibres[2:]: |
| 203 | + if (i1-i0)*(j-j0) != (j1-j0)*(i-i0): |
| 204 | + basis += (((i, j), value),) |
| 205 | + break |
| 206 | + assert descriptions.setdefault(basis, fibres) == fibres |
| 207 | + basis_sizes[len(basis)] += 1 |
| 208 | + assert set(basis_sizes) == {0, 1, 2, 3} |
| 209 | + print("Integer maps by number of recorded basis points:", dict(sorted(basis_sizes.items()))) |
| 210 | + |
| 211 | + |
| 212 | +if __name__ == "__main__": |
| 213 | + check_constants() |
| 214 | + check_cyclic_windows() |
| 215 | + check_erdos_turan_windows() |
| 216 | + check_boxes() |
| 217 | + check_integer_descriptions() |