#!/usr/bin/env python3 """Exact algebra for the common arc formulas and the octagon normalization.""" from fractions import Fraction from math import isqrt import sympy as sp def check_equal(left, right): difference = left - right if isinstance(difference, sp.MatrixBase): assert all(sp.simplify(entry) == 0 for entry in difference), difference else: assert sp.cancel(difference) == 0, difference def check_stationarity(): a, p, n, logarithm = sp.symbols("a p n logarithm", positive=True) m = sp.symbols("m0:3") size = a**2 + 4 missing = (4 - a * p) / (a + p) factors = { (1, 1): (a + p) * (a + n) / size, (-1, -1): size / ((a + p) * (a + n)), (-1, 1): (a + n) / (a + p), (1, -1): (a + p) / (a + n), } def derivative_without_log(neighbor): return 3 * neighbor - (a**2 + neighbor**2) / (a + neighbor) + a**3 / size for (incoming, outgoing), q in factors.items(): previous_label = -incoming % 3 next_label = outgoing % 3 third_label = incoming % 3 margins = [a, missing, missing] margins[previous_label] = p total = sum(margins) # Costate coordinates are divided by sqrt(3). alpha = [-2 * mu - total * margin / 2 + 1 for mu, margin in zip(m, margins)] weights = [total - margin for margin in margins] beta = [-2 * value for value in alpha] beta[third_label] = 2 * ( alpha[0] * weights[0] + alpha[previous_label] * weights[previous_label] ) / weights[third_label] b, c = margins[1:] if outgoing == 1: forcing = a * q * ((a + b) * (logarithm - (q - 1)) + a * (q - 1)**2 / q) else: forcing = -a / q * ((a + c) * logarithm - a * (q - 1) - c * (1 - 1 / q)) switch = ( 2 * q**(2 * outgoing) * (alpha[0] + alpha[next_label]) + q**outgoing * (beta[0] + beta[next_label]) + forcing ) d_in = -(m[0] + m[previous_label]) / (2 * (a + p)**2) d_out = -q**(2 * outgoing) * (m[0] + m[next_label]) / (2 * (a + n)**2) pairing = -m[0] / (2 * size) row = 2 * ( incoming * (a + p) * d_in + outgoing * (a + n) * d_out - (incoming + outgoing) * a * pairing ) - ( incoming * derivative_without_log(p) + outgoing * derivative_without_log(n) - 2 * a * logarithm ) / 8 factor = sp.cancel(sp.diff(switch, logarithm) / sp.diff(row, logarithm)) assert factor != 0 check_equal(switch, factor * row) def check_octagon(): root_two, root_three = sp.sqrt(2), sp.sqrt(3) q = sp.Symbol("q", positive=True) height_factor = 2 * root_two - 1 scale = sp.diag(1, height_factor / root_three) rotation = sp.Matrix([[1, -root_three], [root_three, 1]]) / 2 first_arc = sp.Matrix([ [1, -(q - 1) / height_factor], [1 - 1 / q, 1 - (q + 1 / q - 2) / height_factor], ]) advance = first_arc.subs(q, root_two) * scale * rotation.inv() * scale.inv() check_equal(advance, sp.Matrix([[1, 1], [-1, 1]]) / root_two) calculated = [advance**j * first_arc * scale * rotation**j * sp.Matrix([1, 0]) for j in range(3)] expected = [ sp.Matrix([1, 1 - 1 / q]), sp.Matrix([root_two + 1 - root_two * (q + 1 / q) / 2, 1 - root_two / (2 * q)]), sp.Matrix([root_two - q / 2, q / 2]), ] for left, right in zip(calculated, expected): check_equal(left, right) def check_budget(): denominator = 10**16 def sqrt_bounds(integer): floor = isqrt(integer * denominator**2) return Fraction(floor, denominator), Fraction(floor + 1, denominator) root_two, root_two_upper = sqrt_bounds(2) _, root_three_upper = sqrt_bounds(3) ratio = Fraction(1, 3) log_two_lower = 2 * sum(ratio**(2*i+1) / (2*i+1) for i in range(30)) octagon_upper = ( 2 * root_three_upper * (8 - 4 * root_two - log_two_lower) / (2 * root_two - 1) ) def atan_bounds(x, terms): partial = sum((-1)**i * x**(2*i+1) / (2*i+1) for i in range(terms)) next_partial = partial + (-1)**terms * x**(2*terms+1) / (2*terms+1) return min(partial, next_partial), max(partial, next_partial) pi_lower = 16 * atan_bounds(Fraction(1, 5), 20)[0] - 4 * atan_bounds(Fraction(1, 239), 8)[1] assert root_two < root_two_upper assert octagon_upper < Fraction(312606, 100000) assert pi_lower > Fraction(314159, 100000) assert (3 * octagon_upper / pi_lower - Fraction(3, 2))**2 / 12 < Fraction(9193, 50000) if __name__ == "__main__": if not __debug__: raise SystemExit("Run the exact checks with assertions enabled.") check_stationarity() check_octagon() check_budget() print("PASS: four stationarity rows, octagon frame and boundary arcs, and rational area/cap bounds.")