#!/usr/bin/env python3 """Run the Section 1 Python box verbatim and replay all finite checks for strong-dirac draft5.""" # Standard Library import argparse from collections import Counter from dataclasses import replace import json from pathlib import Path import re import sys DIR = Path(__file__).resolve().parent DRAFT3 = DIR.parent / 'draft3' TEX = DIR / 'hughes-strong-dirac-draft5.tex' sys.path.insert(0, str(DRAFT3)) # First Party (exact integer and quadratic arithmetic modules in draft3) import attainment import balance import boundary import geometry as g from hull import Target, active_residues, certify, count_hull, progressions, step_bound def check_python_box(): tex = TEX.read_text(encoding='utf-8') matches = re.findall( r'\\begin\{Verbatim\}(?:\[[^\]]*\])?\n(.*?)\\end\{Verbatim\}', tex, flags=re.DOTALL, ) assert len(matches) == 1, f'Expected exactly 1 Verbatim Python box, got {len(matches)}' body = matches[0].strip() assert 5 <= len(body.splitlines()) <= 10, f'Expected 5..10 lines, got {len(body.splitlines())}' exec(compile(body, str(TEX), 'exec'), {}) print('Section 1 Python box executed verbatim: r(x=7) = 24 on A(0) (n=53).') def check_half_constructions(): vals = {} for k in (1, 3): lines = g.triangular(k, medians=False) vals[len(lines)] = max(g.counts(lines)) drop = {(1, 1, -3), (1, 0, -3), (0, 1, -1), (2, 1, 0)} full = [p for p in g.triangular(4) if p not in drop] for lines in (full, [p for p in full if p != g.INFINITY]): vals[len(lines)] = max(g.counts(lines)) for u in range(1, 5): sq = ( [g.INFINITY] + g.pencils(g.AXES, range(-u, u + 1)) + g.pencils(g.DIAGONALS, [0]) ) for lines in (sq, sq[1:]): assert max(g.counts(lines)) == 2 * u + 2 for k in range(1, 6): full = g.triangular(2 * k) assert max(g.counts(full)) == 6 * k + 2 if k >= 3: cut = {(1, 0, -3), (0, 1, -3), (1, 1, -3), (0, 1, 3)} assert max(g.counts([p for p in full if p not in cut])) == 6 * k + 1 inc = g.vertices(full) partners = [ ((1, -1, 0), (1, 1, -3)), ((1, 2, 0), (1, 0, -3)), ((2, 1, 0), (0, 1, -3)), ] for d in (2 * k - 1, 2 * k): partners.extend([ ((1, 0, -d), (0, 1, -3)), ((1, 0, d), (0, 1, 3)), ((0, 1, -d), (1, 0, -3)), ((0, 1, d), (1, 1, -3)), ((1, 1, -d), (0, 1, 3)), ((1, 1, d), (1, 0, -3)), ]) assert all(len(inc[g.primitive(g.cross(p, q))]) == 2 for p, q in partners) for k in range(6, 19, 2): lines = g.triangular(k, punctured=True) counts = g.counts(lines) assert (len(lines), max(counts)) == (6 * k - 1, 3 * k - 1) for i, gaps in ((k, [1 - 2 * k, 1, k - 1]), (-k, [1 - k, 1, 2 * k - 1])): finite = set(g.top_heights(lines, (1, 0, -i))) - {None} lo, hi = min(finite), max(finite) assert set(range(int(lo), int(hi) + 1)) - finite == set(gaps) qdata = json.loads((DRAFT3 / 'quadratic.json').read_text(encoding='utf-8'))['sets'] removed = {((-1, -1), (1, 1), (1, 0)), ((-1, -1), (-1, -1), (1, 0))} for name, obj in qdata.items(): pts = [tuple(map(tuple, p)) for p in obj['points']] variants = {'X': [pts], 'H': [pts, pts[1:]], 'O': [[p for p in pts if p not in removed]]} for points in variants[name]: vals[len(points)] = max(g.quadratic_valences(points, obj['A'], obj['B'])) assert vals == {10: 5, 11: 5, 14: 6, 15: 6, 22: 11, 23: 10, 26: 13, 27: 13} print('Exceptional (n, t):', sorted(vals.items())) print('Square grids u=1..4, full grids k=1..5, four deletions k=3..5, punctured even K=6..18: PASS') def check_knight_families(): forms = [(3, -2, -2, -1), (-1, 3, 0, 0), (-3, 0, 4, -1), (-15, 0, -10, 43)] weights = (27, 18, 16, 1) assert [sum(x * y for x, y in zip(weights, col)) for col in zip(*forms)] == [0] * 4 assert all(sum(x * y for x, y in zip(row, (9, 3, 8, 5))) == 0 for row in forms) base = g.knight(0, 'A') vals = g.counts(base) top = g.top_heights(base, (1, 0, -7)) hist = Counter(top.values()) assert (len(base), max(vals), vals.count(24)) == (53, 24, 28) assert hist == {2: 9, 3: 10, 4: 4, 12: 1} and top[None] == 12 assert sum((m - 3) * r for m, r in hist.items()) == 53 - 1 - 2 * 24 print('A(0): 53 lines, 28 with 24 vertices; x=7 multiplicities', sorted(hist.items())) cases = [ ('A', 0, 0, 53, 24), ('B', 0, 0, 69, 32), ('B', 8, 1, 61, 28), ('B', 4, 1, 65, 31), ('A', 8, 2, 45, 21), ('A', 4, 2, 49, 23), ] for family, deleted, begin, intercept, cap in cases: for s in range(begin, 4): lines = g.knight(s, family, deleted) n = len(lines) delta = int(family == 'B') a, b = 9 * s + 7 + 2 * delta, 8 * s + 6 + 2 * delta even = [line for line in lines if line != (1, 1, -b)] limit = 30 * s + cap odd_counts, even_counts = g.counts(lines), g.counts(even) assert n == 60 * s + intercept and len(even) == n - 1 assert max(odd_counts) == limit and max(even_counts) == limit if cap in (24, 32, 28): assert (0, 1, a - b) in even and b - a == -s - 1 assert odd_counts[lines.index((1, 0, -a))] == limit assert even_counts[even.index((1, 0, -a))] == limit else: m = 5 * s + 3 + 2 * delta assert (0, 1, b - m) in even and m - b == -3 * s - 3 assert odd_counts[lines.index((1, 2, -m))] == limit assert even_counts[even.index((1, 2, -m))] == limit assert max(g.counts(g.knight(0, 'B', 8))) == 29 print('All 12 sharp-start knight rows checked through s=3; B(0)\\D_8 has n=61, t=29.') def check_hulls_and_ray(): data = json.loads((DRAFT3 / 'hulls.json').read_text(encoding='utf-8')) targets = [Target(*row) for row in data['certificates']] assert len(targets) == len({t.name for t in targets}) == 36 cells = [] for target in targets: result = certify(target, data['bands'][target.band]) assert max(result[1:]) <= 0, (target.name, result) cells.append(result[0]) assert (sum(cells[:16]), sum(cells[16:])) == (2984, 4432) for target in targets[0:16:4]: damaged = replace(target, start=target.start - 1) assert certify(damaged, data['bands'][target.band])[1] > 0 spec = targets[2] progs = progressions(spec, data['bands'][spec.band]) active = active_residues(progs, 12, 0) vals = [count_hull(active, 12, c, 3) for c in range(0, 49, 12)] assert list(active) == [0, 2, 6, 10] and vals == [111, 121, 137, 161, 189] assert [step_bound(active, 12, dc, 3) for dc in (0, 48)] == [98, 180] assert count_hull(active, 12, 0, 0) == 13 print('36 hull certificates verified: 2984 + 4432 = 7416 cells; 4 lowered bounds rejected.') print('Representative A even-s diagonal cell c=0 mod 12, t=3: H =', vals) boundary.check() attainment.check() balance.check() def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('-q', '--quick', action='store_true', help='run only the Section 1 Python box check') args = parser.parse_args() if not __debug__: raise SystemExit('Run without -O; optimized mode disables assertions.') check_python_box() if not args.quick: check_half_constructions() check_knight_families() check_hulls_and_ray() print('All checks passed.') if __name__ == '__main__': main()