#!/usr/bin/env python3 """Exact check of an upper bound on the autocorrelation constant C (constant 1a of Tao's Optimization Constants). C is the infimum of max_t (f*f)(t) / (int f)^2 over nonnegative f supported on [-1/4, 1/4], not identically zero. A candidate is a step function {"heights": [a_1, ..., a_N]}: f = a_i on the i-th of N equal cells of [-1/4, 1/4]. With w = 1/(2N) the cell width, f*f is continuous and piecewise linear with breakpoints at -1/2 + k w, where it equals w c_{k-1}, c = a*a the discrete autoconvolution (c_{-1} = c_{2N-1} = 0). Hence R = max_t (f*f)(t) / (int f)^2 = w max_k c_k / (w sum_i a_i)^2 = 2N max_k c_k / (sum_i a_i)^2 >= C. The heights are put over their least common denominator, and c is computed by packing the integer heights into one integer, each in a slot wide enough that its square holds every c_k in a slot of its own (Kronecker substitution). No floating-point number enters. Python >= 3.9, standard library only. """ from fractions import Fraction from math import gcd, isfinite MAX_STEPS = 131072 MAX_BITS = 1 << 24 # N times the bit length of the largest height over the common denominator class CandidateError(ValueError): pass def height(value) -> Fraction: """A JSON number (a float is read as the exact binary64 value it denotes) or a string such as "3/7" or "0.125".""" if isinstance(value, bool) or not isinstance(value, (int, float, str)): raise CandidateError(f"a height must be a number or a string, not {type(value).__name__}") if isinstance(value, float) and not isfinite(value): raise CandidateError("a height is not finite") try: x = Fraction(value) except (ValueError, ZeroDivisionError): raise CandidateError(f"unreadable height {value!r}") from None if x < 0: raise CandidateError(f"height {value} is negative") return x def ratio(candidate: dict) -> tuple[int, Fraction]: """The number of steps and the exact ratio R; raises CandidateError if the candidate is not admissible.""" values = candidate.get("heights") if not isinstance(values, list) or not 1 <= len(values) <= MAX_STEPS: raise CandidateError(f'"heights" must be a list of 1 to {MAX_STEPS} heights') hs = [height(v) for v in values] n, q = len(hs), 1 for x in hs: if q % x.denominator: q = q * x.denominator // gcd(q, x.denominator) if n * (q.bit_length() - 64) > MAX_BITS: raise CandidateError("too fine to check: the heights' common denominator is too large") A = [x.numerator * (q // x.denominator) for x in hs] total = sum(A) if total == 0: raise CandidateError("all heights are 0") bits = max(A).bit_length() if n * bits > MAX_BITS: raise CandidateError(f"too fine to check: {n} heights of {bits} bits over their common denominator; " f"N times the bits may be at most {MAX_BITS}") w = (2 * bits + n.bit_length()) // 8 + 1 # bytes per slot: every c_k < n 2^(2 bits) X = int.from_bytes(b"".join(a.to_bytes(w, "little") for a in A), "little") Y = (X * X).to_bytes(w * (2 * n - 1), "little") top = max(int.from_bytes(Y[k * w:(k + 1) * w], "little") for k in range(2 * n - 1)) return n, Fraction(2 * n * top, total * total) def decimal_up(value: Fraction, digits: int = 12) -> str: """value >= 0 in decimal, rounded up.""" text = str(-(-value.numerator * 10 ** digits // value.denominator)).rjust(digits + 1, "0") return f"{text[:-digits]}.{text[-digits:]}"