| | @@ -1,7 +1,7 @@ |
| 1 | | −"""Exact checks for the two numerical deductions in gap.tex. Run with python3 -B.""" |
| 1 | +"""Exact arithmetic for the finite and limiting coefficients in gap.tex.""" |
| 2 | 2 | |
| 3 | 3 | from fractions import Fraction |
| 4 | | −from math import ceil |
| 4 | +from math import ceil, lcm |
| 5 | 5 | from pathlib import Path |
| 6 | 6 | from runpy import run_path |
| 7 | 7 | |
| | @@ -9,32 +9,37 @@ from runpy import run_path |
| 9 | 9 | KAPPA = Fraction(31, 32) |
| 10 | 10 | LAMBDA_MIN = Fraction(4, 10**6) |
| 11 | 11 | OUTER_EXCESS = Fraction(7, 32) |
| 12 | +COUPLED_S = Fraction(6997, 1000) |
| 12 | 13 | |
| 13 | 14 | |
| 14 | | −def check_limit(slope, root_bracket, coefficient_bracket): |
| 15 | +def check_limit(slope, excess, expected_polynomial, root_bracket, coefficient_bracket, endpoint_bound): |
| 15 | 16 | """Bracket the crossover root and the resulting coefficient by rational signs.""" |
| 16 | 17 | root_lower, root_upper = map(Fraction, root_bracket) |
| 17 | 18 | coefficient_lower, coefficient_upper = map(Fraction, coefficient_bracket) |
| 18 | 19 | |
| 20 | + # At the crossover the mean is (1 + excess)q - excess(1 + 1/slope)q². |
| 21 | + coefficients = (excess * (slope + 1) / slope, -(1 + excess), KAPPA) |
| 22 | + scale = lcm(*(value.denominator for value in coefficients)) |
| 23 | + leading, linear, constant = (scale * value for value in coefficients) |
| 24 | + assert (leading, linear, constant) == expected_polynomial |
| 25 | + |
| 19 | 26 | def polynomial(q): |
| 20 | | − return 7 * (slope + 1) * q**2 - 39 * slope * q + 31 * slope |
| 27 | + return (leading * q + linear) * q + constant |
| 21 | 28 | |
| 22 | 29 | assert 0 < root_lower < root_upper < 1 |
| 23 | | − assert 14 * (slope + 1) < 39 * slope # strictly decreasing on [0, 1] |
| 30 | + assert leading > 0 and 2 * leading + linear < 0 |
| 24 | 31 | assert polynomial(root_lower) > 0 > polynomial(root_upper) |
| 25 | | − assert LAMBDA_MIN < OUTER_EXCESS / slope < Fraction(1, 100) |
| 32 | + assert LAMBDA_MIN < excess / slope < Fraction(1, 100) |
| 26 | 33 | high_endpoint = root_upper + slope * (1 - root_lower) * LAMBDA_MIN |
| 27 | | − assert high_endpoint < Fraction("0.96313") < KAPPA |
| 34 | + assert high_endpoint < Fraction(endpoint_bound) < KAPPA |
| 28 | 35 | assert coefficient_lower**2 * root_upper < KAPPA**2 |
| 29 | 36 | assert KAPPA**2 < coefficient_upper**2 * root_lower |
| 30 | | − print(f"D={slope}: {root_bracket[0]} < q* < {root_bracket[1]}, " |
| 37 | + print(f"Width excess {excess}, slope {slope}: {root_bracket[0]} < q* < {root_bracket[1]}, " |
| 31 | 38 | f"{coefficient_bracket[0]} < coefficient < {coefficient_bracket[1]}.") |
| 32 | 39 | |
| 33 | 40 | |
| 34 | | −def check_finite_host(): |
| 41 | +def check_finite_host(threshold): |
| 35 | 42 | """Use the accepted three-pass count with a single concentration bound.""" |
| 36 | | − experiments = Path(__file__).resolve().parents[1] |
| 37 | | − threshold = run_path(str(experiments / "upper-threshold-k7" / "check.py")) |
| 38 | 43 | q = (KAPPA * 78 / 77)**2 |
| 39 | 44 | delta = 1 - q |
| 40 | 45 | minimum_m = 2**256 |
| | @@ -70,9 +75,119 @@ def check_finite_host(): |
| 70 | 75 | assert finite_mean == limiting_mean + q**2 * rounding / 95 |
| 71 | 76 | |
| 72 | 77 | |
| 78 | +def check_coupled_cover(): |
| 79 | + """Evaluate the incidence losses for the two allocation tables at m = 2^240.""" |
| 80 | + mesh = 16 |
| 81 | + tent_masses = (4, 2, 7, 10, 5, 37, 10) |
| 82 | + assert sum(tent_masses) == 75 |
| 83 | + peak = Fraction(mesh, 4) + sum( |
| 84 | + Fraction(mass, 100 * h) for h, mass in enumerate(tent_masses, 1) |
| 85 | + ) |
| 86 | + assert peak == Fraction(2929, 700) |
| 87 | + epsilon = Fraction(1, 1250) |
| 88 | + m = 2**240 |
| 89 | + eighth_root = 2**30 |
| 90 | + for R, initial, pool, heavy, discard, error_bound, pairs in ( |
| 91 | + (32, 5, Fraction(1, 3), 18, 183, "0.001461", 131841), |
| 92 | + (100, 2, Fraction(1, 10), 12, 49, "0.003819", 1282401), |
| 93 | + ): |
| 94 | + n = mesh * R |
| 95 | + assert (n + 1) * (n + 2) // 2 == pairs |
| 96 | + assert (COUPLED_S + epsilon) / R + Fraction(1, m) < pool |
| 97 | + assert R * (3 * pool * (1 + pool))**2 / heavy**4 < epsilon |
| 98 | + assert Fraction(32 * initial * R, eighth_root) < Fraction(1, 100000) |
| 99 | + error = R**2 * peak * (Fraction(1, m) + Fraction(2 * discard, eighth_root)) |
| 100 | + assert error < Fraction(error_bound) < Fraction(1, 250) |
| 101 | + assert COUPLED_S + epsilon - 1 + Fraction(R, m) < Fraction(2999, 500) |
| 102 | + print(f"R={R}: incidence error < {error_bound} < 1/250, K = 2999/500.") |
| 103 | + return peak |
| 104 | + |
| 105 | + |
| 106 | +def log_near_one(value): |
| 107 | + """Fourth and fifth partial sums bracket log(1 + t) for these tilts.""" |
| 108 | + t = value - 1 |
| 109 | + assert 0 <= t <= Fraction(1, 1000) |
| 110 | + lower = t - t**2 / 2 + t**3 / 3 - t**4 / 4 |
| 111 | + return lower, lower + t**5 / 5 |
| 112 | + |
| 113 | + |
| 114 | +def check_coupled_host(threshold, peak): |
| 115 | + """Evaluate the new last pass and enclose both endpoint rates at the first host.""" |
| 116 | + gamma = Fraction(986629, 10**6) |
| 117 | + q = (KAPPA / gamma)**2 |
| 118 | + m = 2**256 |
| 119 | + e = Fraction(1, m) |
| 120 | + first_n = ceil(q * m**2) |
| 121 | + assert Fraction(1, 2) < q < 1 and q * gamma**2 == KAPPA**2 |
| 122 | + assert (first_n - 1) // q < m**2 == first_n // q |
| 123 | + assert first_n <= m**2 <= 2 * first_n and gamma < Fraction(74, 75) |
| 124 | + assert q * m**2 <= first_n < q * (m**2 + 1) < first_n + 1 |
| 125 | + |
| 126 | + # The three counting fractions use only the input width to each pass. |
| 127 | + residuals, count, old_width, record, independence = threshold["costs"](first_n, q) |
| 128 | + assert residuals[0] < Fraction("-.028") and residuals[1] < Fraction("-.171") |
| 129 | + assert count < Fraction("1.4012e-9") |
| 130 | + middle, last = threshold["PASSES"][1:] |
| 131 | + xi = threshold["ROUNDING"] |
| 132 | + assert xi == Fraction(1, 2**74) |
| 133 | + |
| 134 | + def set_aside(cuts): |
| 135 | + return 2 * (cuts.beta * cuts.heavy / (cuts.heavy**2 - 2 * cuts.beta))**2 |
| 136 | + |
| 137 | + second_outsiders = Fraction(1, 2) + set_aside(middle) + xi |
| 138 | + last_outsiders = COUPLED_S / 32 + set_aside(last) + xi |
| 139 | + width = KAPPA + last_outsiders + xi |
| 140 | + assert 0 < last_outsiders < min(last.beta, second_outsiders) |
| 141 | + assert second_outsiders < Fraction(".51") |
| 142 | + assert width == old_width - (threshold["S"] - COUPLED_S) / 32 |
| 143 | + assert width < Fraction("1.187406386") < 2 |
| 144 | + D, T, V = last.degree, last.triples, last.quadruples |
| 145 | + P = 2 * D |
| 146 | + insert = Fraction(D + P, T) + Fraction(3 * T + (P + 3) * (D + P), V) |
| 147 | + assert 0 < insert < 1 |
| 148 | + c_plus = (Fraction(1, D) + insert) / (1 - insert) |
| 149 | + c_minus = (Fraction(1, D) + Fraction(P, T) + Fraction(2 * T + (P + 3) * P, V)) / (1 - insert) |
| 150 | + last_record = c_plus * second_outsiders - c_minus * last_outsiders |
| 151 | + assert 0 < last_record < c_plus * Fraction(".51") < Fraction(1, 32 * 100000) |
| 152 | + discarded = ((4 * last.heavy * last.beta * D + 2) / 2**128 |
| 153 | + + last.beta * (17 * D + 2 * V + 12 * T + 2) / m) |
| 154 | + assert 32**2 * peak * (e + 2 * discarded) < Fraction("2.62e-20") < Fraction(1, 250) |
| 155 | + |
| 156 | + f = Fraction(64, 2**40) |
| 157 | + x0 = 1 + Fraction(52, 2**32) |
| 158 | + assert record <= f < KAPPA and independence <= x0 |
| 159 | + crossing = (width - 1) / 95 |
| 160 | + assert LAMBDA_MIN < crossing < Fraction(1, 100) |
| 161 | + assert 1 + 95 * crossing + e == width + e |
| 162 | + tilt = Fraction(10003, 10000) |
| 163 | + A = 1 - q + q * tilt |
| 164 | + B = (1 - q)**2 + (1 - (1 - q)**2) * tilt |
| 165 | + assert A**2 > B > A > 1 |
| 166 | + log_y = log_near_one(tilt) |
| 167 | + log_A = log_near_one(A) |
| 168 | + log_B = log_near_one(B) |
| 169 | + for x, u, left, right in ( |
| 170 | + (1 - crossing, width + e, "1.40947e-9", "1.40948e-9"), |
| 171 | + (x0, 1 + 95 * LAMBDA_MIN + e, "1.3935e-6", "1.3936e-6"), |
| 172 | + ): |
| 173 | + assert x <= u <= 2 * x |
| 174 | + lower = (KAPPA - f) * log_y[0] - (2 * x - u) * log_A[1] - (u - x) * log_B[1] |
| 175 | + upper = (KAPPA - f) * log_y[1] - (2 * x - u) * log_A[0] - (u - x) * log_B[0] |
| 176 | + assert count < Fraction("1.4012e-9") < Fraction(left) < lower <= upper < Fraction(right) |
| 177 | + print(f"0.986629 from 2^512: log count < 1.4012e-9, {left} < corner rate < {right}.") |
| 178 | + |
| 179 | + |
| 73 | 180 | if __name__ == "__main__": |
| 74 | 181 | if not __debug__: |
| 75 | 182 | raise RuntimeError("Run the arithmetic checks with assertions enabled.") |
| 76 | | − check_limit(96, ("0.96308683", "0.96308684"), ("0.98714054", "0.98714056")) |
| 77 | | − check_limit(95, ("0.96311489", "0.96311490"), ("0.98712616", "0.98712618")) |
| 78 | | − check_finite_host() |
| 183 | + experiments = Path(__file__).resolve().parents[1] |
| 184 | + threshold = run_path(str(experiments / "upper-threshold-k7" / "check.py")) |
| 185 | + check_limit(96, OUTER_EXCESS, (679, -3744, 2976), |
| 186 | + ("0.96308683", "0.96308684"), ("0.98714054", "0.98714056"), "0.96313") |
| 187 | + check_limit(95, OUTER_EXCESS, (672, -3705, 2945), |
| 188 | + ("0.96311489", "0.96311490"), ("0.98712616", "0.98712618"), "0.96313") |
| 189 | + check_finite_host(threshold) |
| 190 | + peak = check_coupled_cover() |
| 191 | + check_coupled_host(threshold, peak) |
| 192 | + check_limit(95, (COUPLED_S - 1) / 32, (575712, -3609715, 2945000), |
| 193 | + ("0.96409665161", "0.96409665162"), ("0.98662343", "0.98662345"), "0.96412") |