Unit circles through three of n points
Show that n points in the plane have only o(n²) unit circles passing through three of them; the best construction gives n^{3/2}.

Add a deterministic 151-case corpus and a differential comparator
Browse files138 valid cases (17 structured, 120 fixed-seed random, one 10x8 grid) with hand-derived expectations for the structured ones, 13 invalid inputs a strict counter must reject, and compare.py, which requires every counter to agree on every exact centre and to reject every invalid input. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
+3 −0 routes/rusirui-repro-verifier/.gitignore added View file
| @@ -0,0 +1,3 @@ | ||
| 1 | +corpus/ | |
| 2 | +__pycache__/ | |
| 3 | +verification.json | |
+134 −0 routes/rusirui-repro-verifier/compare.py added View file
| @@ -0,0 +1,134 @@ | ||
| 1 | +#!/usr/bin/env python3 | |
| 2 | +"""Differential comparison of several independent exact counters. | |
| 3 | + | |
| 4 | +Usage: | |
| 5 | + python3 compare.py --corpus <dir> [--artifact <route-dir>] <count.py> [<count.py> ...] | |
| 6 | + | |
| 7 | +Every count.py must accept one path to a configuration.json and print | |
| 8 | +{"n": ..., "unit_circles": ..., "centers": [["x","y"], ...]} on one line. | |
| 9 | + | |
| 10 | +For each valid case the script requires: every counter exits 0, reports the | |
| 11 | +same n, the same number of circles and the same set of exact centres; where | |
| 12 | +the corpus carries an independently derived count, the counters must match it. | |
| 13 | +For each invalid case it requires every counter to exit 1 with empty stdout. | |
| 14 | +Exit status 0 only when every requirement held. | |
| 15 | +""" | |
| 16 | +import argparse | |
| 17 | +import importlib.util | |
| 18 | +import json | |
| 19 | +import subprocess | |
| 20 | +import sys | |
| 21 | +from fractions import Fraction | |
| 22 | +from pathlib import Path | |
| 23 | + | |
| 24 | +TIMEOUT = 300 | |
| 25 | +failures = [] | |
| 26 | + | |
| 27 | + | |
| 28 | +def fail(message): | |
| 29 | + failures.append(message) | |
| 30 | + print("FAIL " + message) | |
| 31 | + | |
| 32 | + | |
| 33 | +def exact(value): | |
| 34 | + return Fraction(str(value)) | |
| 35 | + | |
| 36 | + | |
| 37 | +def parse_centres(raw): | |
| 38 | + return sorted((exact(x), exact(y)) for x, y in raw) | |
| 39 | + | |
| 40 | + | |
| 41 | +def run_counter(path, config): | |
| 42 | + proc = subprocess.run([sys.executable, str(path), str(config)], | |
| 43 | + capture_output=True, text=True, timeout=TIMEOUT) | |
| 44 | + return proc.returncode, proc.stdout.strip(), proc.stderr.strip() | |
| 45 | + | |
| 46 | + | |
| 47 | +def load_artifact(route_dir): | |
| 48 | + """Import the artifact's verify.py as a module under a private name.""" | |
| 49 | + spec = importlib.util.spec_from_file_location("pr4_verify", Path(route_dir) / "verify.py") | |
| 50 | + module = importlib.util.module_from_spec(spec) | |
| 51 | + sys.modules["pr4_verify"] = module | |
| 52 | + spec.loader.exec_module(module) | |
| 53 | + return module | |
| 54 | + | |
| 55 | + | |
| 56 | +def main(): | |
| 57 | + parser = argparse.ArgumentParser() | |
| 58 | + parser.add_argument("--corpus", required=True, type=Path) | |
| 59 | + parser.add_argument("--artifact", type=Path, default=None) | |
| 60 | + parser.add_argument("counters", nargs="+", type=Path) | |
| 61 | + args = parser.parse_args() | |
| 62 | + | |
| 63 | + expect = json.loads((args.corpus / "expect.json").read_text()) if (args.corpus / "expect.json").exists() else {} | |
| 64 | + cases = sorted((args.corpus / "cases").iterdir()) | |
| 65 | + invalid = sorted((args.corpus / "invalid").iterdir()) if (args.corpus / "invalid").exists() else [] | |
| 66 | + artifact = load_artifact(args.artifact) if args.artifact else None | |
| 67 | + | |
| 68 | + agreed = 0 | |
| 69 | + for case in cases: | |
| 70 | + config = case / "configuration.json" | |
| 71 | + claims = {} | |
| 72 | + for counter in args.counters: | |
| 73 | + try: | |
| 74 | + rc, out, err = run_counter(counter, config) | |
| 75 | + except subprocess.TimeoutExpired: | |
| 76 | + fail(f"{case.name}: {counter.parent.parent.name} timed out") | |
| 77 | + continue | |
| 78 | + if rc != 0: | |
| 79 | + fail(f"{case.name}: {counter.parent.parent.name} exited {rc}: {err.splitlines()[:1]}") | |
| 80 | + continue | |
| 81 | + try: | |
| 82 | + data = json.loads(out) | |
| 83 | + claims[counter.parent.parent.name] = (data["n"], data["unit_circles"], parse_centres(data["centers"])) | |
| 84 | + except (json.JSONDecodeError, KeyError, ValueError) as exc: | |
| 85 | + fail(f"{case.name}: {counter.parent.parent.name} printed unusable output ({exc})") | |
| 86 | + | |
| 87 | + if artifact is not None: | |
| 88 | + try: | |
| 89 | + data = json.loads(config.read_text()) | |
| 90 | + points, _ = artifact.parse(data) | |
| 91 | + centres = artifact.unit_centers(points) | |
| 92 | + claims["pr4-verify.unit_centers"] = (len(points), len(centres), parse_centres([[str(x), str(y)] for x, y in centres])) | |
| 93 | + except Exception as exc: # noqa: BLE001 - report anything the artifact raises | |
| 94 | + fail(f"{case.name}: pr4 verify.unit_centers raised {exc!r}") | |
| 95 | + | |
| 96 | + if not claims: | |
| 97 | + continue | |
| 98 | + values = list(claims.values()) | |
| 99 | + if any(v != values[0] for v in values[1:]): | |
| 100 | + detail = "; ".join(f"{name}={v[1]}" for name, v in claims.items()) | |
| 101 | + fail(f"{case.name}: counters disagree ({detail})") | |
| 102 | + continue | |
| 103 | + n, count, _ = values[0] | |
| 104 | + if n != len(json.loads(config.read_text())["points"]): | |
| 105 | + fail(f"{case.name}: reported n={n} does not match the input") | |
| 106 | + continue | |
| 107 | + if expect.get(case.name) is not None and count != expect[case.name]: | |
| 108 | + fail(f"{case.name}: count {count} but hand-derived expectation is {expect[case.name]}") | |
| 109 | + continue | |
| 110 | + agreed += 1 | |
| 111 | + | |
| 112 | + for case in invalid: | |
| 113 | + config = case / "configuration.json" | |
| 114 | + for counter in args.counters: | |
| 115 | + try: | |
| 116 | + rc, out, err = run_counter(counter, config) | |
| 117 | + except subprocess.TimeoutExpired: | |
| 118 | + fail(f"invalid/{case.name}: {counter.parent.parent.name} timed out") | |
| 119 | + continue | |
| 120 | + name = counter.parent.parent.name | |
| 121 | + if rc != 1: | |
| 122 | + fail(f"invalid/{case.name}: {name} exited {rc}, expected 1") | |
| 123 | + elif out: | |
| 124 | + fail(f"invalid/{case.name}: {name} printed to stdout on rejection") | |
| 125 | + | |
| 126 | + names = ", ".join(c.parent.parent.name for c in args.counters) + (", pr4 verify.unit_centers" if artifact else "") | |
| 127 | + print(f"\n{agreed}/{len(cases)} valid cases agreed across: {names}") | |
| 128 | + print(f"{len(invalid)} invalid cases checked for exit 1 and empty stdout") | |
| 129 | + print(f"{len(failures)} failure(s)") | |
| 130 | + return 0 if not failures else 1 | |
| 131 | + | |
| 132 | + | |
| 133 | +if __name__ == "__main__": | |
| 134 | + sys.exit(main()) | |
+136 −0 routes/rusirui-repro-verifier/make_corpus.py added View file
| @@ -0,0 +1,136 @@ | ||
| 1 | +#!/usr/bin/env python3 | |
| 2 | +"""Write a deterministic corpus of configurations for differential testing. | |
| 3 | + | |
| 4 | +Usage: python3 make_corpus.py <target-dir> | |
| 5 | + | |
| 6 | +Layout produced: | |
| 7 | + <target>/cases/<name>/configuration.json valid input for a counter | |
| 8 | + <target>/invalid/<name>/configuration.json input a strict counter must reject | |
| 9 | + <target>/expect.json hand-derived counts for the structured cases | |
| 10 | + ("null" means: only agreement between counters is | |
| 11 | + required, no independent expected value) | |
| 12 | +The random cases use a fixed seed, so two runs produce identical files. | |
| 13 | +""" | |
| 14 | +import json | |
| 15 | +import random | |
| 16 | +import sys | |
| 17 | +from fractions import Fraction | |
| 18 | +from pathlib import Path | |
| 19 | + | |
| 20 | + | |
| 21 | +def num(value): | |
| 22 | + """Render an exact Fraction as JSON int (integral) or "p/q" string.""" | |
| 23 | + value = Fraction(value) | |
| 24 | + if value.denominator == 1: | |
| 25 | + return int(value) | |
| 26 | + return f"{value.numerator}/{value.denominator}" | |
| 27 | + | |
| 28 | + | |
| 29 | +def put(folder, points, claimed=0, schema=1): | |
| 30 | + folder.mkdir(parents=True, exist_ok=True) | |
| 31 | + data = {"schema": schema, "points": [[num(x), num(y)] for x, y in points], "unit_circles": claimed} | |
| 32 | + (folder / "configuration.json").write_text(json.dumps(data) + "\n") | |
| 33 | + return data | |
| 34 | + | |
| 35 | + | |
| 36 | +def on_unit_circle(k, offset=(0, 0)): | |
| 37 | + """k rational points on the unit circle centred at offset (Pythagorean parametrisation).""" | |
| 38 | + ts = [Fraction(0), Fraction(1, 2), Fraction(1), Fraction(2), Fraction(3), Fraction(-1, 2), | |
| 39 | + Fraction(-1), Fraction(5, 2), Fraction(1, 3), Fraction(3, 5), Fraction(-2, 3)] | |
| 40 | + ts = ts[:k] | |
| 41 | + ox, oy = Fraction(offset[0]), Fraction(offset[1]) | |
| 42 | + return [((1 - t * t) / (1 + t * t) + ox, 2 * t / (1 + t * t) + oy) for t in ts] | |
| 43 | + | |
| 44 | + | |
| 45 | +def on_radius_circle(k, radius, offset=(0, 0)): | |
| 46 | + ox, oy = Fraction(offset[0]), Fraction(offset[1]) | |
| 47 | + return [(radius * x + ox, radius * y + oy) for x, y in on_unit_circle(k)] | |
| 48 | + | |
| 49 | + | |
| 50 | +def main(): | |
| 51 | + if len(sys.argv) != 2: | |
| 52 | + print(__doc__.strip().splitlines()[2], file=sys.stderr) | |
| 53 | + return 2 | |
| 54 | + root = Path(sys.argv[1]) | |
| 55 | + cases = root / "cases" | |
| 56 | + invalid = root / "invalid" | |
| 57 | + expect = {} | |
| 58 | + | |
| 59 | + def case(name, points, claimed, independent): | |
| 60 | + put(cases / name, points, claimed) | |
| 61 | + expect[name] = independent | |
| 62 | + | |
| 63 | + # --- structured cases, expectations derived by hand ------------------------- | |
| 64 | + case("three-on-unit-circle", on_unit_circle(3), 1, 1) | |
| 65 | + case("five-on-unit-circle", on_unit_circle(5), 1, 1) | |
| 66 | + case("eight-on-unit-circle", on_unit_circle(8), 1, 1) | |
| 67 | + case("ten-on-unit-circle", on_unit_circle(10), 1, 1) | |
| 68 | + case("five-on-radius-2", on_radius_circle(5, 2), 0, 0) | |
| 69 | + case("five-on-radius-3-shifted", on_radius_circle(5, 3, (1, -2)), 0, 0) | |
| 70 | + case("three-collinear", [(0, 0), (1, 0), (2, 0)], 0, 0) | |
| 71 | + case("five-collinear", [(i, 3) for i in range(5)], 0, 0) | |
| 72 | + case("grid-2x2", [(x, y) for y in range(2) for x in range(2)], 0, 0) | |
| 73 | + case("grid-3x3", [(x, y) for y in range(3) for x in range(3)], 5, 5) | |
| 74 | + case("grid-4x4", [(x, y) for y in range(4) for x in range(4)], 0, None) | |
| 75 | + case("grid-5x5", [(x, y) for y in range(5) for x in range(5)], 0, None) | |
| 76 | + case("grid-10x8", [(x, y) for y in range(8) for x in range(10)], 76, 76) | |
| 77 | + # Two unit circles at centres 0 and 6/5 meet at (3/5, +/-4/5). Adding (1,0) and (11/5,0) | |
| 78 | + # gives four unit circles, one per triple: centres (0,0) and (6/5,0) each carry three of | |
| 79 | + # them, and the mixed triples {(3/5,4/5),(1,0),(11/5,0)} and {(3/5,-4/5),(1,0),(11/5,0)} | |
| 80 | + # lie on the unit circles centred (8/5,4/5) and (8/5,-4/5). Derived by hand, not by code. | |
| 81 | + case("two-crossing-unit-circles", | |
| 82 | + [(Fraction(3, 5), Fraction(4, 5)), (Fraction(3, 5), Fraction(-4, 5)), (1, 0), (Fraction(11, 5), 0)], | |
| 83 | + 4, 4) | |
| 84 | + # Four concyclic plus one off the circle: still one circle, counted once. | |
| 85 | + case("four-concyclic-plus-one", on_unit_circle(4) + [(3, 3)], 1, 1) | |
| 86 | + # A pair exactly distance 2 apart (centres coincide) plus a third point elsewhere. | |
| 87 | + case("tangent-pair-distance-2", [(0, 0), (2, 0), (5, 5)], 0, 0) | |
| 88 | + # Three points on a circle of radius 1 but with rational centre not at the origin. | |
| 89 | + case("shifted-unit-circle", on_unit_circle(4, (Fraction(1, 3), Fraction(2, 7))), 1, 1) | |
| 90 | + # Circumcircle has centre (1/2, 1) and radius sqrt(5)/2, not 1, so no unit circle exists. | |
| 91 | + case("half-centre-nonunit", [(0, 0), (1, 0), (0, 2)], 0, 0) | |
| 92 | + | |
| 93 | + # --- fixed-seed random cases ------------------------------------------------ | |
| 94 | + rng = random.Random(20261128) | |
| 95 | + values = [Fraction(i) for i in range(-3, 4)] + [Fraction(p, q) for q in (2, 3, 5, 7) for p in range(-4, 5)] | |
| 96 | + for trial in range(120): | |
| 97 | + n = rng.randint(3, 12) | |
| 98 | + points = [] | |
| 99 | + seen = set() | |
| 100 | + while len(points) < n: | |
| 101 | + x = rng.choice(values) | |
| 102 | + y = rng.choice(values) | |
| 103 | + if (x, y) in seen: | |
| 104 | + continue | |
| 105 | + seen.add((x, y)) | |
| 106 | + points.append((x, y)) | |
| 107 | + case(f"random-{trial:03d}", points, 0, None) | |
| 108 | + | |
| 109 | + # --- inputs a strict counter must reject ------------------------------------- | |
| 110 | + def bad(name, data): | |
| 111 | + invalid.mkdir(parents=True, exist_ok=True) | |
| 112 | + (invalid / name).mkdir(exist_ok=True) | |
| 113 | + (invalid / name / "configuration.json").write_text(json.dumps(data) + "\n") | |
| 114 | + | |
| 115 | + good = {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]], "unit_circles": 1} | |
| 116 | + bad("float-coordinate", {**good, "points": [[1.0, 0], [0, 1], [-1, 0]]}) | |
| 117 | + bad("boolean-coordinate", {**good, "points": [[True, 0], [0, 1], [-1, 0]]}) | |
| 118 | + bad("decimal-string", {**good, "points": [["1.0", 0], [0, 1], [-1, 0]]}) | |
| 119 | + bad("exponent-string", {**good, "points": [["1e0", 0], [0, 1], [-1, 0]]}) | |
| 120 | + bad("zero-denominator", {**good, "points": [["1/0", 0], [0, 1], [-1, 0]]}) | |
| 121 | + bad("negative-denominator", {**good, "points": [["1/-2", 0], [0, 1], ([-1, 0])]}) | |
| 122 | + bad("33-digit-numerator", {**good, "points": [["1" + "0" * 32, 0], [0, 1], [-1, 0]]}) | |
| 123 | + bad("duplicate-points", {**good, "points": [[1, 0], ["2/2", 0], [0, 1]]}) | |
| 124 | + bad("two-points", {**good, "points": [[1, 0], [0, 1]]}) | |
| 125 | + bad("eighty-one-points", {**good, "points": [[i, 0] for i in range(81)]}) | |
| 126 | + bad("schema-2", {**good, "schema": 2}) | |
| 127 | + bad("missing-unit-circles", {"schema": 1, "points": [[1, 0], [0, 1], [-1, 0]]}) | |
| 128 | + bad("point-not-a-pair", {**good, "points": [[1, 0, 0], [0, 1], [-1, 0]]}) | |
| 129 | + | |
| 130 | + (root / "expect.json").write_text(json.dumps(expect, indent=2, sort_keys=True) + "\n") | |
| 131 | + print(f"wrote {len(expect)} valid cases and {len(list(invalid.iterdir()))} invalid cases to {root}") | |
| 132 | + return 0 | |
| 133 | + | |
| 134 | + | |
| 135 | +if __name__ == "__main__": | |
| 136 | + sys.exit(main()) | |