#!/usr/bin/env python3 """Rosser--Iwaniec / Ankeny--Onishi delay-equation instruments, plus the project's D_k class densities. Stdlib only (`math`), deterministic, no top-level side effects. Promoted from job #4460 (route 36 step check); the same equations are the served instrument `parity_gate.py` of return #1787 and the class definitions of return #101's `fold-arithmetic-bridge.md` sec. 2, section 3a. Objects ------- F_1, f_1 Rosser--Iwaniec linear sieve functions, from their own delay system (sF)' = f(s-1), (sf)' = F(s-1), F = 2e^g/s on (0,3], f = 0 on (0,2] (Wu, Acta Arith. 114 (2004) (2.6)). f_1 <= 1 <= F_1, f_1 increasing, F_1 decreasing, both -> 1. sigma_2(s) Ankeny--Onishi dimension-2 function, from (s^-2 sigma)' = -2 s^-3 sigma(s-2), sigma = e^-2g s^2/8 on (0,2]. F_2 := 1/sigma_2 is the dimension-2 upper-sieve constant. D_k, rho_odd, rho_even the project's fold densities (fold-arithmetic-bridge sec. 2): D_1 = 1, D_k(u) = int_{k-1}^{u-1} D_{k-1}(v) dv/v; rho_odd = sum_{k odd} D_k, rho_even = sum_{k even} D_k. The route-36 test statistic is, at a common distribution level Q = X^theta (sieving level z = X^(1/u), so the linear-sieve argument is s = theta*u), R_theta(u) = f_1(theta u)^2 rho_odd(u) / ( F_2(u) (rho_odd(u) - 1) ) and the test is R_theta(u) > c_eff(theta) := 2/theta. Two exact structural facts used by `theta_ratio`'s callers: (S-direction) the honest dimension-2 constant at level theta is 1/sigma_2(theta u), which for theta <= 1 is >= 1/sigma_2(u) because sigma_2 is nondecreasing and <= 1; R_theta is decreasing in F_2, so evaluating R at theta < 1 while holding F_2 at its level-1 value OVERSTATES R_theta. At theta = 1 the value used is the honest one. (f_1 floor) (s f_1)' = F_1(s-1) and 1 <= F_1, f_1 <= 1 give s f_1'(s) = F_1(s-1) - f_1(s) >= 1 - f_1(s) >= 0, so f_1 is nondecreasing on [2, inf); hence f_1(u) >= f_1(4) = 2 e^g log 3 / 4 for every u >= 4, and rho_odd - 1 >= D_3 gives, for F_2 = 1, R_1(u) >= f_1(4)^2 (1 + 1/D_3(u)). Boundary: this is exact numerical evaluation of the standard delay equations (calibration rung *verified*, not proven), and the identities quoted from Sections 2/3a of the served note. Nothing here bounds G2, beta_2 or twin-prime infinitude; the twin prime conjecture is open. """ import math GAMMA = 0.57721566490153286060651209 EG = math.exp(GAMMA) # e^gamma INV_E2G = math.exp(-2 * GAMMA) # e^{-2 gamma} # The linear-sieve optimum min_s s F_1(s) e^-gamma = 2, attained at s <= 3 # (return #1787 sec. 2); the parity floor of Selberg's example. PARITY_FLOOR = 2.0 def _cumint(vals, start_idx, h, n): out = [0.0] * (n + 1) acc = 0.0 for i in range(start_idx + 1, n + 1): acc += 0.5 * h * (vals[i - 1] + vals[i]) out[i] = acc return out def evaluate(per_unit=4000, u_max=16.0, k_max=None): """Evaluate the delay systems on a uniform grid of step 1/per_unit. Returns a dict with `u`, the step `h`, `per_unit`, `u_max`, `at(arr, x)`, and the arrays `F`, `f`, `sigma2`, `D` (dict k -> list), `rho_odd`, `rho_even`. `per_unit` must be a positive integer so that the shift u -> u-1 is an exact index shift. """ if not isinstance(per_unit, int) or per_unit <= 0: raise ValueError("per_unit must be a positive integer") h = 1.0 / per_unit n = int(round(u_max * per_unit)) u = [i * h for i in range(n + 1)] i1, i2, i3 = per_unit, 2 * per_unit, 3 * per_unit # ---- F_1, f_1 from their own delay system ---- F = [0.0] * (n + 1) f = [0.0] * (n + 1) sF = [0.0] * (n + 1) sf = [0.0] * (n + 1) for i in range(1, n + 1): if i <= i3: sF[i] = 2 * EG for i in range(i2 + 1, n + 1): sf[i] = sf[i - 1] + 0.5 * h * (sF[i - 1 - per_unit] / u[i - 1 - per_unit] + sF[i - per_unit] / u[i - per_unit]) if i > i3: sF[i] = sF[i - 1] + 0.5 * h * (sf[i - 1 - per_unit] / u[i - 1 - per_unit] + sf[i - per_unit] / u[i - per_unit]) for i in range(1, n + 1): F[i] = sF[i] / u[i] f[i] = sf[i] / u[i] # ---- sigma_2 ---- sig = [0.0] * (n + 1) g2 = [0.0] * (n + 1) for i in range(1, n + 1): if i <= i2: sig[i] = INV_E2G * u[i] ** 2 / 8 g2[i] = sig[i] / u[i] ** 2 for i in range(i2 + 1, n + 1): a = 2 * sig[i - 1 - i2] / u[i - 1] ** 3 if i - 1 - i2 > 0 else 0.0 b = 2 * sig[i - i2] / u[i] ** 3 g2[i] = g2[i - 1] - 0.5 * h * (a + b) sig[i] = g2[i] * u[i] ** 2 # ---- project fold densities D_k ---- if k_max is None: k_max = int(u_max) + 1 D = {1: [1.0 if i >= i1 else 0.0 for i in range(n + 1)]} for k in range(2, k_max + 1): prev = D[k - 1] integrand = [prev[i] / u[i] if u[i] > 0 else 0.0 for i in range(n + 1)] lo = (k - 1) * per_unit I = _cumint(integrand, lo, h, n) cur = [0.0] * (n + 1) for i in range(n + 1): j = i - per_unit if j > lo: cur[i] = I[j] D[k] = cur rho_odd = [sum(D[k][i] for k in D if k % 2 == 1) for i in range(n + 1)] rho_even = [sum(D[k][i] for k in D if k % 2 == 0) for i in range(n + 1)] def at(arr, x): j = int(round(x * per_unit)) if j < 0 or j > n: raise IndexError("u=%r outside [0, %r]" % (x, u_max)) return arr[j] return {"u": u, "h": h, "per_unit": per_unit, "u_max": u_max, "n": n, "F": F, "f": f, "sigma2": sig, "D": D, "rho_odd": rho_odd, "rho_even": rho_even, "at": at} # ---------------------------------------------------------------- closed forms def f1_closed(s): """f_1(s) = 2 e^gamma log(s-1)/s, the exact closed form on [2,4].""" if not (2 <= s <= 4): raise ValueError("closed form is valid on [2,4] only") return 2 * EG * math.log(s - 1) / s def F1_closed(s): """F_1(s) = 2 e^gamma/s, the exact closed form on (0,3].""" if not (0 < s <= 3): raise ValueError("closed form is valid on (0,3] only") return 2 * EG / s def f1_floor(): """f_1(4) = 2 e^gamma log 3 / 4; a lower bound for f_1(u), u >= 4.""" return 2 * EG * math.log(3) / 4 def d3_closed_one_cell(u): """D_3(u) = int_2^{u-1} log(v-1)/v dv by quadrature (no closed form).""" if u <= 3: return 0.0 a, b = 2.0, u - 1.0 m = 1 << 12 h = (b - a) / m tot = 0.5 * (math.log(a - 1) / a + math.log(b - 1) / b) for i in range(1, m): v = a + i * h tot += math.log(v - 1) / v return tot * h # ------------------------------------------------------------------- the test def c_eff(theta): """The contamination constant at distribution level Q = X^theta: 2/theta.""" if theta <= 0: raise ValueError("theta must be positive") return PARITY_FLOOR / theta def theta_ratio(env, theta, uu, f2="one"): """R_theta(u) and its ratio to c_eff(theta). `f2` is "one" for F_2 = 1 or "sigma2" for F_2 = 1/sigma_2(u) (the *level-1* value; for theta < 1 the honest constant is 1/sigma_2(theta u) >= this, so the returned ratio is an upper bound -- see the module docstring). """ at = env["at"] v = at(env["f"], theta * uu) ** 2 ro = at(env["rho_odd"], uu) v *= ro / (ro - 1) if f2 == "sigma2": v *= at(env["sigma2"], uu) elif f2 != "one": raise ValueError("f2 must be 'one' or 'sigma2'") ce = c_eff(theta) return v, v / ce def theta_profile(env, theta, f2="one", u_lo=4.0, u_hi=16.0): """Full grid profile of R_theta/c_eff over (u_lo, u_hi]. Returns dict with max ratio and its argmax, the passing set's u-range and the *minimum* ratio attained inside the passing set (the tight cell) -- the last is the quantity a directed certificate has to reach. """ pu = env["per_unit"] i_lo = int(math.ceil(u_lo * pu)) + 1 i_hi = int(min(env["u_max"], u_hi) * pu) best = (-1.0, None) passing = [] for i in range(i_lo, i_hi + 1): uu = env["u"][i] _, r = theta_ratio(env, theta, uu, f2) if r > best[0]: best = (r, uu) if r > 1.0: passing.append((r, uu)) tight = min(passing) if passing else (None, None) return {"theta": theta, "f2": f2, "max_ratio": best[0], "max_at_u": best[1], "passing_range": [passing[0][1], passing[-1][1]] if passing else None, "min_ratio_in_passing": tight[0], "min_at_u": tight[1], "n_passing": len(passing)} def reduction_threshold(): """The D_3 value below which the closed-form reduction proves R_1 > 2. f_1(4)^2 (1 + 1/D_3(u)) > 2 <=> D_3(u) < f_1(4)^2 / (2 - f_1(4)^2). """ a = f1_floor() ** 2 return a / (2 - a) def reduction_ub(env, uu, f2="one"): """The closed-form lower bound for R_1(u): f_1(4)^2 (1+1/D_3(u)) [* sigma_2].""" at = env["at"] d3 = at(env["D"][3], uu) if d3 <= 0: raise ValueError("D_3 must be positive") v = f1_floor() ** 2 * (1 + 1 / d3) if f2 == "sigma2": v *= at(env["sigma2"], uu) elif f2 != "one": raise ValueError("f2 must be 'one' or 'sigma2'") return v def reduction_region_end(env, f2="one", u_lo=4.0, u_hi=16.0): """sup{u in [u_lo,u_hi] : reduction_ub(u) > 2} on the grid, or None.""" pu = env["per_unit"] last = None for i in range(int(math.ceil(u_lo * pu)) + 1, int(min(env["u_max"], u_hi) * pu) + 1): uu = env["u"][i] try: if reduction_ub(env, uu, f2) > 2.0: last = uu except ValueError: pass return last def structure_checks(env): """The structural facts the bounds rest on, evaluated on the grid.""" f, F, sig, ro = env["f"], env["F"], env["sigma2"], env["rho_odd"] pu, n = env["per_unit"], env["n"] out = { "f_nondecreasing": all(f[i] >= f[i - 1] - 1e-9 for i in range(2 * pu + 1, n + 1)), "F_nonincreasing": all(F[i] <= F[i - 1] + 1e-9 for i in range(3 * pu + 1, n + 1)), "f_max": max(f), "F_min_after_3": min(F[i] for i in range(3 * pu, n + 1)), "sigma2_nondecreasing": all(sig[i] >= sig[i - 1] - 1e-9 for i in range(2 * pu + 1, n + 1)), "sigma2_max": max(sig), "rho_odd_gt_1": min(ro[i] for i in range(4 * pu, n + 1)) > 1.0, "D3_nondecreasing": all(env["D"][3][i] >= env["D"][3][i - 1] - 1e-12 for i in range(2 * pu + 1, n + 1)), "s_times_F_min_over_eg": min(2 * EG if i <= 3 * pu else env["F"][i] * env["u"][i] for i in range(1, n + 1)) / EG, } out["f_floor_holds"] = out["f_nondecreasing"] and out["f_max"] <= 1.0 + 1e-6 return out def selfcheck(per_unit=4000, u_max=16.0): """Run the pinned self-check; returns (ok, dict).""" env = evaluate(per_unit=per_unit, u_max=u_max) st = structure_checks(env) at = env["at"] res = { "F1_at_3_vs_closed": [at(env["F"], 3.0), F1_closed(3.0)], "f1_at_4_vs_closed": [at(env["f"], 4.0), f1_closed(4.0)], "sigma2_at_2_vs_exact": [at(env["sigma2"], 2.0), INV_E2G / 2], "theta1_F2_1": theta_profile(env, 1.0, "one"), "theta1_F2_sigma2": theta_profile(env, 1.0, "sigma2"), "theta_half_F2_sigma2": theta_profile(env, 0.5, "sigma2"), "reduction_threshold": reduction_threshold(), "reduction_ub_u5_F2_1": reduction_ub(env, 5.0, "one"), "reduction_ub_u5_F2_sigma2": reduction_ub(env, 5.0, "sigma2"), "reduction_region_end_F2_1": reduction_region_end(env, "one", 4.0, 8.0), "reduction_region_end_F2_sigma2": reduction_region_end(env, "sigma2", 4.0, 8.0), "structure": st, } ok = (abs(res["F1_at_3_vs_closed"][0] - res["F1_at_3_vs_closed"][1]) < 1e-9 and abs(res["f1_at_4_vs_closed"][0] - res["f1_at_4_vs_closed"][1]) < 1e-6 and st["f_floor_holds"] and st["sigma2_nondecreasing"] and st["F_nonincreasing"] and st["sigma2_max"] <= 1.0 + 1e-6 and abs(st["s_times_F_min_over_eg"] - PARITY_FLOOR) < 1e-9 and res["reduction_ub_u5_F2_1"] > 2.0 and res["reduction_ub_u5_F2_sigma2"] > 2.0) return ok, res def main(): import json ok, res = selfcheck() print(json.dumps({"ok": ok, "selfcheck": res}, indent=1, sort_keys=True)) return 0 if ok else 1 if __name__ == "__main__": raise SystemExit(main())