#!/usr/bin/env python3 """job 2752/2753 -- WHERE does a killer marginal of 10 come from, and what is the window made of? Object (exactly the instrument's, see killer-marginal.py): over the modulus L = P*prod(U) the ALIVE slots are those n with (n,P)=(n+2,P)=1; list them in increasing order as a sequence, and call a run a maximal stretch of CONSECUTIVE alive slots all killed by the killer set. K*(P,S) is the longest run. This locates the maximal U-killed run and decomposes THAT window exactly: |W| = (positions killed by R) + (positions killed only by U\\R) [partition] = sum of the R-fragment lengths + S so that, with A = K*(P,R), the marginal obeys the IDENTITY marginal = sum(l_i) + S - A. DEFECT HISTORY (kept, because it is the reason for the self-check): the first version of this script took the run's start/end RANKS among the killed slots for positions, so it decomposed a window ~1.2M positions away from the real one. The reported length (19) and K*/A were right, the decomposition was not. The partition check below is what catches that class of error, and `witness-diag.py` is the independent recomputation that found it. python marginal-witness.py --base 30 --U 7,11,13,17,19,23 --q 7 Exit code 3 if the partition check fails or the witness disagrees with the instrument. """ import argparse import importlib.util import json import os import sys import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) spec = importlib.util.spec_from_file_location("km", os.path.join(HERE, "killer-marginal.py")) km = importlib.util.module_from_spec(spec) spec.loader.exec_module(km) def kill_sel(slots, v): return ((slots % v) == 0) | (((slots + 2) % v) == 0) def runs_with_positions(flags): """Maximal True stretches -> (start_pos, end_pos, length) in the flag array's own coordinates.""" n = flags.size idx = np.flatnonzero(flags) if idx.size == 0: return [] if int(idx.size) == n: return [(0, n - 1, n)] brk = np.flatnonzero(np.diff(idx) != 1) starts = np.concatenate(([0], brk + 1)) ends = np.concatenate((brk, [idx.size - 1])) return [(int(idx[s]), int(idx[e]), int(idx[e] - idx[s] + 1)) for s, e in zip(starts, ends)] def main(): ap = argparse.ArgumentParser() ap.add_argument("--base", type=int, required=True) ap.add_argument("--U", required=True) ap.add_argument("--q", type=int, required=True) ap.add_argument("--window", type=int, default=1 << 25) ap.add_argument("--out", default="witness-marginal.json") a = ap.parse_args() P = a.base U = tuple(int(v) for v in a.U.split(",")) q = a.q R = tuple(v for v in U if v != q) L = P for v in U: L *= v alive = np.tile(km.slot_pattern(P), L // P) aidx = np.flatnonzero(alive) kU = km.killed_mask(L, U) kR = km.killed_mask(L, R) fU, fR = kU[aidx], kR[aidx] n = int(fU.size) ru = runs_with_positions(fU) rr = runs_with_positions(fR) # merge the head and tail runs when they are adjacent across the wrap (the modulus is a cycle) def cyclic(rs): if len(rs) > 1 and rs[0][0] == 0 and rs[-1][1] == n - 1: return [(rs[-1][0], rs[0][1], rs[0][2] + rs[-1][2])] + rs[1:-1] return list(rs) ru, rr = cyclic(ru), cyclic(rr) K, A = max(v[2] for v in ru), max(v[2] for v in rr) ins_K = km.blocked_kstar(P, U, L, a.window) ins_A = km.blocked_kstar(P, R, L, a.window) # the winning run's window in compressed coordinates, with the cyclic wrap handled explicitly win = max(ru, key=lambda t: t[2]) s, e, ln = win if s <= e: winpos = np.arange(s, e + 1) else: # cyclically merged run: tail then head winpos = np.concatenate((np.arange(s, n), np.arange(0, e + 1))) assert winpos.size == ln, (winpos.size, ln) slots = aidx[winpos] inR = fR[winpos] qk = kill_sel(slots, q) ronly = int((inR & ~qk).sum()) qonly = int((qk & ~inR).sum()) both = int((inR & qk).sum()) union = int((inR | qk).sum()) partition_ok = (ronly + qonly + both == ln) and (union == ln) and int(fU[winpos].sum()) == ln # R-fragment lengths INSIDE the window, and the supplied stretches between them sub = fR[winpos] fragpos = runs_with_positions(sub) frags = sorted((v[2] for v in fragpos), reverse=True) supplied = runs_with_positions(~inR & qk) internal = max(0, len(fragpos) - 1) res = { "P": P, "U": list(U), "R": list(R), "q": q, "modulus": L, "alive_slots": n, "K_star_union": K, "K_star_R": A, "marginal": K - A, "instrument_K_star_union": ins_K, "instrument_K_star_R": ins_A, "window_ranks": {"start": s, "end": e}, "window_length": ln, "window_first_slot": int(slots[0]), "window_last_slot": int(slots[-1]), "killed_by_R_in_window": int(inR.sum()), "killed_only_by_q_in_window": qonly, "killed_by_both": both, "R_only": ronly, "partition_check": bool(partition_ok), "R_fragments_in_window": frags, "fragment_count": len(fragpos), "fragment_sum": sum(frags), "internal_gaps": internal, "supplied_stretches": [v[2] for v in supplied], "supplied_positions": qonly, "identity_marginal_eq_fragsum_plus_S_minus_A": (sum(frags) + qonly - A), "self_check_vs_instrument": bool(K == ins_K and A == ins_A), } print(json.dumps(res, indent=1)) with open(os.path.join(HERE, a.out), "w", encoding="utf-8", newline="\n") as fh: json.dump(res, fh, indent=1, sort_keys=True) fh.write("\n") ok = partition_ok and res["self_check_vs_instrument"] \ and res["identity_marginal_eq_fragsum_plus_S_minus_A"] == res["marginal"] print("SELF-CHECK %s (partition + instrument agreement + identity)" % ("PASS" if ok else "FAIL")) return 0 if ok else 3 if __name__ == "__main__": sys.exit(main())