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 | """Verify and export an exact algebraic symmetric n = 13 arrangement.""" |
| 2 | |
| 3 | import argparse |
| 4 | import json |
| 5 | from decimal import Decimal, localcontext |
| 6 | from fractions import Fraction |
| 7 | from itertools import combinations |
| 8 | from pathlib import Path |
| 9 | |
| 10 | from certify_reduced import Interval |
| 11 | from contact_model import determinant |
| 12 | from polish import derivatives, evaluate, solve |
| 13 | from row_model import build, physical |
| 14 | |
| 15 | |
| 16 | ROOT_BRACKET = ( |
| 17 | "3.83739706246081112637390764752318643202635825613828", |
| 18 | "3.83739706246081112637390764752318643202635825613830", |
| 19 | ) |
| 20 | AREA_ROOT_BRACKET = ( |
| 21 | "0.02669743180759659253529787057778420984950920180034", |
| 22 | "0.02669743180759659253529787057778420984950920180036", |
| 23 | ) |
| 24 | CONTACTS = { |
| 25 | (0, 1, 8): 1, (0, 2, 12): -1, (0, 4, 7): -1, (0, 4, 8): -1, |
| 26 | (0, 8, 11): 1, (1, 5, 9): -1, (1, 8, 9): 1, (1, 9, 12): 1, |
| 27 | (2, 3, 10): -1, (2, 5, 6): -1, (2, 6, 10): 1, (2, 9, 10): 1, |
| 28 | (3, 7, 11): 1, (3, 10, 11): -1, (3, 11, 12): -1, (4, 5, 9): 1, |
| 29 | (4, 8, 10): -1, (5, 10, 12): -1, (6, 7, 11): -1, (6, 8, 10): -1, |
| 30 | (7, 8, 12): 1, |
| 31 | } |
| 32 | FIXED_BOUNDARY = {(1, 1): 1, (3, 1): 1, (4, 0): 0, (6, 0): 1, (12, 1): 0} |
| 33 | |
| 34 | |
| 35 | def polynomial(coefficients, x): |
| 36 | value = 0 |
| 37 | for coefficient in reversed(coefficients): |
| 38 | value = value * x + coefficient |
| 39 | return value |
| 40 | |
| 41 | |
| 42 | def quotient_field(modulus): |
| 43 | from flint import fmpq, fmpq_poly |
| 44 | |
| 45 | class Element: |
| 46 | def __init__(self, value): |
| 47 | if isinstance(value, Fraction): |
| 48 | value = fmpq(value.numerator, value.denominator) |
| 49 | if not isinstance(value, fmpq_poly): |
| 50 | value = fmpq_poly([value]) |
| 51 | self.value = value % modulus |
| 52 | |
| 53 | |
| 54 | def wrap(cls, value): |
| 55 | return value if isinstance(value, cls) else cls(value) |
| 56 | |
| 57 | def __add__(self, value): |
| 58 | return Element(self.value + self.wrap(value).value) |
| 59 | |
| 60 | __radd__ = __add__ |
| 61 | |
| 62 | def __neg__(self): |
| 63 | return Element(-self.value) |
| 64 | |
| 65 | def __sub__(self, value): |
| 66 | return self + -self.wrap(value) |
| 67 | |
| 68 | def __rsub__(self, value): |
| 69 | return self.wrap(value) + -self |
| 70 | |
| 71 | def __mul__(self, value): |
| 72 | return Element(self.value * self.wrap(value).value) |
| 73 | |
| 74 | __rmul__ = __mul__ |
| 75 | |
| 76 | def __truediv__(self, value): |
| 77 | argument = self.wrap(value).value |
| 78 | if argument.is_zero(): |
| 79 | raise ZeroDivisionError("a denominator is zero in the quotient field") |
| 80 | gcd, inverse, _ = argument.xgcd(modulus) |
| 81 | if gcd.degree() != 0: |
| 82 | raise ValueError("a denominator is not invertible modulo the degree-27 polynomial") |
| 83 | return Element(self.value * (inverse / gcd[0])) |
| 84 | |
| 85 | def __rtruediv__(self, value): |
| 86 | return self.wrap(value) / self |
| 87 | |
| 88 | return Element |
| 89 | |
| 90 | |
| 91 | def check_algebra(field): |
| 92 | from flint import fmpq_poly, fmpz_poly |
| 93 | |
| 94 | modulus = fmpq_poly(field["polynomial_b_ascending"]) |
| 95 | for key in ("polynomial_b_ascending", "polynomial_area_ascending"): |
| 96 | poly = fmpz_poly(field[key]) |
| 97 | content, factors = poly.factor() |
| 98 | assert content == 1 and factors == [(poly, 1)] |
| 99 | Element = quotient_field(modulus) |
| 100 | b = Element(fmpq_poly([0, 1])) |
| 101 | c = polynomial(field["numerator_c_ascending"], b) / polynomial(field["denominator_c_ascending"], b) |
| 102 | points, twice_scaled_area, _ = build(b, c) |
| 103 | for triangle, sign in CONTACTS.items(): |
| 104 | actual = determinant(*(points[i] for i in triangle)) |
| 105 | assert (actual - sign * twice_scaled_area).value.is_zero() |
| 106 | area = twice_scaled_area / (4 * points[1][1]) |
| 107 | assert polynomial(field["polynomial_area_ascending"], area).value.is_zero() |
| 108 | |
| 109 | |
| 110 | def verified_bracket(coefficients, endpoints): |
| 111 | low, high = map(Fraction, endpoints) |
| 112 | flo, fhi = polynomial(coefficients, low), polynomial(coefficients, high) |
| 113 | assert flo * fhi < 0 |
| 114 | derivative = polynomial([i * a for i, a in enumerate(coefficients) if i], |
| 115 | Interval(low, high)) |
| 116 | assert derivative.low > 0 or derivative.high < 0 |
| 117 | return low, high, flo |
| 118 | |
| 119 | |
| 120 | def isolate(coefficients): |
| 121 | low, high, flo = verified_bracket(coefficients, ROOT_BRACKET) |
| 122 | for _ in range(55): |
| 123 | mid = (low + high) / 2 |
| 124 | value = polynomial(coefficients, mid) |
| 125 | if value == 0: |
| 126 | return mid, mid |
| 127 | if flo * value > 0: |
| 128 | low, flo = mid, value |
| 129 | else: |
| 130 | high = mid |
| 131 | return low, high |
| 132 | |
| 133 | |
| 134 | def certify_gauge(points, area): |
| 135 | free = [(i, j) for i in range(13) for j in (0, 1) |
| 136 | if (i, j) not in FIXED_BOUNDARY and (i, j) != (12, 0)] |
| 137 | _, jacobian = evaluate(points, free, list(CONTACTS.items()), [points[i][j] for i, j in free] + [area]) |
| 138 | jacobian = [list(map(Interval.wrap, row)) for row in jacobian] |
| 139 | size = len(jacobian) |
| 140 | with localcontext() as ctx: |
| 141 | ctx.prec = 90 |
| 142 | midpoint = lambda a: (Decimal(a.low.numerator) / Decimal(a.low.denominator) |
| 143 | + Decimal(a.high.numerator) / Decimal(a.high.denominator)) / 2 |
| 144 | center = [[midpoint(value) for value in row] for row in jacobian] |
| 145 | identity = [[Decimal(i == j) for j in range(size)] for i in range(size)] |
| 146 | approx = [[Fraction(f"{value:.70f}") for value in row] for row in solve(center, identity)] |
| 147 | columns = list(zip(*jacobian)) |
| 148 | residual = max( |
| 149 | sum((int(i == j) - sum(a * b for a, b in zip(row, column))).magnitude() |
| 150 | for j, column in enumerate(columns)) |
| 151 | for i, row in enumerate(approx) |
| 152 | ) |
| 153 | assert residual < 1 |
| 154 | error = max(sum(map(abs, row)) for row in approx) * residual / (1 - residual) |
| 155 | weights = [-value for value in approx[-1]] |
| 156 | weight_margin = min(weights) - error |
| 157 | assert weight_margin > 0 |
| 158 | |
| 159 | gradient = [[Interval(0), Interval(0)] for _ in range(13)] |
| 160 | for (triple, sign), weight in zip(CONTACTS.items(), weights): |
| 161 | for point, derivative in zip(triple, derivatives(points, triple)): |
| 162 | for axis, value in enumerate(derivative): |
| 163 | gradient[point][axis] += weight * sign * value / 2 |
| 164 | boundary_margin = min( |
| 165 | (gradient[i][j].low if value else -gradient[i][j].high) - error / 2 |
| 166 | for (i, j), value in FIXED_BOUNDARY.items() |
| 167 | ) |
| 168 | assert boundary_margin > 0 |
| 169 | return float(residual), float(weight_margin), float(boundary_margin) |
| 170 | |
| 171 | |
| 172 | def main(): |
| 173 | parser = argparse.ArgumentParser(description=__doc__) |
| 174 | parser.add_argument("--output", type=Path, required=True) |
| 175 | args = parser.parse_args() |
| 176 | data = json.loads(Path(__file__).with_name("row-field.json").read_text()) |
| 177 | for key in ("numerator_c_ascending", "denominator_c_ascending"): |
| 178 | data[key] = list(map(Fraction, data[key])) |
| 179 | check_algebra(data) |
| 180 | low, high = isolate(data["polynomial_b_ascending"]) |
| 181 | b = Interval(low, high) |
| 182 | c = polynomial(data["numerator_c_ascending"], b) / polynomial(data["denominator_c_ascending"], b) |
| 183 | normalized, q, _ = build(b, c) |
| 184 | area = q / (4 * normalized[1][1]) |
| 185 | area_low, area_high, _ = verified_bracket(data["polynomial_area_ascending"], AREA_ROOT_BRACKET) |
| 186 | assert area_low < area.low <= area.high < area_high |
| 187 | coords = [tuple(map(Interval.wrap, p)) for p in physical(normalized)] |
| 188 | coords = [tuple(Interval(FIXED_BOUNDARY[i, j]) if (i, j) in FIXED_BOUNDARY else coordinate |
| 189 | for j, coordinate in enumerate(point)) for i, point in enumerate(coords)] |
| 190 | assert all(0 <= u.low <= u.high <= 1 for point in coords for u in point) |
| 191 | for triple in combinations(range(13), 3): |
| 192 | if triple not in CONTACTS: |
| 193 | twice = determinant(*(coords[i] for i in triple)) |
| 194 | assert twice.low > 2 * area.high or twice.high < -2 * area.high |
| 195 | assert area.low > Fraction("0.0266974318") and area.high < Fraction("0.0266974319") |
| 196 | residual, min_weight, boundary_margin = certify_gauge(coords, area) |
| 197 | shear_limit = Fraction("0.0117") |
| 198 | for i, (x, y) in enumerate(coords): |
| 199 | if i not in (4, 6): |
| 200 | sheared_x = x + Interval(-shear_limit, shear_limit) * (y - coords[4][1]) |
| 201 | assert 0 <= sheared_x.low <= sheared_x.high <= 1 |
| 202 | |
| 203 | with localcontext() as ctx: |
| 204 | ctx.prec = 115 |
| 205 | decimal = lambda value: Decimal(value.numerator) / Decimal(value.denominator) |
| 206 | center = lambda item: decimal((item.low + item.high) / 2) |
| 207 | args.output.write_text("".join(f"{center(x):.60f} {center(y):.60f}\n" for x, y in coords)) |
| 208 | print(json.dumps({ |
| 209 | "degree": len(data["polynomial_b_ascending"]) - 1, |
| 210 | "root_bracket": ROOT_BRACKET, |
| 211 | "root_approx": f"{center(b):.61f}", |
| 212 | "area_approx": f"{center(area):.61f}", |
| 213 | "area_root_bracket": AREA_ROOT_BRACKET, |
| 214 | "area_bracket_rational": [str(area.low), str(area.high)], |
| 215 | "critical_triangles": len(CONTACTS), |
| 216 | "strictly_larger_triangles": 286 - len(CONTACTS), |
| 217 | "gauge_inverse_residual_approx": residual, |
| 218 | "minimum_contact_weight_bound_approx": min_weight, |
| 219 | "minimum_boundary_margin_bound_approx": boundary_margin, |
| 220 | "feasible_shear_magnitude_at_least": "0.0117", |
| 221 | }, indent=2)) |
| 222 | |
| 223 | |
| 224 | if __name__ == "__main__": |
| 225 | main() |