"""Verify the saved n=15 result and proof inputs from Shoal PR #17.""" import json import subprocess import sys from fractions import Fraction from itertools import combinations, pairwise from math import sqrt from pathlib import Path PR = "1fd9685b8c11cdb2ca4a85400023fc04efb3eb57" JOB = "4f09303ea7887ed1a473f6fc12b2abdc56e21836" SOURCE = "routes/hongdy22-codex-n15-search" TRIPLES = tuple(combinations(range(15), 3)) def source(name, revision=PR): return subprocess.check_output(["git", "show", f"{revision}:{SOURCE}/{name}"], text=True) def points(text): result = [tuple(map(Fraction, line.split())) for line in text.splitlines() if line.strip() and not line.lstrip().startswith("#")] assert len(result) == len(set(result)) == 15 assert all(len(point) == 2 and all(0 <= c <= 1 for c in point) for point in result) return result def signed_areas(placement): areas = {} for triple in TRIPLES: i, j, k = triple a, b, c = placement[i], placement[j], placement[k] areas[triple] = sum(x * v - u * y for (x, y), (u, v) in pairwise((a, b, c, a))) / 2 return areas def gradient(placement, triple, orientation): """Signed area is affine in each single coordinate.""" row = [Fraction(0)] * 30 i, j, k = triple for index, after, before in ((i, j, k), (j, k, i), (k, i, j)): next_x, next_y = placement[after] prev_x, prev_y = placement[before] row[2 * index] = orientation * (next_y - prev_y) / 2 row[2 * index + 1] = orientation * (prev_x - next_x) / 2 return row def inverse(matrix): size = len(matrix) rows = [row + [Fraction(i == j) for j in range(size)] for i, row in enumerate(matrix)] for column in range(size): pivot = next(i for i in range(column, size) if rows[i][column]) rows[column], rows[pivot] = rows[pivot], rows[column] scale = rows[column][column] unit_row = [value / scale for value in rows[column]] rows[column] = unit_row for i, row in enumerate(rows): if i != column and row[column]: factor = row[column] rows[i] = [value - factor * unit for value, unit in zip(row, unit_row)] return [row[size:] for row in rows] def dot(left, right): return sum(a * b for a, b in zip(left, right)) def local_certificate(raw, polished, area): raw_areas = signed_areas(raw) minimum = min(map(abs, raw_areas.values())) tight = [t for t, value in raw_areas.items() if abs(value) <= minimum + Fraction("1e-9")] center = [c for point in polished for c in point] free = [i for i, c in enumerate(center) if 0 < c < 1] fixed = [i for i, c in enumerate(center) if c in (0, 1)] radius = Fraction("1e-42") assert len(tight) == len(free) + 1 == 25 assert all(radius < center[i] < 1 - radius for i in free) areas = signed_areas(polished) signs = {t: 1 if raw_areas[t] > 0 else -1 for t in tight} gradients = [gradient(polished, t, signs[t]) for t in tight] jacobian = [[row[i] for i in free] + [Fraction(-1)] for row in gradients] approximate = inverse(jacobian) columns = tuple(zip(*jacobian)) assert all(dot(row, column) == (i == j) for i, row in enumerate(approximate) for j, column in enumerate(columns)) norm = max(sum(map(abs, row)) for row in approximate) contraction = 6 * radius * norm residual = [signs[t] * areas[t] - area for t in tight] correction = max(abs(dot(row, residual)) for row in approximate) assert contraction < 1 and correction + contraction * radius <= radius gap = min(abs(value) for t, value in areas.items() if t not in signs) - area assert gap > 7 * radius and area > radius inverse_error = norm * contraction / (1 - contraction) weights = [-value for value in approximate[-1]] weight_margin = min(weights) - inverse_error weighted_gradient = [dot(weights, derivatives) for derivatives in zip(*gradients)] boundary_margin = min(weighted_gradient[i] * (2 * center[i] - 1) for i in fixed) boundary_margin -= inverse_error / 2 + radius assert weight_margin > 0 and boundary_margin > 0 direction_bound = max(1 / boundary_margin, (norm + inverse_error) * (1 / weight_margin + 1 / (2 * boundary_margin))) local_radius = min(1 / (6 * direction_bound), (area - radius) / 6) assert local_radius > Fraction("3.8e-6") print(f"Exact inverse norm {float(norm):.12g}; q {float(contraction):.12g}; " f"center correction {float(correction):.12g}; inactive gap {float(gap):.12g}") print(f"Weight margin {float(weight_margin):.12g}; boundary margin " f"{float(boundary_margin):.12g}; strict-local radius {float(local_radius):.12g}") def deletion_check(placement): areas = signed_areas(placement) target = Fraction(31, 1485) assert min(map(abs, areas.values())) == target assert sum(abs(area) == target for area in areas.values()) == 54 # Convex witnesses from the PR, with vertex numbers converted to zero-based indices. witnesses = [ [((1, 7, 15), 8), ((2, 7, 11), 31), ((5, 9, 13), 155), ((11, 13, 15), 31)], [((1, 3, 10), 2), ((4, 8, 10), 31)], ] for axis, witness in enumerate(witnesses): rows = [] for vertices, weight in witness: triple = tuple(i - 1 for i in vertices) assert abs(areas[triple]) == target row = gradient(placement, triple, 1 if areas[triple] > 0 else -1) rows.append([weight * coefficient for coefficient in row[axis::2]]) upper_bound = sum(max(0, sum(column)) for column in zip(*rows)) assert upper_bound / sum(weight for _, weight in witness) == target print("Deletion: 54 of 455 triangles attain 31/1485; both axis bounds are 31/1485.") def minimum_moves(placement, target): areas = {t: abs(a) for t, a in signed_areas(placement).items()} for kept in range(15, 0, -1): count = sum(all(areas[t] >= target for t in combinations(subset, 3)) for subset in combinations(range(15), kept)) if count: return 15 - kept, count def main(polished_path): raw = points(source("n15.txt", JOB)) independent = next(track for track in json.loads(source("search-summary.json"))["tracks"] if track["track"] == "independent") assert raw == points("\n".join(independent["coordinates"])) reference = points(source("reference15.dat")) rotated = [(y, 1 - x) for x, y in raw] nearest = [min(((x - u)**2 + (y - v)**2, i) for i, (u, v) in enumerate(reference)) for x, y in rotated] assert len({index for _, index in nearest}) == 15 maximum = max(distance for distance, _ in nearest) assert maximum < Fraction("1.7e-14")**2 print(f"Raw job 101 agrees with its summary; rotation residual {sqrt(float(maximum)):.12g}.") polished = points(polished_path.read_text()) assert polished == points(source("n15.txt")) area = Fraction(json.loads(source("polish-report.json"))["independent"]["area_center"]) local_certificate(raw, polished, area) deletion = points(source("deletion-certificate.dat")) deletion_check(deletion) for placement, target, expected in ((reference, "0.0212105411", (6, 7)), (deletion, "0.0208754209", (7, 2))): moves, count = minimum_moves(placement, Fraction(target)) assert (moves, count) == expected print(f"Target {target}: move at least {moves} points; {count} minimum covers.") if __name__ == "__main__": main(Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).with_name("n15.txt"))