#!/usr/bin/env python3 # job1635-checks.py -- prior-art reading checks for return #159's Tail-Count Transport inequality. # # A. Holt-Rudd arXiv:1408.6002v1 Corollary 6.3 ("if q does not divide g, then # sum_j n_{g,j}(qN) = (q-2) sum_j n_{g,j}(N)", p. 25-26) verified on the # one-class generator cycles G(p#) with an independent implementation: # n_{g,j}(N) is read as the number of contiguous windows of length j of the # cyclic gap word G(N) whose sum is exactly g. # B. The per-length histogram does NOT scale by q-2 (the loss HR's Figure 4 # states: "q-2 remain as driving terms of various lengths"). # C. The two-class (twin) tile: the (q-2) coefficient of the Tail-Count # Transport is an upper bound, measured against the exact alignment count # nu_q(i,0) = q - |{0,2,g_i,g_i+2}| mod q. # D. The plain (q-2) transport of the tail count N(theta) fails on the two-class # tile: witness thetas where N_new(theta) > (q-2) N(theta). import json, sys, time PASS = [] FAIL = [] def check(label, ok, detail=""): (PASS if ok else FAIL).append(label) print(("PASS " if ok else "FAIL ") + label + (" " + detail if detail else "")) def sieve_marks(W, primes): """bytearray of length W: 1 = shares a factor with W (i.e. not coprime).""" comp = bytearray(W) for p in primes: if p >= W: continue comp[0::p] = b"\x01" * len(range(0, W, p)) return comp def coprime_word(W, primes): comp = sieve_marks(W, primes) slots = [s for s in range(1, W) if not comp[s]] return slots def twin_slots(W, primes): """slots s with s and s+2 both coprime to W (cyclically).""" comp = sieve_marks(W, primes) out = [s for s in range(1, W) if not comp[s] and not comp[(s + 2) % W]] return out def gaps(slots, W): n = len(slots) return [slots[(i + 1) % n] - slots[i] if i + 1 < n else slots[0] + W - slots[-1] for i in range(n)] def window_sum_counts(word, g): """counts[j] = number of length-j contiguous cyclic windows with sum exactly g.""" n = len(word) w2 = word + word counts = {} for i in range(n): s = 0 L = 0 while L < n: s += w2[i + L] L += 1 if s > g: break if s == g: counts[L] = counts.get(L, 0) + 1 return counts def hist_of(word): h = {} for g in word: h[g] = h.get(g, 0) + 1 return h def tail_from_hist(h, thetas): vals = sorted(h) out = [] for t in thetas: out.append(sum(h[v] for v in vals if v >= t)) return out def main(): out = {} # ---------------- A/B: one-class generator cycles G(p#) ---------------- prime_sets = {11: [2, 3, 5, 7, 11], 13: [2, 3, 5, 7, 11, 13], 17: [2, 3, 5, 7, 11, 13, 17]} Ws = {11: 2310, 13: 30030, 17: 510510} cyc = {p: gaps(coprime_word(Ws[p], prime_sets[p]), Ws[p]) for p in prime_sets} out["cycle_gap_counts"] = {p: len(cyc[p]) for p in cyc} check("G(11#) has phi(11#)=480 gaps", len(cyc[11]) == 480) check("G(13#) has phi(13#)=5760 gaps", len(cyc[13]) == 5760) # HR Cor 6.3: sum_j n_{g,j}(qN) = (q-2) sum_j n_{g,j}(N) for q not | g hr_rows = [] for (xold, xnew, q) in ((11, 13, 13), (13, 17, 17)): for g in (6, 12, 18, 24): if g % q == 0: continue c_old = window_sum_counts(cyc[xold], g) c_new = window_sum_counts(cyc[xnew], g) s_old, s_new = sum(c_old.values()), sum(c_new.values()) ok = (s_new == (q - 2) * s_old) hr_rows.append({"old": xold, "new": xnew, "q": q, "g": g, "sum_old": s_old, "sum_new": s_new, "factor": q - 2, "ok": ok, "len_hist_old": c_old, "len_hist_new": c_new}) check("HR Cor 6.3: sum_j n_{%d,j}(%d#) = %d * sum_j n_{%d,j}(%d#)" % (g, xnew, q - 2, g, xold), ok, "old=%d new=%d" % (s_old, s_new)) out["hr_cor63_rows"] = hr_rows # B. the per-length histograms lose the scale factor loss_rows = [] for r in hr_rows: per_len_ok = all(r["len_hist_new"].get(L, 0) == (r["factor"]) * r["len_hist_old"].get(L, 0) for L in set(r["len_hist_old"]) | set(r["len_hist_new"])) loss_rows.append({"g": r["g"], "q": r["q"], "old": r["old"], "hist_old": r["len_hist_old"], "hist_new": r["len_hist_new"], "per_length_scales": per_len_ok}) out["per_length_loss"] = loss_rows check("at least one gap has a per-length histogram that does NOT scale by q-2", any(not r["per_length_scales"] for r in loss_rows)) # ---------------- C/D: the two-class (twin) tile ---------------- # T_x = twin slots mod x#; fold by q = next prime. tile = {} for x, W, ps in ((13, 30030, [2, 3, 5, 7, 11, 13]), (17, 510510, [2, 3, 5, 7, 11, 13, 17]), (19, 9699690, [2, 3, 5, 7, 11, 13, 17, 19])): t0 = time.time() slots = twin_slots(W, ps) gs = gaps(slots, W) tile[x] = gs print(" [tile T_%d build %.2fs: D=%d gaps]" % (x, time.time() - t0, len(gs)), file=sys.stderr) out["tile_sizes"] = {x: len(tile[x]) for x in tile} out["tile_maxgap"] = {x: max(tile[x]) for x in tile} check("D(T_17) = 22275 twin slots mod 17#", len(tile[17]) == 22275) check("D(T_19) = 378675 twin slots mod 19#", len(tile[19]) == 378675) # C. exact alignment counts nu_q(i,0) = q - |{0,2,g_i,g_i+2}| on the old word nu_rows = [] for (xold, q) in ((17, 19), (13, 17)): hist = {} for g in tile[xold]: k = len({0, 2, g % q, (g + 2) % q}) v = q - k hist[v] = hist.get(v, 0) + 1 nu_rows.append({"old": xold, "q": q, "qminus2": q - 2, "hist": hist, "all_at_most_qminus2": all(v <= q - 2 for v in hist)}) check("nu_q(i,0) <= q-2 for every old gap (T_%d folded by %d)" % (xold, q), nu_rows[-1]["all_at_most_qminus2"], "hist=%s" % hist) out["nu_q_rows"] = nu_rows # D. the plain (q-2) transport of the tail: N_new(theta) <= (q-2) N(theta)? tail_rows = [] for (xold, xnew, q) in ((17, 19, 19), (13, 17, 17)): hold, hnew = hist_of(tile[xold]), hist_of(tile[xnew]) thetas = sorted(set(hold) | set(hnew)) Nold = tail_from_hist(hold, thetas) Nnew = tail_from_hist(hnew, thetas) worst = None violations = [] for t, a, b in zip(thetas, Nold, Nnew): if a == 0: continue ratio = b / ((q - 2) * a) if worst is None or ratio > worst[1]: worst = (t, ratio, a, b) if b > (q - 2) * a: violations.append((t, a, b, (q - 2) * a)) tail_rows.append({"old": xold, "new": xnew, "q": q, "worst": worst, "violations": violations[:8], "n_violations": len(violations)}) print(" [T_%d -> T_%d q=%d: worst N_new/((q-2)N) = %.4f at theta=%d; violations=%d]" % (xold, xnew, q, worst[1], worst[0], len(violations))) out["tail_rows"] = tail_rows check("the plain (q-2) transport of the tail is NOT valid on the two-class tile", any(r["n_violations"] > 0 for r in tail_rows), "; ".join("T_%d->T_%d: %d thetas" % (r["old"], r["new"], r["n_violations"]) for r in tail_rows)) # E. does the correction term repair exactly the violation? (#159's inequality, # loose form, independent implementation at the fold 17 -> 19 T_17 used above.) def transport_rhs(word, q, thetas, Lcap=40): n = len(word) w2 = word + word qual = [1 if (g % q == 0 or g % q == 2 or g % q == q - 2) else 0 for g in w2] Nt = {t: 0 for t in thetas} Q = {t: 0 for t in thetas} for i in range(n): g0 = w2[i] for t in thetas: if g0 >= t: Nt[t] += 1 s = g0 for L in range(1, Lcap + 1): s += w2[i + L] if L >= 2 and not qual[i + L - 1]: break for t in thetas: if s >= t: Q[t] += 1 return ({t: (q - 2) * Nt[t] + 2 * Q[t] for t in thetas}, Nt, Q) xold, xnew, q = 17, 19, 19 vr = next(r for r in tail_rows if r["old"] == xold) hold, hnew = hist_of(tile[xold]), hist_of(tile[xnew]) probe = [v[0] for v in vr["violations"]] + [vr["worst"][0], 66, 102, 132] probe = sorted(set(probe)) rhs, Nt, Q = transport_rhs(tile[xold], q, probe) repair = [] for t in probe: repair.append({"theta": t, "N_old": Nt[t], "plain_q2": (q - 2) * Nt[t], "sumQ": Q[t], "rhs": rhs[t], "N_new": tail_from_hist(hnew, [t])[0]}) out["transport_repair_T17_q19"] = repair for r in repair: print(" [theta=%4d N_old=%6d (q-2)N=%7d sumQ=%5d RHS=%7d N_new=%7d %s]" % (r["theta"], r["N_old"], r["plain_q2"], r["sumQ"], r["rhs"], r["N_new"], "OK" if r["N_new"] <= r["rhs"] else "VIOLATED")) check("#159 loose transport holds at every probed theta *after* the correction (T_17 by 19)", all(r["N_new"] <= r["rhs"] for r in repair)) check("the correction term strictly repairs the plain-(q-2) violation", any(r["N_new"] > r["plain_q2"] for r in repair)) print("\n%d checks, %d PASS, %d FAIL" % (len(PASS) + len(FAIL), len(PASS), len(FAIL))) print("all_pass=%s" % (len(FAIL) == 0)) json.dump(out, open("job1635-checks.json", "w"), indent=1, default=str) return 0 if not FAIL else 1 if __name__ == "__main__": sys.exit(main())