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 | """Run with python test_verify.py from any directory.""" |
| 2 | from fractions import Fraction as F |
| 3 | from itertools import combinations |
| 4 | import json |
| 5 | from pathlib import Path |
| 6 | import random |
| 7 | import subprocess |
| 8 | import sys |
| 9 | import tempfile |
| 10 | import unittest |
| 11 | |
| 12 | import verify |
| 13 | |
| 14 | HERE = Path(__file__).resolve().parent |
| 15 | |
| 16 | |
| 17 | def determinant(matrix): |
| 18 | if len(matrix) == 1: |
| 19 | return matrix[0][0] |
| 20 | return sum((-1)**j * matrix[0][j] * determinant( |
| 21 | [[row[k] for k in range(len(matrix)) if k != j] for row in matrix[1:]]) |
| 22 | for j in range(len(matrix))) |
| 23 | |
| 24 | |
| 25 | def independent_count(points): |
| 26 | # Radius identity: abc/(4 area) = 1. Deduplicate by complete incident |
| 27 | # point sets, using the 4x4 concyclicity determinant, not centers. |
| 28 | circles = set() |
| 29 | def dist(a, b): |
| 30 | return sum((x-y)**2 for x, y in zip(a, b)) |
| 31 | def row(p): |
| 32 | x, y = p |
| 33 | return [x*x+y*y, x, y, F(1)] |
| 34 | for a, b, c in combinations(points, 3): |
| 35 | cross = (b[0]-a[0])*(c[1]-a[1])-(b[1]-a[1])*(c[0]-a[0]) |
| 36 | if cross and dist(a,b)*dist(b,c)*dist(c,a) == 4*cross*cross: |
| 37 | circles.add(frozenset(i for i,p in enumerate(points) |
| 38 | if determinant([row(a),row(b),row(c),row(p)]) == 0)) |
| 39 | return len(circles) |
| 40 | |
| 41 | |
| 42 | class Tests(unittest.TestCase): |
| 43 | def test_samples(self): |
| 44 | for outcome in ('accept','reject'): |
| 45 | for file in sorted((HERE/'samples'/outcome).glob('*/configuration.json')): |
| 46 | with self.subTest(file=str(file)): |
| 47 | data = json.loads(file.read_text()) |
| 48 | if outcome == 'accept': |
| 49 | result = verify.verify(data) |
| 50 | self.assertEqual(result['unit_circles'], data['unit_circles']) |
| 51 | else: |
| 52 | with self.assertRaises(verify.Invalid): |
| 53 | verify.verify(data) |
| 54 | |
| 55 | def test_independent_oracle(self): |
| 56 | rng = random.Random(104) |
| 57 | grid = [(F(x),F(y)) for x in range(-2,3) for y in range(-2,3)] |
| 58 | pool = grid + [(F(3,5),F(4,5)),(F(-3,5),F(4,5))] |
| 59 | for trial in range(60): |
| 60 | points = rng.sample(pool, rng.randint(3,9)) |
| 61 | if trial % 3 == 0: |
| 62 | points = [(F(3,5)*x-F(4,5)*y+F(2,7), |
| 63 | F(4,5)*x+F(3,5)*y-F(5,11)) for x,y in points] |
| 64 | self.assertEqual(len(verify.unit_centers(points)), independent_count(points)) |
| 65 | |
| 66 | def test_symmetries_and_witnesses(self): |
| 67 | data = json.loads((HERE/'configuration.json').read_text()) |
| 68 | points, count = verify.parse(data) |
| 69 | for order in (points, list(reversed(points))): |
| 70 | moved = [(F(3,5)*x-F(4,5)*y+7,F(4,5)*x+F(3,5)*y-11) for x,y in order] |
| 71 | self.assertEqual(len(verify.unit_centers(moved)),count) |
| 72 | result = verify.verify(data) |
| 73 | for circle in result['circles']: |
| 74 | self.assertGreaterEqual(len(circle['point_indices']),3) |
| 75 | |
| 76 | def test_cli(self): |
| 77 | with tempfile.TemporaryDirectory() as folder: |
| 78 | tmp = Path(folder) |
| 79 | output = tmp/'result.json' |
| 80 | def run(route): |
| 81 | return subprocess.run([sys.executable,str(HERE/'verify.py'),str(route), |
| 82 | '--output',str(output)],capture_output=True,text=True) |
| 83 | good = run(HERE) |
| 84 | self.assertEqual(good.returncode,0,good.stderr) |
| 85 | self.assertIn('exact unit circles=5',good.stdout) |
| 86 | self.assertTrue(output.exists()) |
| 87 | bad = run(HERE/'samples/reject/overcount') |
| 88 | self.assertEqual(bad.returncode,1,bad.stderr) |
| 89 | self.assertFalse(output.exists()) |
| 90 | skipped = run(tmp) |
| 91 | self.assertEqual(skipped.returncode,0) |
| 92 | self.assertIn('no mathematical artifact judged',skipped.stdout) |
| 93 | self.assertEqual(run(tmp/'absent').returncode,2) |
| 94 | (tmp/'configuration.json').write_text('{"schema":1,"schema":1}') |
| 95 | self.assertEqual(run(tmp).returncode,1) |
| 96 | (tmp/'configuration.json').write_bytes(b' '*(verify.MAX_BYTES+1)) |
| 97 | self.assertEqual(run(tmp).returncode,1) |
| 98 | (tmp/'configuration.json').write_bytes(b'\xff') |
| 99 | self.assertEqual(run(tmp).returncode,1) |
| 100 | |
| 101 | def test_limits(self): |
| 102 | good = {'schema':1,'points':[[1,0],[0,1],[-1,0]],'unit_circles':1} |
| 103 | for value in (True, 1.0, '1.0','1/0','NaN', '1e0','9'*33): |
| 104 | bad = {**good,'points':[[value,0],[0,1],[-1,0]]} |
| 105 | with self.subTest(value=value), self.assertRaises(verify.Invalid): |
| 106 | verify.verify(bad) |
| 107 | for value in (True,1.0,-1): |
| 108 | with self.assertRaises(verify.Invalid): |
| 109 | verify.verify({**good,'unit_circles':value}) |
| 110 | with self.assertRaises(verify.Invalid): |
| 111 | verify.verify({**good,'points':[[i,0] for i in range(81)]}) |
| 112 | |
| 113 | |
| 114 | if __name__ == '__main__': |
| 115 | unittest.main(verbosity=2) |