#!/usr/bin/env python3 """The ceiling-direction support lemma (job #2040), and the measurement that decides its reading. The U-identity of #1097 localises a change of the chain endpoint to the divisor window: tI(Ub) - tI(Ua) = sum_{Ua < d <= Ub, d|m} mu(d) C_d. The same one-line expansion localises a change of the SPLIT'S CEILING to a squarefree window of the cofactor. With g_le_V(b) = sum_{k|b, k<=V} mu(k) and f_V(m) = w(e,m) * TI_V(m), tI_{V'}(U) - tI_V(U) = sum_{m>V} w * sum_{d|m, d<=U} mu(d) * sum_{V V, same U) i.e. **the ceiling increment is supported exactly on the cofactors carrying a squarefree factor k in (V, V']** -- a rough-cofactor statement, and the classical Buchstab/Dickman regime rather than a level-of-distribution one -- **minus the support-drift slice**, because family() takes the m-threshold at the row's own ceiling (`m > max(U, V)`), so a change of V can also move the support by (V, V']. **That slice is empty at every scale used here** (the surviving m all satisfy `m > x/(2e) >= V'` because `e <= sqrt(x)`), so in this regime the ceiling increment is the pure rough-window sum; this file measures that margin (`min_surviving_m_over_Vp`) rather than assuming it. Both terms are computed here from scratch and checked against the members measured by v_matched.py / the ladder, bit-for-bit, so the two directions of the family (leg window in U, rough window in V) are exact rather than asserted. Repairs over earlier versions of this file, all caught by controls: * version 1 divided the member difference by x a second time (members_over_x is already normalised), leaving a residual of exactly x * |increment| at every row -- i.e. the identity was "confirmed" only up to a systematic factor of x. That residual signature (resid_rel = x - 1 everywhere) is what identified the bug. * version 2 wrote the cofactor divisors of b = m/d as the squarefree divisors of m coprime to d. That is wrong whenever d does not capture the full p-power of m: e.g. m = 2^5 * 3, d = 4 leaves b even, so k = 2 | b although gcd(2, 4) != 1. The test is `s | (m/d)`, and the smoke run at x = 2^12..2^16 (smoke_ceiling.py) refused the identity on 16/16 rows until it was fixed. * version 2 also took the drift slice at the upper ceiling; the correct slice is f_V, the lower one. Cost note: cheaper than one family() pass per (x, V) -- no per-U accumulation. """ import json import math import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) RUN = os.path.join(HERE, "..") sys.path.insert(0, os.path.join(RUN, "job2050")) from window_increments import Ctx # noqa: E402 from split_census import EPS # noqa: E402 CHAIN = [1, 2, 4, 8, 16] U = 16 PAIRS = [(16, 32), (32, 64), (64, 128), (128, 256)] MEMBERS = os.environ.get("CI_MEMBERS", "v-matched.json") # members source (over x) OUT = os.environ.get("CI_OUT", "v-ceiling-identity.json") JONLY = ([int(v) for v in os.environ["CI_JLIST"].split(",")] if os.environ.get("CI_JLIST") else None) def ceiling_pass(x, V, Vp): """One pass at support m > V: the k-window sum K, the slice K_(V,V'], and the drift S.""" ctx = Ctx(x, Vp) mu, lam, spf = ctx.mu, ctx.lam, ctx.spf e0 = int(math.floor(x ** (0.5 - EPS))) k_tot = 0.0 k_slice = 0.0 s_slice = 0.0 pairs = 0 min_m = None for e in range(1, e0): if e % 2 == 0 or mu[e] == 0: continue m_lo = x // (2 * e) m_hi = x // e for m in range(max(m_lo + 1, V + 1), m_hi + 1): L = lam[e * m - 2] if L == 0.0 or math.gcd(m, e) != 1: continue pairs += 1 min_m = m if min_m is None else min(min_m, m) w = math.log(m) * L dl = [d for d in ctx.divisors_all(m) if d[0] <= U] # (d, mu(d)), d<=U, squarefree inner_k = 0.0 inner_s = 0.0 for d, md in dl: # squarefree divisors of b = m/d: s | m with s | (m/d) (NOT "s coprime to d") b = m // d kw = 0.0 gle = 0.0 for s, ms in ctx.divisors_all(m): if s > Vp or b % s: continue gle += ms if s > V: kw += ms inner_k += md * kw inner_s += md * gle k_tot += w * inner_k if m <= Vp: k_slice += w * inner_k s_slice += w * inner_s return {"k_window_over_x": k_tot / x, "k_slice_over_x": k_slice / x, "s_drift_over_x": s_slice / x, "surviving_pairs": pairs, "min_surviving_m": min_m, "min_surviving_m_over_Vp": (min_m / Vp if min_m else None)} def main(): src = json.load(open(os.path.join(HERE, MEMBERS))) by_j = {} for r in src["rows"]: by_j.setdefault(r["j"], {})[r["V"]] = r["members_over_x"] out = {"chain": CHAIN, "U": U, "pairs": [list(p) for p in PAIRS], "members": MEMBERS, "rows": []} for j in sorted(by_j): if JONLY is not None and j not in JONLY: continue x = 2 ** j mem = by_j[j] for V, Vp in PAIRS: if V not in mem or Vp not in mem: continue d_member = mem[Vp][-1] - mem[V][-1] # already over x p = ceiling_pass(x, V, Vp) predicted = p["k_window_over_x"] - p["s_drift_over_x"] row = {"j": j, "V": V, "Vp": Vp, "member_diff_over_x": d_member, "k_window_over_x": p["k_window_over_x"], "k_slice_over_x": p["k_slice_over_x"], "s_drift_over_x": p["s_drift_over_x"], "predicted_over_x": predicted, "resid_over_x": predicted - d_member, "resid_rel": (abs(predicted - d_member) / abs(d_member) if d_member else None), "fixed_support_gap_over_x": p["k_window_over_x"] - d_member, "drift_share_of_increment": (p["s_drift_over_x"] / d_member if d_member else None), "surviving_pairs": p["surviving_pairs"], "min_surviving_m_over_Vp": p["min_surviving_m_over_Vp"]} out["rows"].append(row) print(json.dumps(row), flush=True) worst = max((abs(r["resid_over_x"]) for r in out["rows"]), default=None) rel = max((r["resid_rel"] for r in out["rows"] if r["resid_rel"] is not None), default=None) out["worst_abs_resid_over_x"] = worst out["worst_rel_resid"] = rel with open(os.path.join(HERE, OUT), "w") as fh: json.dump(out, fh, indent=1) print(json.dumps({"worst_abs_resid_over_x": worst, "worst_rel_resid": rel}, indent=1)) if __name__ == "__main__": main()