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 | """Optionally verify all eliminated contact equations over Q(a,b,c).""" |
| 2 | |
| 3 | from flint import fmpz_mpoly_ctx |
| 4 | |
| 5 | from certify_reduced import ACTIVE, RESIDUAL_TRIPLES |
| 6 | from contact_model import determinant, reduced |
| 7 | |
| 8 | |
| 9 | RING = fmpz_mpoly_ctx.get(("a", "b", "c"), "lex") |
| 10 | ONE = RING.constant(1) |
| 11 | |
| 12 | |
| 13 | class Rational: |
| 14 | def __init__(self, numerator, denominator=ONE): |
| 15 | if isinstance(numerator, int): |
| 16 | numerator = RING.constant(numerator) |
| 17 | if denominator == ONE: |
| 18 | self.numerator, self.denominator = numerator, denominator |
| 19 | else: |
| 20 | divisor = numerator.gcd(denominator) |
| 21 | self.numerator, self.denominator = numerator // divisor, denominator // divisor |
| 22 | |
| 23 | |
| 24 | def wrap(cls, obj): |
| 25 | return obj if isinstance(obj, cls) else cls(obj) |
| 26 | |
| 27 | def __neg__(self): |
| 28 | answer = object.__new__(type(self)) |
| 29 | answer.numerator = -self.numerator |
| 30 | answer.denominator = self.denominator |
| 31 | return answer |
| 32 | |
| 33 | def __add__(self, obj): |
| 34 | other = self.wrap(obj) |
| 35 | a, b, c, d = self.numerator, self.denominator, other.numerator, other.denominator |
| 36 | if b == d: |
| 37 | return Rational(a + c, b) |
| 38 | divisor = b.gcd(d) |
| 39 | return Rational(a * (d // divisor) + c * (b // divisor), b * (d // divisor)) |
| 40 | |
| 41 | __radd__ = __add__ |
| 42 | |
| 43 | def __sub__(self, obj): |
| 44 | return self + -self.wrap(obj) |
| 45 | |
| 46 | def __rsub__(self, obj): |
| 47 | return self.wrap(obj) + -self |
| 48 | |
| 49 | def __mul__(self, obj): |
| 50 | other = self.wrap(obj) |
| 51 | return Rational(self.numerator * other.numerator, self.denominator * other.denominator) |
| 52 | |
| 53 | __rmul__ = __mul__ |
| 54 | |
| 55 | def __truediv__(self, obj): |
| 56 | other = self.wrap(obj) |
| 57 | return Rational(self.numerator * other.denominator, self.denominator * other.numerator) |
| 58 | |
| 59 | def __rtruediv__(self, obj): |
| 60 | return self.wrap(obj) / self |
| 61 | |
| 62 | |
| 63 | def main(): |
| 64 | variables = list(map(Rational, RING.gens())) |
| 65 | alpha = variables[0] |
| 66 | points, _, _ = reduced(*variables) |
| 67 | eliminated = [item for item in ACTIVE.items() if item[0] not in RESIDUAL_TRIPLES] |
| 68 | for triple, sign in eliminated: |
| 69 | equation = determinant(*(points[i] for i in triple)) - sign * alpha |
| 70 | if not equation.numerator.is_zero(): |
| 71 | raise ValueError(f"nonzero determinant residual on {triple}") |
| 72 | print(f"Verified {len(eliminated)} rational determinant identities.") |
| 73 | |
| 74 | |
| 75 | if __name__ == "__main__": |
| 76 | main() |