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}.

| 1 | #!/usr/bin/env python3 |
| 2 | """Reproduce PR #6's finite-input claims; no network or git needed by default.""" |
| 3 | import argparse |
| 4 | from datetime import datetime, timezone |
| 5 | from fractions import Fraction |
| 6 | import hashlib |
| 7 | import importlib.util |
| 8 | import json |
| 9 | from pathlib import Path |
| 10 | import platform |
| 11 | import random |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import tempfile |
| 15 | from time import perf_counter |
| 16 | |
| 17 | from oracle import solve as independent |
| 18 | |
| 19 | HERE = Path(__file__).resolve().parent |
| 20 | PR4 = HERE / "upstream/pr4" |
| 21 | PR6 = HERE / "upstream/pr6" |
| 22 | |
| 23 | |
| 24 | def require(condition, message): |
| 25 | if not condition: |
| 26 | raise RuntimeError(message) |
| 27 | |
| 28 | |
| 29 | def load(name, path): |
| 30 | spec = importlib.util.spec_from_file_location(name, path) |
| 31 | module = importlib.util.module_from_spec(spec) |
| 32 | spec.loader.exec_module(module) |
| 33 | return module |
| 34 | |
| 35 | |
| 36 | def command(argv, cwd=None): |
| 37 | start = perf_counter() |
| 38 | run = subprocess.run(argv, cwd=cwd, text=True, capture_output=True) |
| 39 | return {"exit_code": run.returncode, "stdout": run.stdout, |
| 40 | "stderr": run.stderr, "seconds": perf_counter() - start} |
| 41 | |
| 42 | |
| 43 | def suite(path, cwd): |
| 44 | result = command([sys.executable, str(path)], cwd) |
| 45 | require(result["exit_code"] == 0, f"suite failed: {path}\n{result['stderr']}") |
| 46 | require("Ran 5 tests" in result["stderr"] and "\nOK\n" in result["stderr"], |
| 47 | f"expected all five tests: {path}") |
| 48 | result["test_methods_passed"] = 5 |
| 49 | return result |
| 50 | |
| 51 | |
| 52 | def audit(with_git=False): |
| 53 | manifest = json.loads((HERE / "provenance.json").read_text()) |
| 54 | files = [f for src in manifest["sources"].values() for f in src["files"]] |
| 55 | for entry in files: |
| 56 | actual = hashlib.sha256((HERE / entry["snapshot"]).read_bytes()).hexdigest() |
| 57 | require(actual == entry["sha256"], f"snapshot changed: {entry['snapshot']}") |
| 58 | proposal = load("proposal", PR4 / "verify.py") |
| 59 | chord = load("chord", PR6 / "reproduce.py") |
| 60 | report = { |
| 61 | "created_utc": datetime.now(timezone.utc).isoformat(), |
| 62 | "python": platform.python_version(), "platform": platform.platform(), |
| 63 | "pins": {k: v["commit"] for k, v in manifest["sources"].items()}, |
| 64 | "snapshot_files_checked": len(files), |
| 65 | "scope": "Local software reproduction, not a Shoal mathematical check or extremal result", |
| 66 | "suites": {}, "accept": [], "reject": []} |
| 67 | report["suites"]["pr4_original"] = suite(PR4 / "test_verify.py", HERE) |
| 68 | if with_git: |
| 69 | for source in manifest["sources"].values(): |
| 70 | for entry in source["files"]: |
| 71 | blob = subprocess.check_output( |
| 72 | ["git", "show", f"{source['commit']}:{entry['source']}"], cwd=HERE) |
| 73 | require(hashlib.sha256(blob).hexdigest() == entry["sha256"], |
| 74 | f"snapshot differs from pinned Git source: {entry['source']}") |
| 75 | report["snapshot_files_matched_to_git"] = len(files) |
| 76 | # Run the untouched PR #6 suite in its real, detached source tree. |
| 77 | # It requires PR #4's commit object, even if the branch has moved. |
| 78 | with tempfile.TemporaryDirectory(prefix="unit-circle-pr6-") as folder: |
| 79 | tree = Path(folder) / "tree" |
| 80 | added = command(["git", "worktree", "add", "--detach", str(tree), |
| 81 | report["pins"]["pr6"]], HERE) |
| 82 | require(added["exit_code"] == 0, added["stderr"]) |
| 83 | try: |
| 84 | report["suites"]["pr6_original"] = suite( |
| 85 | tree / "routes/rusirui-independent-reproduction/test_reproduce.py", tree) |
| 86 | finally: |
| 87 | removed = command(["git", "worktree", "remove", "--force", str(tree)], HERE) |
| 88 | require(removed["exit_code"] == 0, removed["stderr"]) |
| 89 | else: |
| 90 | report["suites"]["pr6_original"] = {"not_run": "Use --with-git and fetch both pinned commits"} |
| 91 | |
| 92 | config = json.loads((PR4 / "configuration.json").read_text()) |
| 93 | expected_bytes = (PR4 / "example-verification.json").read_bytes() |
| 94 | expected = json.loads(expected_bytes) |
| 95 | require(independent(config) == chord.solve(config) == expected, |
| 96 | "baseline exact witness mismatch") |
| 97 | with tempfile.TemporaryDirectory(prefix="unit-circle-audit-") as folder: |
| 98 | tmp = Path(folder) |
| 99 | output = tmp / "verification.json" |
| 100 | |
| 101 | def cli(route): |
| 102 | output.write_text("stale evidence\n") |
| 103 | run = command([sys.executable, str(PR4 / "verify.py"), str(route), |
| 104 | "--output", str(output)], HERE) |
| 105 | run["output_exists"] = output.exists() |
| 106 | return run |
| 107 | |
| 108 | baseline = cli(PR4) |
| 109 | require(baseline["exit_code"] == 0 and output.exists(), "baseline CLI failed") |
| 110 | require(json.loads(output.read_bytes()) == expected, "baseline parsed JSON differs") |
| 111 | baseline["parsed_json_equal"] = True |
| 112 | baseline["byte_equal"] = output.read_bytes() == expected_bytes |
| 113 | report["baseline"] = baseline |
| 114 | |
| 115 | for outcome in ("accept", "reject"): |
| 116 | paths = sorted((PR4 / "samples" / outcome).glob("*/configuration.json")) |
| 117 | require(len(paths) == 6, f"expected six {outcome} fixtures") |
| 118 | for path in paths: |
| 119 | data = json.loads(path.read_text()) |
| 120 | entry = {"fixture": path.parent.name, "claim": data["unit_circles"]} |
| 121 | run = cli(path.parent) |
| 122 | entry.update(run) |
| 123 | if outcome == "accept": |
| 124 | oracle = independent(data) |
| 125 | require(run["exit_code"] == 0 and output.exists(), str(entry)) |
| 126 | require(oracle == chord.solve(data) == json.loads(output.read_text()), |
| 127 | f"full witnesses differ: {path}") |
| 128 | require(oracle["unit_circles"] == data["unit_circles"], str(entry)) |
| 129 | entry["exact_count"] = oracle["unit_circles"] |
| 130 | entry["full_witnesses_equal"] = True |
| 131 | else: |
| 132 | require(run["exit_code"] == 1 and not output.exists(), str(entry)) |
| 133 | try: |
| 134 | oracle = independent(data) |
| 135 | except ValueError as exc: |
| 136 | require(path.parent.name in ("duplicate", "float"), str(exc)) |
| 137 | entry["invalid_input"] = str(exc) |
| 138 | else: |
| 139 | require(oracle["unit_circles"] != data["unit_circles"], |
| 140 | f"reject does not disagree with claimed count: {path}") |
| 141 | require(oracle == chord.solve(data), f"reject count disagreement: {path}") |
| 142 | entry["exact_count"] = oracle["unit_circles"] |
| 143 | entry["claim_disagrees"] = True |
| 144 | report[outcome].append(entry) |
| 145 | |
| 146 | grid = {"points": [[x, y] for x in range(10) for y in range(8)]} |
| 147 | grid_results = {} |
| 148 | for name, solve in (("independent", independent), ("pr6_chord", chord.solve)): |
| 149 | start = perf_counter() |
| 150 | answer = solve(grid) |
| 151 | grid_results[name] = {"seconds": perf_counter() - start, |
| 152 | "n": answer["n"], "unit_circles": answer["unit_circles"]} |
| 153 | require(answer["n"] == 80 and answer["unit_circles"] == 76, name) |
| 154 | if name == "independent": |
| 155 | grid_oracle = answer |
| 156 | else: |
| 157 | require(answer == grid_oracle, "80-point full witnesses differ") |
| 158 | report["grid_10x8"] = grid_results |
| 159 | |
| 160 | # Independent exact witness comparisons on rational rotations/translations |
| 161 | # and permutations, not merely agreement on a scalar count. |
| 162 | rng = random.Random(104007) |
| 163 | pool = [(x, y) for x in range(-2, 3) for y in range(-2, 3)] |
| 164 | for trial in range(100): |
| 165 | points = rng.sample(pool, rng.randint(3, 12)) |
| 166 | if trial % 2: |
| 167 | points = [[str(Fraction(3, 5)*x - Fraction(4, 5)*y + Fraction(2, 7)), |
| 168 | str(Fraction(4, 5)*x + Fraction(3, 5)*y - Fraction(5, 11))] |
| 169 | for x, y in points] |
| 170 | else: |
| 171 | points = [list(p) for p in reversed(points)] |
| 172 | data = {"points": points} |
| 173 | answer = independent(data) |
| 174 | require(answer == chord.solve(data), f"random witness mismatch: trial {trial}") |
| 175 | proposal_data = {"schema": 1, "points": points, |
| 176 | "unit_circles": answer["unit_circles"]} |
| 177 | require(answer == proposal.verify(proposal_data), f"proposal mismatch: trial {trial}") |
| 178 | report["additional_full_witness_comparisons"] = {"seed": 104007, "cases": 100, "mismatches": 0} |
| 179 | report["passed"] = True |
| 180 | return report |
| 181 | |
| 182 | |
| 183 | def main(): |
| 184 | parser = argparse.ArgumentParser(description=__doc__) |
| 185 | parser.add_argument("--with-git", action="store_true", |
| 186 | help="also run untouched PR #6 suite using fetched commit objects") |
| 187 | parser.add_argument("--output", type=Path) |
| 188 | args = parser.parse_args() |
| 189 | report = audit(args.with_git) |
| 190 | if args.output: |
| 191 | args.output.parent.mkdir(parents=True, exist_ok=True) |
| 192 | args.output.write_text(json.dumps(report, indent=2) + "\n") |
| 193 | print(json.dumps(report, indent=2)) |
| 194 | |
| 195 | |
| 196 | if __name__ == "__main__": |
| 197 | main() |