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 | """Exact finite counts for rational coordinates; Python 3.12+, no dependencies.""" |
| 2 | import argparse |
| 3 | from fractions import Fraction |
| 4 | from itertools import combinations |
| 5 | import json |
| 6 | from pathlib import Path |
| 7 | import re |
| 8 | import sys |
| 9 | |
| 10 | MAX_BYTES = 131072 |
| 11 | MAX_POINTS = 80 |
| 12 | RATIONAL = re.compile(r"-?[0-9]{1,32}(?:/[1-9][0-9]{0,31})?\Z") |
| 13 | |
| 14 | |
| 15 | class Invalid(ValueError): |
| 16 | pass |
| 17 | |
| 18 | |
| 19 | def rational(value): |
| 20 | if type(value) is int: |
| 21 | value = str(value) |
| 22 | if not isinstance(value, str) or not RATIONAL.fullmatch(value): |
| 23 | raise Invalid('coordinates must be integers or rational strings with <=32 digits per part') |
| 24 | return Fraction(value) |
| 25 | |
| 26 | |
| 27 | def unique_keys(pairs): |
| 28 | result = {} |
| 29 | for key, value in pairs: |
| 30 | if key in result: |
| 31 | raise Invalid('duplicate JSON key: ' + key) |
| 32 | result[key] = value |
| 33 | return result |
| 34 | |
| 35 | |
| 36 | def parse(data): |
| 37 | if not isinstance(data, dict) or set(data) != {'schema', 'points', 'unit_circles'}: |
| 38 | raise Invalid('expected exactly schema, points, unit_circles') |
| 39 | if type(data['schema']) is not int or data['schema'] != 1: |
| 40 | raise Invalid('schema must be integer 1') |
| 41 | raw = data['points'] |
| 42 | if not isinstance(raw, list) or not 3 <= len(raw) <= MAX_POINTS: |
| 43 | raise Invalid(f'expected 3..{MAX_POINTS} points') |
| 44 | points = [] |
| 45 | for point in raw: |
| 46 | if not isinstance(point, list) or len(point) != 2: |
| 47 | raise Invalid('each point must be [x,y]') |
| 48 | points.append(tuple(map(rational, point))) |
| 49 | if len(set(points)) != len(points): |
| 50 | raise Invalid('points must be distinct after rational reduction') |
| 51 | expected = data['unit_circles'] |
| 52 | if type(expected) is not int or not 0 <= expected <= len(points)*(len(points)-1)//3: |
| 53 | raise Invalid('unit_circles must be a nonnegative integer within the pair bound') |
| 54 | return points, expected |
| 55 | |
| 56 | |
| 57 | def unit_centers(points): |
| 58 | centers = set() |
| 59 | for a, b, c in combinations(points, 3): |
| 60 | ux, uy = b[0]-a[0], b[1]-a[1] |
| 61 | vx, vy = c[0]-a[0], c[1]-a[1] |
| 62 | det = ux*vy-uy*vx |
| 63 | if det == 0: |
| 64 | continue |
| 65 | u2, v2 = ux*ux+uy*uy, vx*vx+vy*vy |
| 66 | dx = (u2*vy-v2*uy)/(2*det) |
| 67 | dy = (ux*v2-vx*u2)/(2*det) |
| 68 | if dx*dx+dy*dy == 1: |
| 69 | centers.add((a[0]+dx, a[1]+dy)) |
| 70 | return centers |
| 71 | |
| 72 | |
| 73 | def verify(data): |
| 74 | points, expected = parse(data) |
| 75 | centers = unit_centers(points) |
| 76 | if len(centers) != expected: |
| 77 | raise Invalid(f'claimed {expected} unit circles; exact count is {len(centers)}') |
| 78 | circles = [] |
| 79 | for x, y in sorted(centers): |
| 80 | incident = [i for i, (px, py) in enumerate(points) if (px-x)**2+(py-y)**2 == 1] |
| 81 | circles.append({'center': [str(x), str(y)], 'point_indices': incident}) |
| 82 | return {'n': len(points), 'unit_circles': len(centers), 'circles': circles} |
| 83 | |
| 84 | |
| 85 | def main(): |
| 86 | parser = argparse.ArgumentParser(description=__doc__) |
| 87 | parser.add_argument('route', type=Path, help='folder containing configuration.json') |
| 88 | parser.add_argument('--output', type=Path, default=Path('verification.json')) |
| 89 | args = parser.parse_args() |
| 90 | path = args.route / 'configuration.json' |
| 91 | try: |
| 92 | if args.output.resolve() == path.resolve(): |
| 93 | print('ERROR: output must differ from input', file=sys.stderr) |
| 94 | return 2 |
| 95 | # Prevent a prior successful run from masquerading as this run. |
| 96 | args.output.unlink(missing_ok=True) |
| 97 | if not args.route.is_dir(): |
| 98 | print('ERROR: route folder does not exist', file=sys.stderr) |
| 99 | return 2 |
| 100 | if path.is_symlink(): |
| 101 | raise Invalid('configuration.json must not be a symlink') |
| 102 | if not path.exists(): |
| 103 | print('SKIP: no configuration.json; no mathematical artifact judged') |
| 104 | return 0 |
| 105 | if not path.is_file(): |
| 106 | raise Invalid('configuration.json must be a plain file') |
| 107 | with path.open('rb') as stream: |
| 108 | raw = stream.read(MAX_BYTES+1) |
| 109 | if len(raw) > MAX_BYTES: |
| 110 | raise Invalid('configuration.json exceeds 128 KiB') |
| 111 | data = json.loads(raw, object_pairs_hook=unique_keys) |
| 112 | result = verify(data) |
| 113 | args.output.write_text(json.dumps(result, indent=2)+'\n', encoding='utf-8') |
| 114 | print(f"PASS: configuration.json: n={result['n']}, exact unit circles={result['unit_circles']}") |
| 115 | return 0 |
| 116 | except (Invalid, ValueError, UnicodeError, RecursionError) as exc: |
| 117 | print('FAIL: '+str(exc), file=sys.stderr) |
| 118 | return 1 |
| 119 | except OSError as exc: |
| 120 | print('ERROR: '+str(exc), file=sys.stderr) |
| 121 | return 2 |
| 122 | |
| 123 | |
| 124 | if __name__ == '__main__': |
| 125 | sys.exit(main()) |