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 | """Stress for count.py: performance on hostile exact arithmetic, plus a |
| 3 | 3000-round fuzz against the independent O(n^3) reference in test_count.py. |
| 4 | |
| 5 | Run: python3 stress.py |
| 6 | """ |
| 7 | |
| 8 | import json |
| 9 | import math |
| 10 | import os |
| 11 | import random |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import time |
| 15 | from fractions import Fraction |
| 16 | |
| 17 | import test_count as T # positions only: the reference implementation |
| 18 | |
| 19 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 20 | COUNT = os.path.join(HERE, "count.py") |
| 21 | CASES = os.path.join(HERE, "cases") |
| 22 | |
| 23 | |
| 24 | def write(name, points): |
| 25 | os.makedirs(CASES, exist_ok=True) |
| 26 | path = os.path.join(CASES, name + ".json") |
| 27 | with open(path, "w", encoding="utf-8") as handle: |
| 28 | json.dump({"schema": 1, "points": points, "unit_circles": 0}, handle) |
| 29 | return path |
| 30 | |
| 31 | |
| 32 | def time_cli(name, points): |
| 33 | path = write(name, points) |
| 34 | started = time.time() |
| 35 | proc = subprocess.run([sys.executable, COUNT, path], capture_output=True, text=True) |
| 36 | elapsed = time.time() - started |
| 37 | return elapsed, proc |
| 38 | |
| 39 | |
| 40 | def random_rational(n, denominators, rng): |
| 41 | points = set() |
| 42 | while len(points) < n: |
| 43 | points.add((Fraction(rng.randint(-50, 50), rng.choice(denominators)), |
| 44 | Fraction(rng.randint(-50, 50), rng.choice(denominators)))) |
| 45 | return [[f"{x.numerator}/{x.denominator}", f"{y.numerator}/{y.denominator}"] |
| 46 | for x, y in sorted(points)] |
| 47 | |
| 48 | |
| 49 | def pythagorean(n, bound, rng): |
| 50 | """n distinct rational points on the unit circle, denominators ~ bound^2.""" |
| 51 | points = set() |
| 52 | while len(points) < n: |
| 53 | u = rng.randint(1, bound) |
| 54 | v = rng.randint(1, bound) |
| 55 | norm = u * u + v * v |
| 56 | points.add((Fraction(v * v - u * u, norm), Fraction(2 * u * v, norm))) |
| 57 | return [[f"{x.numerator}/{x.denominator}", f"{y.numerator}/{y.denominator}"] |
| 58 | for x, y in sorted(points)] |
| 59 | |
| 60 | |
| 61 | def lcm_digits(points): |
| 62 | scale = 1 |
| 63 | for x, y in points: |
| 64 | for value in (x, y): |
| 65 | q = int(value.split("/")[1]) |
| 66 | scale = scale * q // math.gcd(scale, q) |
| 67 | return len(str(scale)) |
| 68 | |
| 69 | |
| 70 | def main(): |
| 71 | rng = random.Random(7) |
| 72 | print("== CLI performance (n = 80) ==") |
| 73 | families = { |
| 74 | "den_small": random_rational(80, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37], rng), |
| 75 | "den_16digit": random_rational(80, [random.randrange(10**15, 10**16) | 1 for _ in range(20)], rng), |
| 76 | "pyth_U_10^4": pythagorean(80, 10**4, rng), |
| 77 | "pyth_U_10^5": pythagorean(80, 10**5, rng), |
| 78 | "pyth_U_10^6": pythagorean(80, 10**6, rng), |
| 79 | } |
| 80 | for name, points in families.items(): |
| 81 | elapsed, proc = time_cli(name, points) |
| 82 | out = json.loads(proc.stdout) if proc.stdout else None |
| 83 | print(f"{name} | lcm digits {lcm_digits(points)} | exit {proc.returncode} | " |
| 84 | f"{elapsed:.2f}s | count {out['unit_circles'] if out else 'n/a'}") |
| 85 | |
| 86 | print() |
| 87 | print("== fuzz vs O(n^3) reference (3000 rounds) ==") |
| 88 | structured_cache = {} |
| 89 | |
| 90 | def structured(centre, k): |
| 91 | a, b = centre |
| 92 | points = set() |
| 93 | while len(points) < k: |
| 94 | u = rng.randint(-9, 9) |
| 95 | v = rng.randint(1, 9) |
| 96 | norm = u * u + v * v |
| 97 | points.add((Fraction(a) + Fraction(v * v - u * u, norm), |
| 98 | Fraction(b) + Fraction(2 * u * v, norm))) |
| 99 | return points |
| 100 | |
| 101 | bad = 0 |
| 102 | rounds = 0 |
| 103 | started = time.time() |
| 104 | for index in range(3000): |
| 105 | mode = index % 6 |
| 106 | if mode == 0: |
| 107 | n = rng.randint(3, 10) |
| 108 | points = set() |
| 109 | while len(points) < n: |
| 110 | points.add((Fraction(rng.randint(-8, 8), rng.choice([1, 2, 3, 4, 5, 6])), |
| 111 | Fraction(rng.randint(-8, 8), rng.choice([1, 2, 3, 4, 5, 6])))) |
| 112 | points = sorted(points) |
| 113 | elif mode == 1: |
| 114 | centre = (Fraction(rng.randint(-3, 3), rng.choice([1, 2, 3, 4])), |
| 115 | Fraction(rng.randint(-3, 3), rng.choice([1, 2, 3, 4]))) |
| 116 | points = sorted(structured(centre, rng.randint(3, 6))) |
| 117 | elif mode == 2: |
| 118 | pool = [(Fraction(x), Fraction(y)) for x in range(-4, 5) for y in range(-4, 5)] |
| 119 | points = sorted(rng.sample(pool, rng.randint(3, 14))) |
| 120 | elif mode == 3: |
| 121 | c1 = (Fraction(rng.randint(-2, 2)), Fraction(rng.randint(-2, 2))) |
| 122 | c2 = (Fraction(rng.randint(-2, 2), 2), Fraction(rng.randint(-2, 2), 2)) |
| 123 | points = sorted(structured(c1, rng.randint(3, 5)) | structured(c2, rng.randint(3, 5))) |
| 124 | if len(points) < 3: |
| 125 | continue |
| 126 | elif mode == 4: |
| 127 | slope = Fraction(rng.randint(-3, 3), rng.choice([1, 2, 3])) |
| 128 | intercept = Fraction(rng.randint(-3, 3), rng.choice([1, 2, 3])) |
| 129 | xs = rng.sample(range(-6, 7), rng.randint(2, 6)) |
| 130 | points = set((Fraction(x), slope * x + intercept) for x in xs) |
| 131 | points.add((Fraction(rng.randint(-3, 3), rng.choice([1, 2])), |
| 132 | Fraction(rng.randint(-3, 3), rng.choice([1, 2])))) |
| 133 | points = sorted(points) |
| 134 | if len(points) < 3: |
| 135 | continue |
| 136 | else: |
| 137 | n = rng.randint(3, 9) |
| 138 | points = set() |
| 139 | while len(points) < n: |
| 140 | points.add((Fraction(rng.randint(-5, 5), rng.choice([1, 2, 3, 5, 7])), |
| 141 | Fraction(rng.randint(-5, 5), rng.choice([1, 2, 3, 5, 7])))) |
| 142 | points = sorted(points) |
| 143 | |
| 144 | rounds += 1 |
| 145 | got = [(cx, cy) for cx, cy, _ in T.impl.count_unit_circles(points)] |
| 146 | want = T.reference_centres(points) |
| 147 | if got != want: |
| 148 | bad += 1 |
| 149 | print("MISMATCH round", index, len(got), len(want), points) |
| 150 | if bad > 4: |
| 151 | break |
| 152 | |
| 153 | print(f"rounds {rounds} | mismatches {bad} | {time.time() - started:.2f}s") |
| 154 | return 1 if bad else 0 |
| 155 | |
| 156 | |
| 157 | if __name__ == "__main__": |
| 158 | sys.exit(main()) |