#!/usr/bin/env python3 """Experiment 2 - the internal gap words of every T29 component of size >= 3, and the producer's alternation lemma, tested on the measured word. Reads the comp.txt files written by killcensus (.p

.comp.txt: one line per component of size L >= 3, its L-1 internal gap values in order) and checks, per component: A. the non-Z internal gaps strictly alternate between class P (m = +2) and class M (m = -2) [the producer's transfer rule]; B. L >= 3 forces at least one internal gap >= 4p-2 [(a)] more sharply, #(gaps >= 4p-2) >= floor((L-1)/2) [(b)] C. mean internal gap >= 3p - p/(L-1) [(c)] Usage: t29_components.py """ import sys, json from collections import Counter import numpy as np def a_plus(p): a = 2 while a % 6: a += p return a def means_atleast(vals, thr): arr = np.array(vals, dtype=np.int64) return np.cumsum(arr[::-1])[::-1] >= thr def analyse(path, p, out): comps = [] with open(f"{path}.p{p}.comp.txt") as f: for line in f: t = line.split() if t: comps.append([int(x) for x in t[1:]]) # first token is the start slot aP, aM = a_plus(p), 6 * p - a_plus(p) small, large = min(aP, aM), max(aP, aM) thr = 4 * p - 2 words = Counter() Ldist = Counter() viol_alt, viol_a, viol_b, viol_c = [], [], [], [] class_hist = Counter() for w in comps: L = len(w) + 1 Ldist[L] += 1 words[tuple(w)] += 1 cls = [] for v in w: m = v % p class_hist[m] += 1 if m == 0: cls.append("Z") elif m == 2: cls.append("P") elif m == p - 2: cls.append("M") else: raise AssertionError(f"non-kill-class internal gap {v} at p={p}") nz = [c for c in cls if c != "Z"] alt_ok = all(nz[i] != nz[i + 1] for i in range(len(nz) - 1)) if not alt_ok: viol_alt.append(w) nlarge = sum(1 for v in w if v >= thr) if nlarge < 1 and L >= 3: viol_a.append(w) if nlarge < (L - 1) // 2: viol_b.append(w) mu = sum(w) / len(w) if mu < 3 * p - p / len(w) - 1e-12: viol_c.append(w) res = { "p": p, "aP": aP, "aM": aM, "small": small, "large": large, "threshold_4p_2": thr, "ncomp_ge3": len(comps), "Ldist": {int(k): int(v) for k, v in sorted(Ldist.items())}, "class_values_present": {int(k): int(v) for k, v in sorted(class_hist.items())}, "words": {"+".join(map(str, k)): int(v) for k, v in sorted(words.items())}, "violations": {"alternation": viol_alt[:20], "a": viol_a[:20], "b": viol_b[:20], "c": viol_c[:20]}, "n_violations": {"alternation": len(viol_alt), "a": len(viol_a), "b": len(viol_b), "c": len(viol_c)}, "all_pass": not (viol_alt or viol_a or viol_b or viol_c), } print(f"p={p}: ncomp_ge3={len(comps)} Ldist={res['Ldist']} " f"small={small} large={large} 4p-2={thr}") top = Counter(words).most_common(8) print(" words:", top) print(" violations:", res["n_violations"], "ALL PASS" if res["all_pass"] else "FAIL") return res def main(): path, out = sys.argv[1], sys.argv[2] res = {"folds": []} for p in [29, 31, 37]: res["folds"].append(analyse(path, p, out)) with open(out, "w") as f: json.dump(res, f, indent=2) return 0 if all(f["all_pass"] for f in res["folds"]) else 1 if __name__ == "__main__": sys.exit(main())