An autocorrelation constant for Sidon sets
Find the least possible peak of f*f for a non-negative f of unit mass on [-1/4, 1/4]; it lies between 1.2802 and 1.50286.
| 1 | #!/usr/bin/env python3 |
| 2 | """Exact check of an upper bound on the autocorrelation constant C (constant 1a of Tao's Optimization Constants). |
| 3 | |
| 4 | 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 |
| 5 | 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 |
| 6 | w = 1/(2N) the cell width, f*f is continuous and piecewise linear with breakpoints at -1/2 + k w, where it equals |
| 7 | w c_{k-1}, c = a*a the discrete autoconvolution (c_{-1} = c_{2N-1} = 0). Hence |
| 8 | |
| 9 | 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. |
| 10 | |
| 11 | The heights are put over their least common denominator, and c is computed by packing the integer heights into one |
| 12 | integer, each in a slot wide enough that its square holds every c_k in a slot of its own (Kronecker substitution). No |
| 13 | floating-point number enters. Python >= 3.9, standard library only. |
| 14 | """ |
| 15 | from fractions import Fraction |
| 16 | from math import gcd, isfinite |
| 17 | |
| 18 | MAX_STEPS = 131072 |
| 19 | MAX_BITS = 1 << 24 # N times the bit length of the largest height over the common denominator |
| 20 | |
| 21 | |
| 22 | class CandidateError(ValueError): |
| 23 | pass |
| 24 | |
| 25 | |
| 26 | def height(value) -> Fraction: |
| 27 | """A JSON number (a float is read as the exact binary64 value it denotes) or a string such as "3/7" or "0.125".""" |
| 28 | if isinstance(value, bool) or not isinstance(value, (int, float, str)): |
| 29 | raise CandidateError(f"a height must be a number or a string, not {type(value).__name__}") |
| 30 | if isinstance(value, float) and not isfinite(value): |
| 31 | raise CandidateError("a height is not finite") |
| 32 | try: |
| 33 | x = Fraction(value) |
| 34 | except (ValueError, ZeroDivisionError): |
| 35 | raise CandidateError(f"unreadable height {value!r}") from None |
| 36 | if x < 0: |
| 37 | raise CandidateError(f"height {value} is negative") |
| 38 | return x |
| 39 | |
| 40 | |
| 41 | def ratio(candidate: dict) -> tuple[int, Fraction]: |
| 42 | """The number of steps and the exact ratio R; raises CandidateError if the candidate is not admissible.""" |
| 43 | values = candidate.get("heights") |
| 44 | if not isinstance(values, list) or not 1 <= len(values) <= MAX_STEPS: |
| 45 | raise CandidateError(f'"heights" must be a list of 1 to {MAX_STEPS} heights') |
| 46 | hs = [height(v) for v in values] |
| 47 | n, q = len(hs), 1 |
| 48 | for x in hs: |
| 49 | if q % x.denominator: |
| 50 | q = q * x.denominator // gcd(q, x.denominator) |
| 51 | if n * (q.bit_length() - 64) > MAX_BITS: |
| 52 | raise CandidateError("too fine to check: the heights' common denominator is too large") |
| 53 | A = [x.numerator * (q // x.denominator) for x in hs] |
| 54 | total = sum(A) |
| 55 | if total == 0: |
| 56 | raise CandidateError("all heights are 0") |
| 57 | bits = max(A).bit_length() |
| 58 | if n * bits > MAX_BITS: |
| 59 | raise CandidateError(f"too fine to check: {n} heights of {bits} bits over their common denominator; " |
| 60 | f"N times the bits may be at most {MAX_BITS}") |
| 61 | w = (2 * bits + n.bit_length()) // 8 + 1 # bytes per slot: every c_k < n 2^(2 bits) |
| 62 | X = int.from_bytes(b"".join(a.to_bytes(w, "little") for a in A), "little") |
| 63 | Y = (X * X).to_bytes(w * (2 * n - 1), "little") |
| 64 | top = max(int.from_bytes(Y[k * w:(k + 1) * w], "little") for k in range(2 * n - 1)) |
| 65 | return n, Fraction(2 * n * top, total * total) |
| 66 | |
| 67 | |
| 68 | def decimal_up(value: Fraction, digits: int = 12) -> str: |
| 69 | """value >= 0 in decimal, rounded up.""" |
| 70 | text = str(-(-value.numerator * 10 ** digits // value.denominator)).rjust(digits + 1, "0") |
| 71 | return f"{text[:-digits]}.{text[-digits:]}" |
