#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Job #1415: does the corrected cyclic wrap change any entry of #622's 280-entry bank? Question (route 27, from #643's next_step): #622's three methods closed the tile period without carrying M mod p into the wrap. The corrected convention says the slot after res[i] in the next period is res[i] + M, so its residue mod p is (res[i] + M) mod p, not res[i] mod p. Instrument 1 (INSTRUMENT-CORRECTED): L via the cyclic gap sequence of the tile. The gaps sum to M by construction, so the wrap gap is g_{n-1} = (res[0] + M) - res[n-1] exactly; a chain of k gaps whose partial sums mod p lie in {0,2} or all in {0,-2} is a run of k+1 consecutive killed slots, so L = 1 + longest such cyclic chain (#640's reduction, applied to the true cyclic gaps). Instrument 2 (INSTRUMENT-LITERAL): the definitional slot-level scan on the CORRECTED doubled residue array: second copy = (res + M) mod p (this is diagonal-check2.py's L_of(corrected=True)). Used as the gate on a sample, so a reduction error cannot hide in the 280-row diff. Bank: work/L-grid-622.json, fetched from the server in this run (GET /files/ of return #622's L-grid.json). Bank field `L` is the recorded capacity per (level, p). """ import json, sys, time from pathlib import Path import numpy as np OUT = Path(__file__).parent BANK = OUT / "L-grid-622.json" def primes_upto(n): out, c = [], 2 while c <= n: if all(c % q for q in out if q * q <= c): out.append(c) c += 1 return out def tile_crt(level): """Admissible residue set mod M = level#, in order (anchors 1 and 1+2 = twin-admissible).""" res, M = [1], 2 for q in [x for x in primes_upto(level) if x >= 3]: inv = pow(M % q, -1, q) classes = [c for c in range(q) if c % q != 0 and (c + 2) % q != 0] res = [r + M * (((c - r) * inv) % q) for r in res for c in classes] M *= q res.sort() return np.asarray(res, dtype=np.int64), M def L_gap(res, M, p): """L from the true cyclic gap sequence mod p (chain = longest alternating {0,+2}/{0,-2} run).""" n = len(res) g = np.diff(res, append=res[0] + M) # cyclic gaps, sum(g) == M s = g % p ok = (s == 0) | (s == 2) | (s == (p - 2) % p) sign = np.where(s == 0, 0, np.where(s == 2, 1, -1)) sign = np.where(ok, sign, 9) # 9 = barrier (not in {0, +-2} mod p) d = np.concatenate([sign, sign]) # doubled, cyclic cur = np.where(d != 0, np.arange(2 * n), -1) filled = np.maximum.accumulate(cur) # forward fill of last nonzero index prev = np.where(filled >= 1, d[np.maximum(filled - 1, 0)], 9) # last NONZERO STRICTLY BEFORE j br = (d == 9) | ((d != 0) & (prev == d)) # a chain cannot take two equal nonzero steps # NO forced boundary at 0: a legitimate chain may span index 0 (cyclic). A boundary marks the # FIRST position of a block, so blocks are the positions strictly between boundaries. bnd = np.flatnonzero(br) if len(bnd) < 2: chain = n - 1 else: sizes = np.diff(np.concatenate([bnd, [bnd[0] + 2 * n]])) - 1 chain = int(min(int(sizes.max()), n - 1)) return 1 + chain def L_literal(res, M, p): """Definitional scan on the corrected doubled residue array (independent of the gap reduction).""" n = len(res) R = res % p dbl = np.concatenate([R, (R + (M % p)) % p]) best = 0 for a in range(p): killed = (dbl == a) | (dbl == ((a + 2) % p)) if killed.all(): m = n else: miss = np.flatnonzero(~killed) d = np.diff(np.concatenate([miss, [miss[0] + 2 * n]])) - 1 m = int(min(d.max(), n)) if m > best: best = m return best def main(): t0 = time.time() bank = json.loads(BANK.read_text(encoding="utf-8")) rows = bank["rows"] levels = sorted({r["level"] for r in rows}) rec = {"tool": "bank-diff.py", "bank": {"file": BANK.name, "entries": bank["entries"], "levels": bank["domain"]["levels"], "rows": len(rows)}, "instrument": "L_gap on the true cyclic gap sequence (wrap gap (res[0]+M)-res[n-1])", "gates": [], "changes": [], "mismatches": [], "rows_out": []} tiles = {} for lv in levels: res, M = tile_crt(lv) tiles[lv] = (res, M) print(f"tile T_{lv}: {len(res)} slots, M={M}", file=sys.stderr) for r in rows: lv, p, Lb = r["level"], r["p"], r["L"] res, M = tiles[lv] Lc = L_gap(res, M, p) rec["rows_out"].append({"level": lv, "p": p, "bank_L": Lb, "corrected_L": Lc, "agrees": Lc == Lb}) if Lc != Lb: rec["mismatches"].append({"level": lv, "p": p, "bank_L": Lb, "corrected_L": Lc, "M_mod_p": int(M % p), "witness_start": r.get("witness_start")}) # gate: literal definitional instrument on a sample (diagonal folds + every level-23 p > 103) sample = [(r["level"], r["p"]) for r in rows if (r["p"] == min([x for x in primes_upto(r["level"] + 25) if x > r["level"]] or [0]) # diagonal folds or (r["level"] == 23))] seen, gok = set(), 0 for lv, p in sample: if (lv, p) in seen or lv > 19: continue seen.add((lv, p)) res, M = tiles[lv] Ll = L_literal(res, M, p) Lc = L_gap(res, M, p) ok = Ll == Lc gok += ok rec["gates"].append({"level": lv, "p": p, "literal_L": Ll, "gap_L": Lc, "agree": ok, "bank_L": next(x["L"] for x in rows if x["level"] == lv and x["p"] == p)}) print(f"gate T_{lv} p={p}: literal={Ll} gap={Lc} agree={ok}", file=sys.stderr) rec["totals"] = {"cells": len(rows), "corrected_matches_bank": sum(1 for x in rec["rows_out"] if x["agrees"]), "cells_changed": len(rec["mismatches"]), "literal_gates": len(rec["gates"]), "literal_gates_agree": gok} rec["observed_utc"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) rec["seconds"] = round(time.time() - t0, 2) (OUT / "bank-diff.json").write_text(json.dumps(rec, indent=1), encoding="utf-8") print(json.dumps(rec["totals"])) print(json.dumps([g for g in rec["gates"] if not g["agree"]])) print(json.dumps(rec["mismatches"][:20])) if __name__ == "__main__": main()