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 | """Certify a zero of the reduced contact model using rational intervals.""" |
| 2 | |
| 3 | import argparse |
| 4 | import json |
| 5 | from decimal import Decimal, ROUND_CEILING, ROUND_FLOOR, localcontext |
| 6 | from fractions import Fraction |
| 7 | from itertools import combinations |
| 8 | from pathlib import Path |
| 9 | |
| 10 | from contact_model import determinant, reduced |
| 11 | from polish import solve |
| 12 | |
| 13 | |
| 14 | GRID = 10**100 |
| 15 | RADIUS = Fraction(1, 10**42) |
| 16 | ACTIVE = { |
| 17 | (0, 1, 8): 1, (0, 2, 7): -1, (0, 3, 10): 1, (0, 5, 12): -1, |
| 18 | (0, 6, 12): -1, (1, 5, 8): 1, (1, 9, 11): -1, (2, 3, 5): 1, |
| 19 | (2, 4, 10): 1, (2, 8, 9): -1, (3, 4, 5): 1, (3, 7, 8): 1, |
| 20 | (4, 6, 8): -1, (5, 9, 12): 1, (6, 9, 11): -1, (6, 11, 12): 1, |
| 21 | (7, 8, 9): 1, (7, 11, 12): 1, (8, 10, 11): 1, (8, 10, 12): -1, |
| 22 | } |
| 23 | RESIDUAL_TRIPLES = {(0, 3, 10), (2, 4, 10), (4, 6, 8)} |
| 24 | BOUNDARY = {(1, 0): 0, (1, 1): 1, (4, 1): 1, (5, 1): 0, |
| 25 | (6, 0): 0, (7, 0): 1, (11, 1): 0} |
| 26 | |
| 27 | |
| 28 | class Interval: |
| 29 | def __init__(self, low, high=None): |
| 30 | low = Fraction(low) |
| 31 | high = low if high is None else Fraction(high) |
| 32 | self.low = Fraction((low * GRID).__floor__(), GRID) |
| 33 | self.high = Fraction((high * GRID).__ceil__(), GRID) |
| 34 | |
| 35 | |
| 36 | def wrap(cls, value): |
| 37 | return value if isinstance(value, cls) else cls(value) |
| 38 | |
| 39 | def __add__(self, other): |
| 40 | other = self.wrap(other) |
| 41 | return Interval(self.low + other.low, self.high + other.high) |
| 42 | |
| 43 | __radd__ = __add__ |
| 44 | |
| 45 | def __neg__(self): |
| 46 | return Interval(-self.high, -self.low) |
| 47 | |
| 48 | def __sub__(self, other): |
| 49 | return self + -self.wrap(other) |
| 50 | |
| 51 | def __rsub__(self, other): |
| 52 | return self.wrap(other) + -self |
| 53 | |
| 54 | def __mul__(self, other): |
| 55 | other = self.wrap(other) |
| 56 | products = [x * y for x in (self.low, self.high) for y in (other.low, other.high)] |
| 57 | return Interval(min(products), max(products)) |
| 58 | |
| 59 | __rmul__ = __mul__ |
| 60 | |
| 61 | def __truediv__(self, other): |
| 62 | other = self.wrap(other) |
| 63 | if other.low <= 0 <= other.high: |
| 64 | raise ValueError("a denominator interval contains zero") |
| 65 | return self * Interval(1 / other.high, 1 / other.low) |
| 66 | |
| 67 | def __rtruediv__(self, other): |
| 68 | return self.wrap(other) / self |
| 69 | |
| 70 | def magnitude(self): |
| 71 | return max(abs(self.low), abs(self.high)) |
| 72 | |
| 73 | |
| 74 | class Jet: |
| 75 | """An interval value and its three first derivatives.""" |
| 76 | |
| 77 | def __init__(self, value, gradient=(0, 0, 0)): |
| 78 | self.value = Interval.wrap(value) |
| 79 | self.gradient = tuple(map(Interval.wrap, gradient)) |
| 80 | |
| 81 | |
| 82 | def wrap(cls, value): |
| 83 | return value if isinstance(value, cls) else cls(value) |
| 84 | |
| 85 | def __add__(self, other): |
| 86 | other = self.wrap(other) |
| 87 | return Jet(self.value + other.value, [u + v for u, v in zip(self.gradient, other.gradient)]) |
| 88 | |
| 89 | __radd__ = __add__ |
| 90 | |
| 91 | def __neg__(self): |
| 92 | return Jet(-self.value, [-u for u in self.gradient]) |
| 93 | |
| 94 | def __sub__(self, other): |
| 95 | return self + -self.wrap(other) |
| 96 | |
| 97 | def __rsub__(self, other): |
| 98 | return self.wrap(other) + -self |
| 99 | |
| 100 | def __mul__(self, other): |
| 101 | other = self.wrap(other) |
| 102 | return Jet(self.value * other.value, |
| 103 | [u * other.value + self.value * v for u, v in zip(self.gradient, other.gradient)]) |
| 104 | |
| 105 | __rmul__ = __mul__ |
| 106 | |
| 107 | def __truediv__(self, other): |
| 108 | other = self.wrap(other) |
| 109 | value = self.value / other.value |
| 110 | return Jet(value, [(u - value * v) / other.value for u, v in zip(self.gradient, other.gradient)]) |
| 111 | |
| 112 | def __rtruediv__(self, other): |
| 113 | return self.wrap(other) / self |
| 114 | |
| 115 | |
| 116 | def jacobian(parameters): |
| 117 | jets = [Jet(p, [int(i == j) for j in range(3)]) for i, p in enumerate(parameters)] |
| 118 | _, _, residuals = reduced(*jets) |
| 119 | return [row.gradient for row in residuals] |
| 120 | |
| 121 | |
| 122 | def assert_contacts(): |
| 123 | # The model must enforce the other 17 contacts by substitution alone. |
| 124 | for parameters in [(Fraction(62, 10), Fraction(85, 10), Fraction(101, 10)), |
| 125 | (Fraction(619, 100), Fraction(851, 100), Fraction(1007, 100))]: |
| 126 | points, _, _ = reduced(*parameters) |
| 127 | for triple, sign in ACTIVE.items(): |
| 128 | if triple not in RESIDUAL_TRIPLES: |
| 129 | assert determinant(*(points[i] for i in triple)) == sign * parameters[0] |
| 130 | |
| 131 | |
| 132 | def main(): |
| 133 | parser = argparse.ArgumentParser(description=__doc__) |
| 134 | parser.add_argument("coordinates", type=Path) |
| 135 | args = parser.parse_args() |
| 136 | coordinates = [tuple(map(Fraction, line.split())) for line in args.coordinates.read_text().splitlines()] |
| 137 | if len(coordinates) != 13: |
| 138 | parser.error("expected the 13 polished, ordered coordinates") |
| 139 | x_scale, y_scale = coordinates[11][0], coordinates[6][1] |
| 140 | center = (coordinates[9][1] / y_scale, coordinates[5][0] / x_scale, 1 / y_scale) |
| 141 | singletons = list(map(Interval, center)) |
| 142 | box = [Interval(value - RADIUS, value + RADIUS) for value in center] |
| 143 | at_center = jacobian(singletons) |
| 144 | center_matrix = [[(entry.low + entry.high) / 2 for entry in row] for row in at_center] |
| 145 | preconditioner = solve(center_matrix, [[Fraction(i == j) for j in range(3)] for i in range(3)]) |
| 146 | interval_matrix = jacobian(box) |
| 147 | contraction = max( |
| 148 | sum((int(i == j) - sum(preconditioner[i][k] * interval_matrix[k][j] for k in range(3))).magnitude() |
| 149 | for j in range(3)) |
| 150 | for i in range(3) |
| 151 | ) |
| 152 | _, _, center_residuals = reduced(*singletons) |
| 153 | correction = max(sum(weight * error for weight, error in zip(row, center_residuals)).magnitude() |
| 154 | for row in preconditioner) |
| 155 | assert contraction < 1 and correction + contraction * RADIUS < RADIUS |
| 156 | |
| 157 | normalized, width, _ = reduced(*box) |
| 158 | height = box[2] |
| 159 | assert width.low > 0 and height.low > 0 |
| 160 | points = [(x / width, y / height) for x, y in normalized] |
| 161 | points = [tuple(Interval(BOUNDARY[i, j]) if (i, j) in BOUNDARY else coord |
| 162 | for j, coord in enumerate(point)) for i, point in enumerate(points)] |
| 163 | assert all(0 <= coord.low <= coord.high <= 1 for point in points for coord in point) |
| 164 | area = box[0] / (2 * width * height) |
| 165 | assert area.low > 0 |
| 166 | for triple in combinations(range(len(points)), 3): |
| 167 | if triple not in ACTIVE: |
| 168 | double_area = determinant(*(points[i] for i in triple)) |
| 169 | assert double_area.low > 2 * area.high or double_area.high < -2 * area.high |
| 170 | assert_contacts() |
| 171 | |
| 172 | with localcontext() as ctx: |
| 173 | ctx.prec = 90 |
| 174 | decimal = lambda value: str(Decimal(value.numerator) / Decimal(value.denominator)) |
| 175 | approximate_center = list(map(decimal, center)) |
| 176 | ctx.rounding = ROUND_FLOOR |
| 177 | lower = decimal(area.low) |
| 178 | ctx.rounding = ROUND_CEILING |
| 179 | upper = decimal(area.high) |
| 180 | print(json.dumps({ |
| 181 | "parameter_center_exact": list(map(str, center)), |
| 182 | "parameter_center_approx": approximate_center, |
| 183 | "parameter_box_radius": "1e-42", |
| 184 | "contraction_bound_approx": float(contraction), |
| 185 | "center_correction_approx": float(correction), |
| 186 | "area_interval": [lower, upper], |
| 187 | "critical_triangles": len(ACTIVE), |
| 188 | "strictly_larger_triangles": 286 - len(ACTIVE), |
| 189 | }, indent=2)) |
| 190 | |
| 191 | |
| 192 | if __name__ == "__main__": |
| 193 | main() |