#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Route 39: an EXACT-RATIONAL certificate that closes the open window (24/5, 8]. Proposition 5 of `fold-arithmetic-bridge.md` section 4a gives, for every u > 4, c*_real(u) <= f_1(u/2)^2 (1 + 1/D_3(u)), D_3(u) = int_2^(u-1) log(v-1)/v dv, and on a piece (u_0, u_1] one bounds the right side by f_1(u_1/2)^2 (1 + 1/D_3(u_0)). The published table leaves `c*_real < 2` open exactly on (24/5, 6] and (6, 8]; return #781 showed the residue is an enclosure shortfall, using the exact identity D_3(u) = log(u-2) log(u-1) + pi^2/12 + Li_2(-(u-2)). (★) This script turns that into a certificate in exact rational arithmetic, with no floating point anywhere on the decision path: * log z for rational z > 1: z = m 2^k with m in [1,2), t = (m-1)/(m+1) in [0,1/3], log m = 2 sum_j t^(2j+1)/(2j+1) with a rational tail bound 2 t^(2N+1)/((2N+1)(1-t^2)); * log 2 the same way at t = 1/3; * pi^2/12 = (1/2) sum_n 1/n^2 with tail 1/(2N); * Li_2(-A) for rational A > 1 by the inversion formula Li_2(-A) = -pi^2/6 - (1/2) log(A)^2 - Li_2(-1/A), whose remaining series is alternating, so its partial sums bracket it exactly. Every quantity that must be UPPER bounded (f_1, e^gamma) uses an upper bound, and every quantity that must be LOWER bounded (D_3) uses a lower bound; the decision is a Fraction comparison with 2. Gates first: the rational machinery is checked against the floating evaluation of return #781, and the certificate's published table is reproduced with rational bounds before any new piece is claimed. """ import json import sys from fractions import Fraction as F sys.stdout.reconfigure(encoding='utf-8') EG_HI = F(9, 5) # certified upper bound for e^gamma, as the corpus certificate uses NLOG = 90 # terms for the log series (t <= 1/3) NLI2 = 260 # terms for the alternating dilogarithm series NP2 = 400 # terms for pi^2/12 def _log1p_series(t, n): """2 sum_{j=n} ...""" s = F(0) for j in range(n): s += F(2 * t ** (2 * j + 1), 2 * j + 1) tail = F(2) * t ** (2 * n + 1) / ((2 * n + 1) * (1 - t * t)) return s, tail def log_bounds(z, n=NLOG): """Rigorous (lo, hi) for log(z), z a Fraction > 1.""" z = F(z) if z <= 1: raise ValueError("log_bounds needs z > 1") k = 0 m = z while m >= 2: m /= 2 k += 1 t = (m - 1) / (m + 1) s, tail = _log1p_series(t, n) lo = s hi = s + tail if k: l2 = _log1p_series(F(1, 3), n) lo += k * l2[0] hi += k * (l2[0] + l2[1]) return lo, hi def pi2_over_12_bounds(n=NP2): s = F(0) for i in range(1, n + 1): s += F(1, i * i) lo = s / 2 hi = lo + F(1, 2 * n) return lo, hi def li2_neg_lo(A, n=NLI2): """Rigorous LOWER bound for Li_2(-A), A a Fraction > 1.""" A = F(A) p_lo, p_hi = pi2_over_12_bounds() p6_hi = 2 * p_hi la_lo, la_hi = log_bounds(A) s = F(0) for k in range(1, n + 1): s += F((-1) ** k, 1) / (A ** k * k * k) nxt = F(1, 1) / (A ** (n + 1) * (n + 1) ** 2) part_hi = max(s, s + nxt) # Li_2(-1/A) <= part_hi return -p6_hi - la_hi * la_hi / 2 - part_hi def d3_lo(u): """Lower bound for D_3(u) from (★), u a Fraction > 3.""" u = F(u) A = u - 2 p_lo, _ = pi2_over_12_bounds() lt_lo, _ = log_bounds(A) # log(u-2) l1_lo, _ = log_bounds(u - 1) # log(u-1) return lt_lo * l1_lo + p_lo + li2_neg_lo(A) def d3_hi(u): u = F(u) A = u - 2 _, p_hi = pi2_over_12_bounds() _, lt_hi = log_bounds(A) _, l1_hi = log_bounds(u - 1) # Li_2(-A) upper bound: symmetric construction p_lo, _ = pi2_over_12_bounds() la_lo, _ = log_bounds(A) s = F(0) for k in range(1, NLI2 + 1): s += F((-1) ** k, 1) / (A ** k * k * k) nxt = F(1, 1) / (A ** (NLI2 + 1) * (NLI2 + 1) ** 2) return lt_hi * l1_hi + p_hi - 2 * p_lo - la_lo * la_lo / 2 - min(s, s + nxt) def f1_sq_hi(u_half, tail=False): """Upper bound for f_1(s)^2 with s = u_half, as the certificate uses it.""" if tail: return F(1) s = F(u_half) if s <= 2: return F(0) _, lh = log_bounds(s - 1) x = 2 * EG_HI * lh / s x = min(F(1), x) return x * x def bound_hi(u0, u1, tail=False): """Upper bound for c*_real on the piece (u0, u1].""" f = F(1) if (tail or u1 is None) else f1_sq_hi(F(u1) / 2) D = d3_lo(F(u0)) return f * (1 + 1 / D) def main(): out = {} print("=" * 78) print("0. GATES: the rational machinery against the floating evaluation of #781") print("=" * 78) float_d3 = {F(24, 5): 0.351504406413, F(5): 0.406091633495, F(6): 0.683681939006, F(7): 0.956913527102, F(8): 1.219127918173} ok = True for u, ref in float_d3.items(): lo, hi = d3_lo(u), d3_hi(u) inside = float(lo) <= ref <= float(hi) ok = ok and inside print(" D_3(%-5s) in [%.9f, %.9f] contains the float value %.9f : %s" % (u, float(lo), float(hi), ref, "yes" if inside else "NO")) print(" enclosure widths: %s" % ", ".join("%.1e" % float(d3_hi(u) - d3_lo(u)) for u in float_d3)) print(" gate: %s" % ("rational enclosures contain the computed values" if ok else "FAILED")) out["gate_d3_contains_float"] = ok print() print(" the certificate's published table reproduced with rational bounds (upper bounds):") pub = [(F(4), F(22, 5), F(781, 1000)), (F(22, 5), F(24, 5), F(1491, 1000)), (F(24, 5), F(6), F(3120, 1000)), (F(6), F(8), F(2899, 1000))] gate_rows = [] for u0, u1, claimed in pub: b = bound_hi(u0, u1) # the certificate's own D_3 device, for comparison on the same piece gate_rows.append(dict(piece="(%s,%s]" % (u0, u1), published=str(claimed), published_float=float(claimed), rational_hi=str(b), rational_hi_float=float(b), tighter_than_published=float(b) <= float(claimed) + 1e-9, closes=bool(b < 2))) print(" (%s,%s] published %.4f rational upper bound %.4f %s (tighter certificate: %s)" % (u0, u1, float(claimed), float(b), "<= 2" if b < 2 else "> 2", "yes" if float(b) <= float(claimed) + 1e-9 else "NO")) out["gate_published_table"] = gate_rows tail = bound_hi(F(8), None, tail=True) print(" (8,infinity) published 1971/1000 rational upper bound %.4f" % float(tail)) print() print("=" * 78) print("1. THE REFINED CERTIFICATE: (24/5, 8] in 7 pieces, exact rationals") print("=" * 78) print(" sub-piece f_1(u_1/2)^2 (hi) D_3(u_0) (lo) bound (hi) < 2 ?") rows = [] pieces = [(F(24, 5), F(5)), (F(5), F(11, 2)), (F(11, 2), F(6)), (F(6), F(13, 2)), (F(13, 2), F(7)), (F(7), F(15, 2)), (F(15, 2), F(8))] for u0, u1 in pieces: f = f1_sq_hi(u1 / 2) D = d3_lo(u0) b = f * (1 + 1 / D) rows.append(dict(u0=str(u0), u1=str(u1), f1_sq_hi=str(f), d3_lo=str(D), d3_lo_float=float(D), bound_hi=str(b), bound_float=float(b), closes=bool(b < 2))) print(" (%s,%s] %.6f %.9f %.6f %s" % (u0, u1, float(f), float(D), float(b), "YES" if b < 2 else "no")) out["refined_rational"] = rows allc = all(r["closes"] for r in rows) print() print(" => %s" % ("ALL SEVEN pieces are below 2 in exact rational arithmetic." if allc else "a piece still fails.")) print(" With (4, 4.8] and (8, infinity) already certified by Proposition 5's own table, this") print(" gives c*_real(u) < 2 for EVERY u > 4, with no parity hypothesis and no deduction of") print(" any kind." if allc else " the residue is not closed by this partition.") out["closes_window"] = allc print() print(" margins (2 - bound, as a fraction of 2):") for r in rows: print(" (%s,%s] margin = %.4f = %.2f %% of the threshold" % (r["u0"], r["u1"], 2 - r["bound_float"], 100 * (2 - r["bound_float"]) / 2)) with open("work/route39-rational.json", "w", encoding="utf-8") as fh: json.dump(out, fh, ensure_ascii=False, indent=1, sort_keys=True) print() print("wrote work/route39-rational.json") return 0 if __name__ == "__main__": raise SystemExit(main())