| | @@ -0,0 +1,263 @@ |
| 1 | +"""Exact finite checks for the full-row Sidon bound in NOTES.md.""" |
| 2 | + |
| 3 | +from collections import Counter |
| 4 | +from fractions import Fraction as Q |
| 5 | +from itertools import combinations, permutations, product |
| 6 | +from math import ceil, comb, floor, gcd, isqrt, prod |
| 7 | +from pathlib import Path |
| 8 | +from runpy import run_path |
| 9 | +from sys import argv |
| 10 | + |
| 11 | + |
| 12 | +PRIOR = run_path(str(Path(__file__).resolve().parents[2] / "lower-middle-energy" / "check.py")) |
| 13 | +HALF = Q(1, 2) |
| 14 | +BOXES = { |
| 15 | + "AP": (0, 1, 2), |
| 16 | + "squares": (1, 16, 49, 64), |
| 17 | + "integer Sidon": (1, 2, 4), |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +def three_hit_floor(eta, distance): |
| 22 | + return max(0, Q(1, 8) - Q(3, 32) / (eta * distance)) / (1 + eta) |
| 23 | + |
| 24 | + |
| 25 | +def gate_intersection(starts, w): |
| 26 | + """Length of the intersection of the half-circle tests in the intercept.""" |
| 27 | + residues = [(w * d) % 1 for d in starts] |
| 28 | + ends = sorted({Q(0), Q(1), *residues, *((r + HALF) % 1 for r in residues)}) |
| 29 | + length = Q(0) |
| 30 | + for left, right in zip(ends, ends[1:]): |
| 31 | + middle = (left + right) / 2 |
| 32 | + if all((middle - r) % 1 < HALF for r in residues): |
| 33 | + length += right - left |
| 34 | + return length |
| 35 | + |
| 36 | + |
| 37 | +def uniform_w_intersection(starts, eta): |
| 38 | + """Exact auxiliary integral; between endpoint crossings the length is affine.""" |
| 39 | + starts = [d - min(starts) for d in starts] |
| 40 | + cuts = {Q(1), 1 + eta} |
| 41 | + for x, y in combinations(sorted(starts), 2): |
| 42 | + twice_gap = 2 * (y - x) |
| 43 | + for j in range(ceil(twice_gap), floor((1 + eta) * twice_gap) + 1): |
| 44 | + cuts.add(Q(j) / twice_gap) |
| 45 | + cuts = sorted(cuts) |
| 46 | + return sum((b - a) * gate_intersection(starts, (a + b) / 2) |
| 47 | + for a, b in zip(cuts, cuts[1:])) / eta |
| 48 | + |
| 49 | + |
| 50 | +def check_conditional_estimate(): |
| 51 | + triples = [(Q(0), Q(3), Q(6)), (Q(5, 4), Q(17, 4), Q(43, 4)), |
| 52 | + (Q(0), Q(13), Q(41)), (Q(1, 3), Q(85, 3), Q(163, 3))] |
| 53 | + cases = 0 |
| 54 | + for starts, eta in product(triples, [Q(1, 3), HALF, Q(3, 4)]): |
| 55 | + pairs = list(combinations(starts, 2)) |
| 56 | + pair_prob = [uniform_w_intersection(pair, eta) for pair in pairs] |
| 57 | + for (x, y), p in zip(pairs, pair_prob): |
| 58 | + assert abs(p - Q(1, 4)) <= 1 / (16 * eta * abs(x - y)) |
| 59 | + p = uniform_w_intersection(starts, eta) |
| 60 | + assert p == sum(pair_prob) / 2 - Q(1, 4) |
| 61 | + d = min(y - x for x, y in pairs) |
| 62 | + assert p / (1 + eta) >= three_hit_floor(eta, d) > 0 |
| 63 | + cases += 1 |
| 64 | + print(f"{cases} exact conditional integrals passed the pair and triple bounds.") |
| 65 | + |
| 66 | + |
| 67 | +def relation(labels): |
| 68 | + """Primitive relation on the three phases, including the common intercept.""" |
| 69 | + x, y, z = labels |
| 70 | + coeff = (z - y, x - z, y - x) |
| 71 | + divisor = gcd(*coeff) |
| 72 | + return tuple(n // divisor for n in coeff) |
| 73 | + |
| 74 | + |
| 75 | +def all_hits_probability(labels, length, m, eta): |
| 76 | + """Exact probability that each of three entire rows hits the physical window.""" |
| 77 | + coeff = relation(labels) |
| 78 | + # Density and CDF of the sum of c_i U_i for independent U_i uniform on [0,1/2]. |
| 79 | + low = sum(min(0, c) * HALF for c in coeff) |
| 80 | + widths = [abs(c) * HALF for c in coeff] |
| 81 | + high = low + sum(widths) |
| 82 | + volume = prod(widths) |
| 83 | + knots = [(low + sum(included), (-1)**size) |
| 84 | + for size in range(4) for included in combinations(widths, size)] |
| 85 | + |
| 86 | + def density(z): |
| 87 | + return sum(sign * max(0, z - knot)**2 for knot, sign in knots) / (2 * volume) |
| 88 | + |
| 89 | + def distribution(z): |
| 90 | + return sum(sign * max(0, z - knot)**3 for knot, sign in knots) / (6 * volume) |
| 91 | + |
| 92 | + offset_sums = Counter(sum(c * j for c, j in zip(coeff, offsets)) |
| 93 | + for offsets in product(range(length), repeat=3)) |
| 94 | + start_spacing = 1 / (1 + eta) |
| 95 | + total = Q(0) |
| 96 | + for offset_sum, count in offset_sums.items(): |
| 97 | + a, b = sorted((Q(offset_sum), offset_sum * start_spacing)) |
| 98 | + for period in range(ceil((low - b) / m), floor((high - a) / m) + 1): |
| 99 | + shift = period * m |
| 100 | + if a == b: |
| 101 | + mean_density = density(a + shift) |
| 102 | + else: |
| 103 | + mean_density = (distribution(b + shift) - distribution(a + shift)) / (b - a) |
| 104 | + total += count * mean_density |
| 105 | + return total / (8 * m**2) |
| 106 | + |
| 107 | + |
| 108 | +def clip(polygon, linear): |
| 109 | + """Intersect a rational polygon with the half-plane linear(u,v) >= 0.""" |
| 110 | + out = [] |
| 111 | + for p, q in zip(polygon, polygon[1:] + polygon[:1]): |
| 112 | + a, b = linear(*p), linear(*q) |
| 113 | + if a >= 0: |
| 114 | + out.append(p) |
| 115 | + if a * b < 0: |
| 116 | + t = a / (a - b) |
| 117 | + out.append(tuple(pj + t * (qj - pj) for pj, qj in zip(p, q))) |
| 118 | + return out |
| 119 | + |
| 120 | + |
| 121 | +def area(polygon): |
| 122 | + return abs(sum((x * v - y * u for (x, y), (u, v) in |
| 123 | + zip(polygon, polygon[1:] + polygon[:1])), Q(0))) / 2 |
| 124 | + |
| 125 | + |
| 126 | +def separated_probability(labels, H, distance, m): |
| 127 | + """Area on the actual two-dimensional torus of the three row starts.""" |
| 128 | + c1, c2, c3 = relation(labels) |
| 129 | + lo = sum(min(0, c) for c in (c1, c2, c3)) * H |
| 130 | + hi = sum(max(0, c) for c in (c1, c2, c3)) * H |
| 131 | + square = [(Q(0), Q(0)), (H, Q(0)), (H, H), (Q(0), H)] |
| 132 | + total = Q(0) |
| 133 | + for period in range(ceil(lo / m), floor(hi / m) + 1): |
| 134 | + coord = (lambda x, y: x, lambda x, y: y, |
| 135 | + lambda x, y: (period * m - c1 * x - c2 * y) / c3) |
| 136 | + for first, mid, last in permutations(coord): |
| 137 | + polygon = clip(square, first) |
| 138 | + polygon = clip(polygon, lambda x, y: H - last(x, y)) |
| 139 | + polygon = clip(polygon, lambda x, y: mid(x, y) - first(x, y) - distance) |
| 140 | + polygon = clip(polygon, lambda x, y: last(x, y) - mid(x, y) - distance) |
| 141 | + total += area(polygon) |
| 142 | + return total / (m**2 * abs(c3)) |
| 143 | + |
| 144 | + |
| 145 | +def check_boxes(show_fractions): |
| 146 | + length, m, eta, distance = 16, 30, HALF, 3 |
| 147 | + H = (length - 1) / (1 + eta) |
| 148 | + tau = three_hit_floor(eta, distance) |
| 149 | + assert tau == Q(1, 24) |
| 150 | + assert 1 + 64 == 16 + 49 and PRIOR["is_sidon"](BOXES["integer Sidon"]) |
| 151 | + print("Box floor(10^6 E[hit triples]) floor(10^6 tau E[separated triples])") |
| 152 | + |
| 153 | + def report(name, hits, separated): |
| 154 | + print(f"{name:<14} {floor(10**6 * hits):>26} {floor(10**6 * tau * separated):>36}") |
| 155 | + if show_fractions: |
| 156 | + print(f" E[hit triples] = {hits}\n E[separated triples] = {separated}") |
| 157 | + |
| 158 | + for name, labels in BOXES.items(): |
| 159 | + hits = separated = Q(0) |
| 160 | + for triple in combinations(labels, 3): |
| 161 | + p = all_hits_probability(triple, length, m, eta) |
| 162 | + good = separated_probability(triple, H, distance, m) |
| 163 | + assert 0 < tau * good <= p <= (Q(length, 2 * m))**2 |
| 164 | + assert separated_probability(triple, Q(m), Q(0), m) == 1 |
| 165 | + # For single offsets and m larger than the support, the middle term in |
| 166 | + # c.U is uniform on an interval covering the sum of the other two terms. |
| 167 | + middle = abs(relation(triple)[1]) |
| 168 | + assert all_hits_probability(triple, 1, 127, eta) == Q(1, 4 * middle * 127**2) |
| 169 | + hits += p |
| 170 | + separated += good |
| 171 | + # Print short reproducible intervals; the preceding comparisons use full fractions. |
| 172 | + report(name, hits, separated) |
| 173 | + |
| 174 | + # On three independent real row generators, the three starts can be chosen |
| 175 | + # independently. In each of the six orders, removing the two gaps gives a simplex. |
| 176 | + hits = Q(length, 2 * m)**3 |
| 177 | + separated = (max(0, H - 2 * distance) / m)**3 |
| 178 | + assert (hits, separated) == (Q(64, 3375), Q(8, 3375)) |
| 179 | + assert hits >= tau * separated > 0 |
| 180 | + report("free generators", hits, separated) |
| 181 | + |
| 182 | + # For an AP, with j=0 in every row, two endpoint hits give exactly one good midpoint |
| 183 | + # out of its two possible residues. This independently fixes the density normalization. |
| 184 | + for mod in (3, 7, 30): |
| 185 | + assert all_hits_probability(BOXES["AP"], 1, mod, eta) == Q(1, 8 * mod**2) |
| 186 | + # At L=2 the AP offset sums 0, ±1, ±2 have masses 2, 2 each, 1 each. |
| 187 | + # The corresponding densities for a fixed h in [2/3,1] are 1, 2(1-h)^2, 0. |
| 188 | + assert all_hits_probability(BOXES["AP"], 2, m, eta) == Q(31, 108 * m**2) |
| 189 | + |
| 190 | + |
| 191 | +def check_lifts_and_count(): |
| 192 | + template, m = PRIOR["ruzsa"](5) |
| 193 | + assert PRIOR["is_sidon"](template, m) |
| 194 | + tested = 0 |
| 195 | + for labels in BOXES.values(): |
| 196 | + length = 5 |
| 197 | + for t, v, h, a in product([Q(0), Q(1, 3), Q(8, 3), Q(23, 4)], |
| 198 | + [Q(0), Q(1, 2), Q(19, 2)], |
| 199 | + [Q(2, 3), Q(5, 6), Q(1)], [Q(0), Q(3, 4)]): |
| 200 | + occupied = {} |
| 201 | + for x in labels: |
| 202 | + row_hits = set() |
| 203 | + for j in range(length): |
| 204 | + pos = (-x * t - v + h * (j + a)) % m |
| 205 | + z = floor(pos) |
| 206 | + if pos - z < HALF: |
| 207 | + assert z not in row_hits |
| 208 | + row_hits.add(z) |
| 209 | + occupied.setdefault(z, 2 * length * x + j) |
| 210 | + for shift in range(m): |
| 211 | + residues = [(z + shift) % m for z in template] |
| 212 | + assert PRIOR["is_sidon"]([occupied[z] for z in residues if z in occupied]) |
| 213 | + tested += 1 |
| 214 | + for k in range(3, 65): |
| 215 | + for s in range(k + 1): |
| 216 | + surplus = comb(s - 1, 2) if s >= 1 else 0 |
| 217 | + assert k * surplus >= 3 * comb(s, 3) |
| 218 | + assert Q(3 * comb(k, 3), k) >= Q(2, 3) * (comb(k, 2) - Q(k, 2)) |
| 219 | + for r in range(1, 4): |
| 220 | + labels = [sum(digit * 3**i for i, digit in enumerate(digits)) |
| 221 | + for digits in product(range(2), repeat=r)] |
| 222 | + length = len(labels) |
| 223 | + points = [2 * length * x + j for x in labels for j in range(length)] |
| 224 | + assert PRIOR["additive_energy"](points) == 6**r * (2 * length**3 + length) // 3 |
| 225 | + assert len({x - y for x, y in product(points, repeat=2)}) == 3**r * (2 * length - 1) |
| 226 | + print(f"Verified {tested} Sidon pullbacks, the surplus count through K = 64, and three digit boxes.") |
| 227 | + |
| 228 | + |
| 229 | +def check_constants(): |
| 230 | + def squared_coefficient(load): |
| 231 | + return load * ((11 - 2 * load) / 24)**2 |
| 232 | + |
| 233 | + base_sq = Q(4, 27) |
| 234 | + assert squared_coefficient(Q(4, 3)) == base_sq * Q(25, 24)**2 |
| 235 | + assert base_sq * Q(1, 24)**2 == Q(3, 108**2) |
| 236 | + best = squared_coefficient(Q(11, 6)) |
| 237 | + assert best == Q(1331, 7776) and best > Q(413, 1000)**2 > base_sq |
| 238 | + assert best / base_sq == Q(11, 48)**2 * 22 |
| 239 | + print("Gain at load 4/3 = sqrt(3)/108") |
| 240 | + print(f"Optimal coefficient squared = {best}") |
| 241 | + print("Optimal gain = (11 sqrt(22)/48 - 1) * 2/(3 sqrt(3))") |
| 242 | + |
| 243 | + k, length = 10**4, 10**8 |
| 244 | + n = k * length |
| 245 | + q = isqrt(6 * n // 11) |
| 246 | + while not PRIOR["is_prime"](q): |
| 247 | + q += 1 |
| 248 | + m, b = q * (q - 1), q - 1 |
| 249 | + eta, distance = Q(1, 100), 10**4 |
| 250 | + H = (length - 1) / (1 + eta) |
| 251 | + one, two = k * H / m, comb(k, 2) * (H / m)**2 |
| 252 | + close = comb(k, 2) * (H**2 - max(0, H - distance)**2) / m**2 |
| 253 | + surplus = three_hit_floor(eta, distance) * max(0, Q(2, 3) * (two - one / 2) - 3 * close) |
| 254 | + bound = b * (Q(n, 2 * m) - comb(k, 2) * Q(length, 2 * m)**2 + surplus) |
| 255 | + assert surplus > 0 and bound > 0 and bound**2 > Q(41, 100)**2 * n |
| 256 | + print(f"N = {n}, q = {q}, guaranteed size = {ceil(bound)}") |
| 257 | + |
| 258 | + |
| 259 | +if __name__ == "__main__": |
| 260 | + check_conditional_estimate() |
| 261 | + check_boxes("--fractions" in argv[1:]) |
| 262 | + check_lifts_and_count() |
| 263 | + check_constants() |