--- a/job1415-bank-diff.py +++ b/job1415-bank-diff.py @@ -48,47 +48,29 @@ 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_gap(R,M,p): + assert 0 < len(R) < 2**30, "index range exceeded" + n=len(R);g=np.diff(R,append=R[0]+M)%p + one=np.where(g==0,0,np.where(g==2,1,np.where(g==p-2,-1,9))).astype(np.int8) + d=np.tile(one,2);idx=np.arange(len(d),dtype=np.int32) + # previous is the INDEX of the preceding nonzero gap, not its adjacent position. + filled=np.maximum.accumulate(np.where(d!=0,idx+1,0)) + previous=np.empty_like(filled);previous[0]=-1;previous[1:]=filled[:-1]-1 + same=(d!=0)&(d!=9)&(previous>=0)&(d==d[np.maximum(previous,0)]) + forbidden=np.where(d==9,idx,np.where(same,previous,-1)) + # Repeated signs exclude the earlier nonzero gap, retaining intervening zeros. + frontier=np.maximum.accumulate(forbidden) + assert np.any(one==9),'This bounded cyclic check requires a barrier' + return 1+int(np.max(idx-frontier)) -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 +def L_literal(R,M,p): + n=len(R);dbl=np.concatenate((R%p,(R+M)%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 + killed=(dbl==a)|(dbl==(a+2)%p) + miss=np.concatenate(([-1],np.flatnonzero(~killed),[2*n])) + best=max(best,int(np.diff(miss).max())-1) + return min(best,n) def main(): @@ -131,6 +113,7 @@ 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) + assert all(g["agree"] for g in rec["gates"]), "literal gate failed" 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}