"""Check the nonic root boxes against the full partition domain, degree by degree.""" import argparse import json import subprocess from functools import lru_cache from pathlib import Path GROUPS = [(22, 23), (24, 24), (25, 26), (27, 29), (30, 32), (33, 34), (35, 37), (38, 39)] LONG = (90, 9, 5, 4, 3, 2, 2) DEEP = (19, 12, 9, 7, 5, 5, 4) @lru_cache(None) def rectangle_fit(height, width, size): if size < 0 or size > height * width: return 0 if not size: return 1 return rectangle_fit(height - 1, width, size) + rectangle_fit(height, width - 1, size - height) def check_intervals(records): print("b\tintervals\tshapes") for b in range(18, 22): total = 0 for x in range(29, 9 * b + 1): for y in range(b, min(x, 9 * b - x) + 1): tail = 9 * b - x - y total += rectangle_fit(7, y, tail) - rectangle_fit(3, y, tail) if b >= 19: total -= rectangle_fit(7, y - 10, tail - 70) intervals, shapes = records[str(b)] if shapes != total: raise ValueError(f"incomplete second-row intervals in degree {b}") print(f"{b}\t{intervals}\t{shapes}") def unrestricted(weight): counts = [1] + [0] * weight for height in range(1, 10): for n in range(height, weight + 1): counts[n] += counts[n - height] return counts def chart_counts(cap, weight): """Bound rows 3..9. At each row, previous[v] allows the next row to be at most v.""" empty = [1] + [0] * weight previous = [empty] * (weight + 1) for maximum in reversed((weight, weight) + cap): row = [empty] for v in range(1, maximum + 1): smaller = row[-1] row.append(smaller[:v] + [x + y for x, y in zip(smaller[v:], previous[v])]) previous = row + [row[-1]] * (weight - maximum) return previous[weight] @lru_cache(maxsize=4096) def box_polynomial(widths, weight): # The degree fixes gap 1; letting it range freely supplies 1/(1-z). counts = [1] * (weight + 1) for height, width in enumerate(widths, 2): stop = height * (width + 1) terms = counts.copy() for n in range(height, weight + 1): terms[n] += terms[n - height] if n >= stop: terms[n] -= counts[n - stop] counts = terms return counts def exterior_key(lo, hi, caps): """Prove the entire box first exits all charts at a specific row above a fixed suffix.""" suffix = () for row in range(9, 2, -1): below = suffix[0] if suffix else 0 value = below + lo[row - 2] surviving = [cap for cap in caps if value <= cap[row - 3]] if not surviving: return row, suffix if lo[row - 2] != hi[row - 2]: raise ValueError("unfixed suffix below the first chart violation") suffix = (value,) + suffix caps = surviving raise ValueError("root intersects a tail chart") def check_group(roots, first, last): weight = 9 * last partitions = unrestricted(weight) caps = (LONG, DEEP) if first >= 33 else () union = [0] * (weight + 1) if caps: overlap = tuple(map(min, zip(*caps))) union = [ x + y - z for x, y, z in zip( chart_counts(LONG, weight), chart_counts(DEEP, weight), chart_counts(overlap, weight), ) ] keys = set() counted = {b: 0 for b in range(first, last + 1)} for bounds in roots: lo, hi = bounds[::2], bounds[1::2] if len(bounds) != 16 or any(x < 0 or x > y for x, y in zip(lo, hi)) or hi[-1] > 9: raise ValueError("invalid gap box") key = exterior_key(lo, hi, caps) if caps else () if key in keys: raise ValueError(f"duplicate root class {key}") keys.add(key) start = sum(i * v for i, v in enumerate(lo, 2)) poly = box_polynomial(tuple(y - x for x, y in zip(lo, hi)), weight) for b in counted: if 9 * b >= start: counted[b] += poly[9 * b - start] for b, outside in counted.items(): n = 9 * b rectangle = partitions[n - 90] if rectangle + union[n] + outside != partitions[n]: raise ValueError(f"incomplete root cover in degree {b}") print(f"{b}\t{partitions[n]}\t{rectangle}\t{union[n]}\t{outside}\t{len(keys)}") def collect_inputs(box_program, fiber_program): intervals = {} for b in range(18, 22): result = subprocess.check_output([fiber_program, "--intervals", str(b)], text=True) intervals[str(b)] = [int(x) for x in result.split()] roots = {} for first, last in GROUPS: result = subprocess.check_output([box_program, "--roots", str(first), str(last)], text=True) roots[f"{first}-{last}"] = [[int(x) for x in line.split()] for line in result.splitlines()] return {"intervals": intervals, "roots": roots} if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("gap_box", nargs="?", type=Path, help="compiled gap-box executable") parser.add_argument( "fiber_comparison", nargs="?", type=Path, help="compiled fiber-comparison executable" ) parser.add_argument( "--input", type=Path, help="read the recorded intervals and root boxes from JSON" ) args = parser.parse_args() if args.input and not (args.gap_box or args.fiber_comparison): data = json.loads(args.input.read_text()) elif args.gap_box and args.fiber_comparison and not args.input: data = collect_inputs(args.gap_box.resolve(), args.fiber_comparison.resolve()) else: parser.error("supply the two executables or --input FILE") check_intervals(data["intervals"]) print("b\tall\trectangle\tinitial_chart_union\troot_shapes\troots") for first, last in GROUPS: check_group(data["roots"][f"{first}-{last}"], first, last)