#!/usr/bin/env python3 """Job #1986 -- cross-lane synthesis: the transport tail's support index. THE CONNECTION BEING TESTED --------------------------- Return #159 (lane 1, break) measures the PROVEN Tail-Count Transport inequality N_new(theta) <= (q-2)*N(theta) + 2*SUM_{L>=1} Q_L(theta), Q_L(theta) = #{i : G_{L+1}(i) >= theta, g_{i+1}..g_{i+L-1} all qualify}, qualifies(g,q) := g mod q in {0, 2, q-2}, with the tail evaluated up to a hard cap LMAX = 8, at folds 11..41. Return #161 (lane 4, measure) measures L(T_x, p), "the longest adjacent-kill run": the longest run of consecutive slots whose residues mod p occupy at most two values differing by 2 (free translate; served script research/a3-08-adjacent-pairs.js, whose table gives L = 1 for every column from p = 127 on, and L(T_29, 31) = 4). SHARED OBJECT. Both are statistics of the same cyclic gap word of the same tiles, and both use the SAME qualifying set: a run of consecutive slots in a two-set {a, a+2} has consecutive gaps g = 0 or +-2 (mod q), which is exactly `qualifies`. Therefore, with M(x,q) := the longest cyclic run of consecutive QUALIFYING GAPS of T_x mod q, 1. Q_L needs L-1 consecutive qualifying gaps (indices i+1..i+L-1), so Q_L == 0 for every L >= M(x,q) + 2 [exact, definitional] i.e. the infinite Q_L tail is FINITELY SUPPORTED with support index M+1. 2. #161's L is a LOWER bound for that index in the same word: M >= L_true - 1 [exact, because two-set runs require legality but qualifying runs do not, so the free-translate run is a subset] 3. The direction of the cap matters: dropping terms by capping L below the support index makes the RHS SMALLER, hence the reported N_new/RHS LARGER, hence a spurious violation. So the reported ratios are safe only where LMAX = 8 >= M+1. WHAT THIS PROGRAM DOES (independent implementation; nothing is imported from the served runs) * builds the ladder tiles by the served fold rule (T_3 = {5} mod 6, then kill slots = a, a-2 mod q per copy k with a_k = -kW mod q); * VALIDATES the port against two published tables it did not produce: #161's T17/T19/T23 columns (where each column reads 1) and #159's five published max ratios; * measures M(x,q), the support index M+1, and delta = M - (L_true - 1); * re-evaluates the transport inequality with the tail capped at the SERVED cap (8) and at the measured support index, and asserts the two RHS profiles are identical -- that is the sharpened form of #159's statement, tested on real words; * extends the ladder beyond #159's folds for the tile T_23, where #161's table covers q to 1009. Run: python transport_support.py (writes transport_support.json, prints a summary) """ import io import json import sys import time import numpy as np NB = 4200 # theta bins in units of 6, exactly the served grid LMAX = 8 # exactly the served cap PRIMES = [3, 5, 7, 11, 13, 17, 19, 23] LOG = [] def say(s): print(s, flush=True) LOG.append(s) # ------------------------------------------------------------------ tile machinery def fold(slots, W, q): """The served foldTile: copy k deletes residues a_k = -kW mod q and a_k - 2.""" rs = slots % q wq = W % q out = [] for k in range(q): kw = (k * wq) % q d0 = (q - kw) % q d2 = (2 * q - 2 - kw) % q m = (rs != d0) & (rs != d2) out.append(slots[m] + k * W) return np.concatenate(out), W * q def build_ladder(upto): tiles = {} slots, W = np.array([5], dtype=np.int64), 6 tiles[3] = (slots, W) for q in PRIMES[1:]: if q > upto: break slots, W = fold(slots, W, q) tiles[q] = (slots, W) return tiles def gap_word(slots, W): g = np.diff(slots) return np.append(g, slots[0] + W - slots[-1]).astype(np.int64) def qualify_mask(g, q): r = g % q return (r == 0) | (r == 2) | (r == q - 2) def longest_cyclic_run(mask): n = mask.size if n == 0: return 0 if mask.all(): return int(n) if not mask.any(): return 0 idx = np.flatnonzero(~mask) ext = np.append(idx, idx[0] + n) return int(np.max(np.diff(ext)) - 1) def M_of(g, q): return longest_cyclic_run(qualify_mask(g, q)) def _chains(sorted_idx, D): """Longest / leading / trailing chain of consecutive integers in a sorted index array.""" if sorted_idx.size == 0: return 0, 0, 0 d = np.diff(sorted_idx) brk = np.flatnonzero(d != 1) starts = np.concatenate(([0], brk + 1)) ends = np.concatenate((brk, [sorted_idx.size - 1])) lens = sorted_idx[ends] - sorted_idx[starts] + 1 internal = int(lens.max()) prefix = int(lens[0]) if sorted_idx[0] == 0 else 0 suffix = int(lens[-1]) if sorted_idx[-1] == D - 1 else 0 return internal, prefix, suffix def trueL(slots, W, q): """Faithful port of the served a3-08/transport `trueL`: ONE linearised pass over the q copies, copy k killing residues a_k = -kW mod q and a_k - 2, longest run of surviving SLOTS (indices). Same semantics as the served loop, computed in O(D log D) instead of O(qD): the surviving positions of copy k are the sorted union of the index groups of its two killed residues, so a run is a chain of consecutive INDICES inside that union, and the served loop's carry across copies is the suffix/prefix join. Validated below against the published #161 columns.""" rs = slots % q wq = W % q D = int(slots.size) order = np.argsort(rs, kind="stable") counts = np.bincount(rs, minlength=q) starts = np.concatenate(([0], np.cumsum(counts)[:-1])) groups = [order[starts[r]:starts[r] + counts[r]] for r in range(q)] best = 0 cur = 0 for k in range(q): kw = (k * wq) % q d0 = (q - kw) % q d2 = (2 * q - 2 - kw) % q if d0 == d2 or counts[d0] + counts[d2] == 0: internal = pre = suf = 0 else: pos = np.sort(np.concatenate((groups[d0], groups[d2]))) internal, pre, suf = _chains(pos, D) best = max(best, cur + pre, internal) cur = suf return max(best, cur) def new_tail(slots, W, q, nb=NB): """N_new(theta) for theta = 6*b, streamed over the fold (no folded word is stored).""" rs = slots % q wq = W % q hist = np.zeros(nb, dtype=np.int64) prev_last = None first = None for k in range(q): kw = (k * wq) % q d0 = (q - kw) % q d2 = (2 * q - 2 - kw) % q sv = slots[(rs != d0) & (rs != d2)] + k * W if sv.size == 0: continue if prev_last is not None: b = int((sv[0] - prev_last) // 6) if b < nb: hist[b] += 1 dd = np.diff(sv) if dd.size: bb = (dd // 6).astype(np.int64) good = bb < nb hist += np.bincount(bb[good], minlength=nb) prev_last = sv[-1] if first is None: first = sv[0] b = int((first + q * W - prev_last) // 6) if b < nb: hist[b] += 1 return np.cumsum(hist[::-1])[::-1] def tct_rhs(g, q, lmax, nb=NB): """The served tctRHS, term by term, keeping the by-L decomposition.""" D = g.size rhs = np.zeros(nb, dtype=np.int64) byL = [np.zeros(nb, dtype=np.int64) for _ in range(lmax + 1)] b0 = g // 6 sel = b0 < nb rhs += np.bincount(b0[sel], minlength=nb) * (q - 2) byL[0] += np.bincount(b0[sel], minlength=nb) * (q - 2) ok = np.ones(D, dtype=bool) s = g.copy() for L in range(1, lmax + 1): if L >= 2: ok = ok & qualify_mask(np.roll(g, -(L - 1)), q) s = s + np.roll(g, -L) bb = s // 6 good = ok & (bb < nb) add = np.bincount(bb[good], minlength=nb) * 2 rhs += add byL[L] += add for arr in [rhs] + byL: arr[:] = np.cumsum(arr[::-1])[::-1] return rhs, byL def max_ratio(nn, rhs, nb=NB): worst = 0.0 tested = 0 viol = 0 for b in range(1, nb): if nn[b] == 0 and rhs[b] == 0: continue tested += 1 if rhs[b] == 0: r = float("inf") else: r = float(nn[b]) / float(rhs[b]) if r > worst: worst = r if nn[b] > rhs[b]: viol += 1 return worst, tested, viol def main(): t0 = time.time() tiles = build_ladder(23) for q in sorted(tiles): slots, W = tiles[q] say("tile T_%-3d D=%-10d W=%d" % (q, slots.size, W)) gaps = {q: gap_word(*tiles[q]) for q in tiles} out = {"generated_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "nb_bins": NB, "served_lmax": LMAX, "tiles": {}, "validation": {}, "support": [], "extension": [], "truncation_test": []} for q in sorted(tiles): slots, W = tiles[q] g = gaps[q] gm = int(g.max()) out["tiles"][str(q)] = {"D": int(slots.size), "W": int(W), "G2": gm} say(" G2(T_%d) = %d" % (q, gm)) # ---------------- validation 1: #161's columns (where each column reads 1) say("\nVALIDATION 1 -- reproducing return #161's published L columns (a3-08 trueL)") col_claim = {17: (59, 1), 19: (71, 1), 23: (107, 1)} val = {} for x, (thresh, expect) in col_claim.items(): slots, W = tiles[x] bad = [] seen = [] for q in [p for p in [29, 31, 37, 41, 47, 53, 59, 67, 71, 79, 89, 97, 103, 107, 113, 127] if p >= max(x, 29)]: L = trueL(slots, W, q) seen.append([q, L]) if q >= thresh and L != expect: bad.append([q, L]) val["T_%d" % x] = {"claim": "L = 1 for p >= %d" % thresh, "minors": seen, "violations": bad} say(" T_%-3d claim L=1 from p>=%-4d : %s (sample %s)" % (x, thresh, "HOLDS" if not bad else "FAILS %s" % bad, seen[:6])) # the dip #161 reports for T23 (1 at 71,73; back to 2 at 79..103) slots23, W23 = tiles[23] dip = {q: trueL(slots23, W23, q) for q in (71, 73, 79, 83, 89, 97, 101, 103, 107)} val["T_23_dip"] = dip say(" T_23 dip check (published: 1 at 71,73 and 2 at 79..103): %s" % dip) out["validation"]["column_L"] = val # ---------------- validation 2: #159's published max ratios say("\nVALIDATION 2 -- reproducing return #159's five published max N_new/RHS") published = {(13, 17): 0.8881, (17, 19): 0.8975, (19, 23): 0.9180, (23, 29): 0.9324, (23, 31): 0.9361, (23, 37): 0.9499} for (x, q), want in sorted(published.items()): slots, W = tiles[x] g = gaps[x] rhs8, byL8 = tct_rhs(g, q, LMAX) nn = new_tail(slots, W, q) worst, tested, viol = max_ratio(nn, rhs8) got = round(worst, 4) ok = abs(got - want) <= 0.0001 say(" (x=%d,q=%-3d) published %.4f independent %.4f %s (thetas %d, violations %d)" % (x, q, want, worst, "MATCH" if ok else "DIFFERS", tested, viol)) out["validation"].setdefault("max_ratio", {})["%d>%d" % (x, q)] = { "published": want, "independent": got, "match": bool(ok), "thetas": tested, "violations": viol} # ---------------- the support index and the truncation test say("\nSUPPORT -- M(x,q), the measured transport tail index, against #161's L") say(" x q G2(old) L_true(#161 word) M(qual gaps) support M+1 delta=M-(L-1) rhs(LMAX=8)==rhs(M+1)") for (x, q) in [(13, 17), (17, 19), (19, 23), (23, 29), (23, 31), (23, 37), (19, 29), (19, 31), (17, 23), (13, 23), (17, 29), (17, 31), (19, 37), (13, 19), (11, 13), (7, 11)]: if x not in tiles: continue slots, W = tiles[x] g = gaps[x] L = trueL(slots, W, q) M = M_of(g, q) rhs8, byL8 = tct_rhs(g, q, LMAX) rhsM, byLM = tct_rhs(g, q, min(max(M + 1, 1), 40)) same = bool(np.array_equal(rhs8, rhsM)) tailab = [L2 for L2 in range(M + 2, LMAX + 1) if byL8[L2].any()] row = {"x": x, "q": q, "L_true": L, "M": M, "support": M + 1, "delta": M - (L - 1), "rhs_equal": same, "nonzero_L_above_support": tailab, "bound_below_cap": bool(M + 1 <= LMAX), "byL_total": [int(a.sum()) for a in byL8]} out["support"].append(row) say(" %3d %4d %7d %17d %14d %12d %14d %s%s" % (x, q, int(g.max()), L, M, M + 1, M - (L - 1), same, "" if not tailab else " NONZERO ABOVE SUPPORT: %s" % tailab)) # ---------------- extension beyond #159's ladder, on T_23 (where #161's table reaches) say("\nEXTENSION -- T_23 folded by primes to 131 (beyond #159's folds 11..41)") say(" q L_true M support max N_new/RHS violations rhs(8)==rhs(M+1) #161 says L=1?") for q in [29, 31, 37, 41, 47, 53, 61, 71, 83, 97, 107, 113, 127]: slots, W = tiles[23] g = gaps[23] L = trueL(slots, W, q) M = M_of(g, q) rhs8, byL8 = tct_rhs(g, q, LMAX) nn = new_tail(slots, W, q) worst, tested, viol = max_ratio(nn, rhs8) cap = min(max(M + 1, 1), 40) rhsM, _ = tct_rhs(g, q, cap) outer = [L2 for L2 in range(M + 2, LMAX + 1) if byL8[L2].any()] row = {"x": 23, "q": q, "L_true": L, "M": M, "support": M + 1, "max_ratio": round(worst, 4), "violations": viol, "thetas": tested, "rhs_equal": bool(np.array_equal(rhs8, rhsM)), "nonzero_L_above_support": outer} out["extension"].append(row) say(" %4d %7d %3d %8d %15.4f %11d %16s %s" % (q, L, M, M + 1, worst, viol, np.array_equal(rhs8, rhsM), "yes" if L == 1 else "no")) out["log"] = LOG out["runtime_s"] = round(time.time() - t0, 1) with io.open("transport_support.json", "w", encoding="utf-8", newline="\n") as fh: json.dump(out, fh, indent=1, sort_keys=True) say("\nwrote transport_support.json (%.1f s)" % out["runtime_s"]) return 0 if __name__ == "__main__": sys.exit(main())