Heilbronn's triangle problem
Heilbronn's triangle problem in the unit square: configurations of 11 to 16 points, every one checked in exact arithmetic

| 1 | """Reduce Karpov's 20 equal-area equations to three scalar equations.""" |
| 2 | |
| 3 | import argparse |
| 4 | from decimal import Decimal, localcontext |
| 5 | from pathlib import Path |
| 6 | |
| 7 | |
| 8 | def determinant(p, q, r): |
| 9 | x, y = p |
| 10 | u, v = q |
| 11 | a, b = r |
| 12 | return (u - x) * (b - y) - (v - y) * (a - x) |
| 13 | |
| 14 | |
| 15 | def intersect(p, q, goal, r, s, other_goal): |
| 16 | """Solve det(p,q,z) = goal and det(r,s,z) = other_goal for z.""" |
| 17 | a, b = p[1] - q[1], q[0] - p[0] |
| 18 | c, d = r[1] - s[1], s[0] - r[0] |
| 19 | u = goal + a * p[0] + b * p[1] |
| 20 | v = other_goal + c * r[0] + d * r[1] |
| 21 | cross = a * d - b * c |
| 22 | return (u * d - b * v) / cross, (a * v - u * c) / cross |
| 23 | |
| 24 | |
| 25 | def reduced(alpha, beta, height): |
| 26 | """Return normalized points, width, and three contact residuals.""" |
| 27 | p1, p5, p6, p9, p11 = (0, height), (beta, 0), (0, 1), (1, alpha), (1, 0) |
| 28 | slope = beta - 2 - alpha |
| 29 | ratio = alpha / (1 + slope) |
| 30 | p12 = (1 + ratio, slope * ratio) |
| 31 | shift = alpha / (beta * p12[1] - beta + p12[0]) |
| 32 | p0 = (p12[0] + beta * shift, p12[1] - shift) |
| 33 | gap = alpha * (2 * height - p0[1]) / ( |
| 34 | height * p0[0] - beta * (height - p0[1]) |
| 35 | ) |
| 36 | p8 = ((alpha + beta * gap) / height, height - gap) |
| 37 | |
| 38 | at_zero = determinant(p6, p8, p9) |
| 39 | slope_along_line = determinant((1, 1 + slope), p8, p9) - at_zero |
| 40 | width = (alpha - at_zero) / slope_along_line |
| 41 | p7 = (width, 1 + slope * width) |
| 42 | p2 = intersect(p0, p7, alpha, p8, p9, -alpha) |
| 43 | p3 = intersect(p2, p5, -alpha, p7, p8, alpha) |
| 44 | p4 = (beta + ((p3[0] - beta) * height - alpha) / p3[1], height) |
| 45 | p10 = intersect(p8, p11, -alpha, p8, p12, alpha) |
| 46 | |
| 47 | residuals = ( |
| 48 | determinant(p0, p3, p10) - alpha, |
| 49 | determinant(p2, p4, p10) - alpha, |
| 50 | determinant(p4, p6, p8) + alpha, |
| 51 | ) |
| 52 | return (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12), width, residuals |
| 53 | |
| 54 | |
| 55 | def main(): |
| 56 | parser = argparse.ArgumentParser(description=__doc__) |
| 57 | parser.add_argument("coordinates", type=Path, help="60-decimal output of polish.py in published order") |
| 58 | args = parser.parse_args() |
| 59 | with localcontext() as ctx: |
| 60 | ctx.prec = 90 |
| 61 | points = [tuple(map(Decimal, line.split())) for line in args.coordinates.read_text().splitlines() |
| 62 | if line.strip() and not line.lstrip().startswith("#")] |
| 63 | if len(points) != 13: |
| 64 | parser.error("expected 13 points in the published order") |
| 65 | scale_x = points[11][0] |
| 66 | scale_y = points[6][1] |
| 67 | alpha, beta, height = points[9][1] / scale_y, points[5][0] / scale_x, 1 / scale_y |
| 68 | actual, width, residuals = reduced(alpha, beta, height) |
| 69 | reconstructed = [(x / width, y / height) for x, y in actual] |
| 70 | coordinate_error = max(abs(a - b) for x, y in zip(points, reconstructed) for a, b in zip(x, y)) |
| 71 | assert max(map(abs, residuals)) < Decimal("1e-52") |
| 72 | assert coordinate_error < Decimal("1e-53") |
| 73 | print("alpha, beta, height", *(f"{x:.44f}" for x in (alpha, beta, height))) |
| 74 | print("width", f"{width:.44f}") |
| 75 | print("minimum area from normalized variables", f"{alpha / (2 * width * height):.52f}") |
| 76 | print("maximum residual", max(map(abs, residuals))) |
| 77 | print("maximum coordinate error", coordinate_error) |
| 78 | |
| 79 | |
| 80 | if __name__ == "__main__": |
| 81 | main() |