"""Certify a zero of the reduced contact model using rational intervals.""" import argparse import json from decimal import Decimal, ROUND_CEILING, ROUND_FLOOR, localcontext from fractions import Fraction from itertools import combinations from pathlib import Path from contact_model import determinant, reduced from polish import solve GRID = 10**100 RADIUS = Fraction(1, 10**42) ACTIVE = { (0, 1, 8): 1, (0, 2, 7): -1, (0, 3, 10): 1, (0, 5, 12): -1, (0, 6, 12): -1, (1, 5, 8): 1, (1, 9, 11): -1, (2, 3, 5): 1, (2, 4, 10): 1, (2, 8, 9): -1, (3, 4, 5): 1, (3, 7, 8): 1, (4, 6, 8): -1, (5, 9, 12): 1, (6, 9, 11): -1, (6, 11, 12): 1, (7, 8, 9): 1, (7, 11, 12): 1, (8, 10, 11): 1, (8, 10, 12): -1, } RESIDUAL_TRIPLES = {(0, 3, 10), (2, 4, 10), (4, 6, 8)} BOUNDARY = {(1, 0): 0, (1, 1): 1, (4, 1): 1, (5, 1): 0, (6, 0): 0, (7, 0): 1, (11, 1): 0} class Interval: def __init__(self, low, high=None): low = Fraction(low) high = low if high is None else Fraction(high) self.low = Fraction((low * GRID).__floor__(), GRID) self.high = Fraction((high * GRID).__ceil__(), GRID) @classmethod def wrap(cls, value): return value if isinstance(value, cls) else cls(value) def __add__(self, other): other = self.wrap(other) return Interval(self.low + other.low, self.high + other.high) __radd__ = __add__ def __neg__(self): return Interval(-self.high, -self.low) def __sub__(self, other): return self + -self.wrap(other) def __rsub__(self, other): return self.wrap(other) + -self def __mul__(self, other): other = self.wrap(other) products = [x * y for x in (self.low, self.high) for y in (other.low, other.high)] return Interval(min(products), max(products)) __rmul__ = __mul__ def __truediv__(self, other): other = self.wrap(other) if other.low <= 0 <= other.high: raise ValueError("a denominator interval contains zero") return self * Interval(1 / other.high, 1 / other.low) def __rtruediv__(self, other): return self.wrap(other) / self def magnitude(self): return max(abs(self.low), abs(self.high)) class Jet: """An interval value and its three first derivatives.""" def __init__(self, value, gradient=(0, 0, 0)): self.value = Interval.wrap(value) self.gradient = tuple(map(Interval.wrap, gradient)) @classmethod def wrap(cls, value): return value if isinstance(value, cls) else cls(value) def __add__(self, other): other = self.wrap(other) return Jet(self.value + other.value, [u + v for u, v in zip(self.gradient, other.gradient)]) __radd__ = __add__ def __neg__(self): return Jet(-self.value, [-u for u in self.gradient]) def __sub__(self, other): return self + -self.wrap(other) def __rsub__(self, other): return self.wrap(other) + -self def __mul__(self, other): other = self.wrap(other) return Jet(self.value * other.value, [u * other.value + self.value * v for u, v in zip(self.gradient, other.gradient)]) __rmul__ = __mul__ def __truediv__(self, other): other = self.wrap(other) value = self.value / other.value return Jet(value, [(u - value * v) / other.value for u, v in zip(self.gradient, other.gradient)]) def __rtruediv__(self, other): return self.wrap(other) / self def jacobian(parameters): jets = [Jet(p, [int(i == j) for j in range(3)]) for i, p in enumerate(parameters)] _, _, residuals = reduced(*jets) return [row.gradient for row in residuals] def assert_contacts(): # The model must enforce the other 17 contacts by substitution alone. for parameters in [(Fraction(62, 10), Fraction(85, 10), Fraction(101, 10)), (Fraction(619, 100), Fraction(851, 100), Fraction(1007, 100))]: points, _, _ = reduced(*parameters) for triple, sign in ACTIVE.items(): if triple not in RESIDUAL_TRIPLES: assert determinant(*(points[i] for i in triple)) == sign * parameters[0] def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("coordinates", type=Path) args = parser.parse_args() coordinates = [tuple(map(Fraction, line.split())) for line in args.coordinates.read_text().splitlines()] if len(coordinates) != 13: parser.error("expected the 13 polished, ordered coordinates") x_scale, y_scale = coordinates[11][0], coordinates[6][1] center = (coordinates[9][1] / y_scale, coordinates[5][0] / x_scale, 1 / y_scale) singletons = list(map(Interval, center)) box = [Interval(value - RADIUS, value + RADIUS) for value in center] at_center = jacobian(singletons) center_matrix = [[(entry.low + entry.high) / 2 for entry in row] for row in at_center] preconditioner = solve(center_matrix, [[Fraction(i == j) for j in range(3)] for i in range(3)]) interval_matrix = jacobian(box) contraction = max( sum((int(i == j) - sum(preconditioner[i][k] * interval_matrix[k][j] for k in range(3))).magnitude() for j in range(3)) for i in range(3) ) _, _, center_residuals = reduced(*singletons) correction = max(sum(weight * error for weight, error in zip(row, center_residuals)).magnitude() for row in preconditioner) assert contraction < 1 and correction + contraction * RADIUS < RADIUS normalized, width, _ = reduced(*box) height = box[2] assert width.low > 0 and height.low > 0 points = [(x / width, y / height) for x, y in normalized] points = [tuple(Interval(BOUNDARY[i, j]) if (i, j) in BOUNDARY else coord for j, coord in enumerate(point)) for i, point in enumerate(points)] assert all(0 <= coord.low <= coord.high <= 1 for point in points for coord in point) area = box[0] / (2 * width * height) assert area.low > 0 for triple in combinations(range(len(points)), 3): if triple not in ACTIVE: double_area = determinant(*(points[i] for i in triple)) assert double_area.low > 2 * area.high or double_area.high < -2 * area.high assert_contacts() with localcontext() as ctx: ctx.prec = 90 decimal = lambda value: str(Decimal(value.numerator) / Decimal(value.denominator)) approximate_center = list(map(decimal, center)) ctx.rounding = ROUND_FLOOR lower = decimal(area.low) ctx.rounding = ROUND_CEILING upper = decimal(area.high) print(json.dumps({ "parameter_center_exact": list(map(str, center)), "parameter_center_approx": approximate_center, "parameter_box_radius": "1e-42", "contraction_bound_approx": float(contraction), "center_correction_approx": float(correction), "area_interval": [lower, upper], "critical_triangles": len(ACTIVE), "strictly_larger_triangles": 286 - len(ACTIVE), }, indent=2)) if __name__ == "__main__": main()