| | @@ -0,0 +1,193 @@ |
| 1 | +"""Exact rectangle bounds for the signed-difference tests in NOTES.md. Python 3.10+.""" |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from fractions import Fraction as Q |
| 5 | +from itertools import accumulate |
| 6 | +from math import isqrt |
| 7 | + |
| 8 | + |
| 9 | +@dataclass(frozen=True) |
| 10 | +class Test: |
| 11 | + inverse_deficit: int | str |
| 12 | + half_size: int |
| 13 | + reverse_weight: str |
| 14 | + lengths: str |
| 15 | + weights: str |
| 16 | + uniform: bool = False |
| 17 | + ramps: str = ".965 .94 .915 .905 .89" |
| 18 | + mesh: int = 64 |
| 19 | + |
| 20 | + |
| 21 | +TESTS = ( |
| 22 | + Test(100, 48, ".062", "50 20 9.25 5.4 2.25", "4.8 10.6 21.5 24.5 45", True), |
| 23 | + Test(80, 50, ".058", "40 18.4 9.2 5.5 2.4", "7.4 10.7 21.1 22.5 42", True), |
| 24 | + *(Test(r, 49, ".077", "40 18 7 3.5 1.35", "5.9 14.6 23.5 24.4 41.8") |
| 25 | + for r in range(79, 53, -1)), |
| 26 | + *(Test(r, 49, ".087", "44 22.8 8.3 4.6 2", "3.8 14.5 24.4 27.1 48.1") |
| 27 | + for r in range(53, 49, -1)), |
| 28 | + Test(45, 51, ".114", "43 23.2 8.7 5.1 1.9", "5.1 13.7 22.6 25.9 45.9"), |
| 29 | + Test(40, 55, ".095", "40 24 10.3 5.5 2.25", "4.2 16.5 25.75 27.7 48.4"), |
| 30 | + Test(32, 62, ".134", "32 19.6 17 6.1 2.3", "13 3.8 24.8 41.3 66.3"), |
| 31 | + Test(25, 70, ".158", "25 25 13.7 5.75 2.55", "5.2 22 40.7 32.5 54.1"), |
| 32 | + Test(20, 105, ".18", "20 19.8 10.9 5.5 3.3", "30.6 71.2 91 100 74"), |
| 33 | + Test(18, 145, ".192", "18 11.4 9.55 4.4 2.75", "212 72 126.5 64.3 121"), |
| 34 | + Test(16, 250, ".193", "16 16 8.7 8.7 2.8", "151 566 318 364 390"), |
| 35 | + Test(15, 280, ".152 .081 .803 .597 .334", "15 15 7.26 7.44 2.16", "181 1040 385 292 503", |
| 36 | + ramps=".974 .983 .841 1.018 .772"), |
| 37 | + Test(14, 600, ".012 .0001 1 1 .455", "14 14 7.74 7.74 1.063", "391 5789 310 2553 3737", |
| 38 | + ramps=".978 .978 .923 .923 .805", mesh=128), |
| 39 | + Test(13, 720, ".0026 .00001 .464 47.2 .0471", "13 13 2.41 7.28 1.53", |
| 40 | + "269 10978 26.3 107.7 4993", ramps=".975 .976 .762 .916 .806", mesh=128), |
| 41 | + Test("25/2", 2400, ".0026 .00001 .464 48.3 .0471", "12.5 12.5 2.317 7.007 1.465", |
| 42 | + "2992 128098 292.2 1222.1 55380", ramps=".9752 .9755 .7622 .9146 .8051", mesh=512), |
| 43 | +) |
| 44 | +SMOOTH = Q(1, 10**5) |
| 45 | +HOST = 10**14 |
| 46 | +VARIANCE_SLACK = Q(1, 1000) |
| 47 | +CUTOFF = Q(1, 10000) |
| 48 | +SCALE = 2**20 |
| 49 | + |
| 50 | + |
| 51 | +def score(w): |
| 52 | + # j(j-1) <= w <= j(j + 1) |
| 53 | + j = max(1, (1 + isqrt(1 + (4 * w.numerator) // w.denominator)) // 2) |
| 54 | + return j - 1 + w / j |
| 55 | + |
| 56 | + |
| 57 | +def floor_scaled(x): |
| 58 | + return (x.numerator * SCALE) // x.denominator |
| 59 | + |
| 60 | + |
| 61 | +def minimum_distance(lo, hi): |
| 62 | + """Distance from [lo, hi] to 0.""" |
| 63 | + assert lo <= hi |
| 64 | + return max(0, lo, -hi) |
| 65 | + |
| 66 | + |
| 67 | +def check(test): |
| 68 | + R, s, mesh = Q(test.inverse_deficit), test.half_size, test.mesh |
| 69 | + tau = 1 / R |
| 70 | + lengths = list(map(Q, test.lengths.split())) |
| 71 | + weights = list(map(Q, test.weights.split())) |
| 72 | + H = max(lengths) |
| 73 | + betas = list(map(Q, test.reverse_weight.split())) |
| 74 | + if len(betas) == 1: |
| 75 | + betas *= len(lengths) |
| 76 | + ramps = list(map(Q, test.ramps.split())) |
| 77 | + windows = list(zip(lengths, weights, betas, ramps, strict=True)) |
| 78 | + aggregate = [(length, c * (1 + b), a) for length, c, b, a in windows] |
| 79 | + assert min(lengths) > 0 and H <= R |
| 80 | + assert all(c > 0 and b >= 0 and a >= 0 for _, c, b, a in windows) |
| 81 | + if test.uniform: |
| 82 | + assert 2 * H <= R |
| 83 | + h, n = int(H * mesh), int(R * mesh) |
| 84 | + assert h == H * mesh and n == R * mesh and n % 2 == 0 |
| 85 | + k_lo = 1 - tau |
| 86 | + k_hi = 1 if test.uniform else k_lo + Q(1, HOST) |
| 87 | + g_max = score(max(sum(weights), sum(c * b for _, c, b, _ in windows))) |
| 88 | + c0 = sum(c for _, c, _ in aggregate) |
| 89 | + c1 = sum(c * length for length, c, _ in aggregate) |
| 90 | + c2 = sum( |
| 91 | + c * length**2 * (Q(1, 6) + (1 - a)**2 / 3) for length, c, a in aggregate |
| 92 | + ) |
| 93 | + p_mass = sum(c * a * length**2 / 3 for length, c, a in aggregate) |
| 94 | + cost = (c1 * (1 if test.uniform else 1 - tau / 2) + k_hi**2 * c2) / s |
| 95 | + variance_factor = (2 if test.uniform else 2 - tau) + VARIANCE_SLACK |
| 96 | + |
| 97 | + # Moving each atom by at most SMOOTH² * tau costs its test's total variation |
| 98 | + # times the maximum smoothed mass in an interval of twice this length. |
| 99 | + p_zero = sum(c * a * length / 2 for length, c, a in aggregate) |
| 100 | + total_variation = 4 * g_max + 2 * p_zero / s |
| 101 | + window_error = 1 / (HOST * SMOOTH**2) |
| 102 | + assert SMOOTH**2 + window_error + SMOOTH**2 * window_error < VARIANCE_SLACK |
| 103 | + assert 2 * (2 + VARIANCE_SLACK) <= 3**2 |
| 104 | + finite_error = total_variation * (2 * SMOOTH**2 + 3 * SMOOTH) + c0 / (2 * s * HOST) |
| 105 | + |
| 106 | + # Each entry lies between its stored integer / SCALE and (integer + 1) / SCALE. |
| 107 | + plus, minus, p = [], [], [] |
| 108 | + for j in range(h + 1): |
| 109 | + z = Q(j, mesh) |
| 110 | + w = sum(c * max(0, 1 - z / length) for length, c, _, _ in windows) |
| 111 | + back = sum(c * b * max(0, 1 - z / length) for length, c, b, _ in windows) |
| 112 | + ramp = sum( |
| 113 | + c * a * max(0, length - z)**2 / (2 * length) for length, c, a in aggregate |
| 114 | + ) |
| 115 | + plus.append(floor_scaled(score(w))) |
| 116 | + minus.append(floor_scaled(score(back))) |
| 117 | + p.append(floor_scaled(ramp)) |
| 118 | + # Beyond the support, zero is a lower bound and one fixed-point unit is an upper bound. |
| 119 | + p.extend([0] * (max(n, 2 * h) + 3)) |
| 120 | + measure = mesh * SCALE |
| 121 | + square_measure = measure * SCALE |
| 122 | + |
| 123 | + def rectangles(curve): |
| 124 | + lower = list(accumulate(curve[1:], initial=0)) |
| 125 | + upper = list(accumulate((v + 1 for v in curve[:-1]), initial=0)) |
| 126 | + second = list(accumulate(((v + 1)**2 for v in curve[:-1]), initial=0)) |
| 127 | + return lower, upper, second |
| 128 | + |
| 129 | + plus_lo, plus_hi, plus_sq = rectangles(plus) |
| 130 | + minus_lo, minus_hi, minus_sq = rectangles(minus) |
| 131 | + # 2 int p² + 2 int p(y)p(R-y). At greater inverse deficits the overlap decreases. |
| 132 | + p_square_sum = sum( |
| 133 | + (p[j] + 1) * ((p[j] + 1) + (p[n - j - 1] + 1)) for j in range(h) |
| 134 | + ) |
| 135 | + p_sq = Q(2 * p_square_sum, square_measure) |
| 136 | + worst = None |
| 137 | + cells = h if test.uniform else n // 2 |
| 138 | + for i in range(cells + int(test.uniform)): |
| 139 | + # Uniform rows have a separate last case x/tau >= H. |
| 140 | + far = test.uniform and i == h |
| 141 | + a_lo = h if test.uniform else min(h, n - i - 1) |
| 142 | + a_hi = h if test.uniform else min(h, n - i) |
| 143 | + b_lo, b_hi = min(h, i), min(h, i + 1) |
| 144 | + mass_lo = Q(plus_lo[a_lo] + minus_lo[b_lo], measure) |
| 145 | + mass_hi = Q(plus_hi[a_hi] + minus_hi[b_hi], measure) |
| 146 | + z_sq = Q(plus_sq[a_hi] + minus_sq[b_hi], square_measure) |
| 147 | + cross = 0 |
| 148 | + if not far: |
| 149 | + cross = sum(plus[j + 1] * p[i + j + 2] for j in range(a_lo)) |
| 150 | + cross += sum(minus[j + 1] * p[i + 1 - j] for j in range(b_lo)) |
| 151 | + if not test.uniform: |
| 152 | + cross += sum(plus[j + 1] * p[n - i - j] for j in range(a_lo)) |
| 153 | + cross += sum(minus[j + 1] * p[n - i + j + 1] for j in range(b_lo)) |
| 154 | + v = z_sq + k_hi**2 * p_sq / s**2 - 2 * k_lo * Q(cross, square_measure) / s |
| 155 | + if not test.uniform: |
| 156 | + centered_mass = minimum_distance( |
| 157 | + mass_lo - k_hi * p_mass / s, mass_hi - k_lo * p_mass / s |
| 158 | + ) |
| 159 | + v -= tau * centered_mass**2 |
| 160 | + surplus = k_lo * mass_lo - cost - s - CUTOFF * g_max - finite_error |
| 161 | + assert surplus > 0 and v >= 0 |
| 162 | + margin = surplus**2 - variance_factor * v |
| 163 | + if worst is None or margin < worst[0]: |
| 164 | + worst = (margin, i) |
| 165 | + margin, cell = worst |
| 166 | + assert margin > 0, (R, s, cell, margin) |
| 167 | + lower = (1000 * margin.numerator) // margin.denominator |
| 168 | + print(f"R={str(R):>4} s={s:3d} squared margin >= {lower}/1000 (cell {cell})") |
| 169 | + |
| 170 | + |
| 171 | +def check_covering_ranges(): |
| 172 | + """Extend each fixed-deficit certificate to the interval after the previous row.""" |
| 173 | + coefficients = {} |
| 174 | + previous = None |
| 175 | + for test in TESTS: |
| 176 | + inverse = Q(test.inverse_deficit) |
| 177 | + if previous is not None: |
| 178 | + assert inverse < previous |
| 179 | + if test.uniform: |
| 180 | + coefficient = 2 * test.half_size |
| 181 | + else: |
| 182 | + coefficient = max(coefficients[previous], 2 * test.half_size * previous / inverse) |
| 183 | + coefficients[inverse] = coefficient |
| 184 | + previous = inverse |
| 185 | + for inverse, expected in [(100, 96), (50, 100), (25, Q(896, 5)), (Q(25, 2), 4992)]: |
| 186 | + assert coefficients[inverse] == expected |
| 187 | + print(f"All positive deficits up to {1 / Q(inverse)}: K <= {expected}") |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + for test in TESTS: |
| 192 | + check(test) |
| 193 | + check_covering_ranges() |