#!/usr/bin/env python3 """Exact algebra for the revised geometric and signed-closure interfaces.""" from itertools import product import sympy as sp def assert_equal(left, right): difference = left - right entries = difference if isinstance(difference, sp.MatrixBase) else [difference] for entry in entries: assert sp.factor(entry) == 0, entry def matrix_product(matrices): result = sp.eye(2) for matrix in matrices: result *= matrix return result def margins_and_hexagon(): x, y, side, q = sp.symbols("x y side q", real=True, positive=True) z = x + sp.I * y chart = (z + side) / (side - z) assert_equal(-sp.re(chart) / sp.im(chart), (x**2 + y**2 - side**2) / (2 * side * y)) side_value = sp.sqrt(3) / 3 def margins(horizontal, height): return ( sp.sqrt(3) * (horizontal**2 + height**2 - side_value**2) / height, 2 * (side_value - horizontal) / height, 2 * (side_value + horizontal) / height, ) p0, p1, p2 = margins(x, y) # The arc towards the right ideal vertex scales the ray by 1/q. updated = margins(side_value + (x - side_value) / q, y / q) for value, expected in zip(updated, ((p1 + p0) / q - p1, p1, (p1 + p2) * q - p1)): assert_equal(value, expected) corners = [sp.Matrix(sp.symbols(f"x{i} y{i}")) for i in range(3)] corners += [-corner for corner in corners] pairs = list(zip(corners, corners[1:] + corners[:1])) midpoints = [(left + right) / 2 for left, right in pairs] area = sum(sp.Matrix.hstack(left, right).det() for left, right in pairs) / 2 assert_equal(midpoints[2], midpoints[1] - midpoints[0]) assert_equal(area, 4 * sp.Matrix.hstack(*midpoints[:2]).det()) print("PASS margin-distance chart, active ray transport, and hexagon midpoint area") def switches_and_disk(): signs_by_length = [] for count in range(1, 8): survivors = 0 for signs in product((1, -1), repeat=count): if sum(signs) % 3 != 1: continue cyclic = signs * 3 if any(cyclic[i:i+3] in ((1, -1, 1), (-1, -1, -1)) for i in range(count)): continue survivors += 1 signs_by_length.append(survivors) assert signs_by_length == [1, 0, 0, 1, 5, 3, 1] x, y, r, s = sp.symbols("x y r s", real=True) norm = r**2 + s**2 disk_form = sp.Matrix([[(1 - x)**2 + y**2, 2*y], [2*y, (1 + x)**2 + y**2]]) disk_form /= 1 - x**2 - y**2 vector = sp.Matrix([r, s]) endpoint = (r - sp.I*s)**2 / norm rayleigh = (vector.T * disk_form * vector)[0] / norm assert_equal(disk_form.det(), 1) assert_equal( rayleigh, (1+x**2+y**2-2*sp.re(sp.conjugate(endpoint)*(x + sp.I*y))) / (1-x**2-y**2), ) print("PASS short twisted sign patterns and the disk Rayleigh convention") def bridge_coordinate_change(): x, beta, alpha, scale = sp.symbols("x beta alpha scale", positive=True) start, end, middle = 1 + x, alpha + beta, x + beta unused_start = (scale - x) / start unused_end = (scale - alpha * beta) / end state_start = sp.Matrix([[-x, x + unused_start], [-start, x]]) state_end = sp.Matrix([[-alpha, alpha + unused_end], [-end, alpha]]) factor_left, factor_right = middle / start, middle / end transfer_left = sp.diag(factor_left, 1 / factor_left) transfer_right = sp.Matrix([[1 / factor_right, factor_right - 1 / factor_right], [0, factor_right]]) triangular_bridge = -state_start * transfer_left * transfer_right * state_end / scale basis = sp.Matrix([[-1, 2], [1, 0]]) cover_bridge = ( sp.Matrix([[-unused_start - 2, x + 2], [unused_start, x]]) * sp.Matrix([[start / end, 0], [(start**2 - middle**2) / (start * end), end / start]]) * sp.Matrix([[-alpha, alpha + 2 * beta], [unused_end, unused_end + 2 * beta]]) / (2 * scale) ) assert_equal(cover_bridge, basis * triangular_bridge * basis.inv()) assert_equal(cover_bridge.det(), 1) print("PASS short bridge change of basis and determinant") def signed_seven_arc_chart(): t = sp.symbols("t0:7", positive=True) matrices = [sp.Matrix([[0, 1 + x], [-1, 1 + x]]) for x in t] # These are q_i times the physical matrices M(q_i), so scaling preserves signs. first_four = matrix_product(matrices[:4]) last_three = matrix_product(matrices[4:]) assert_equal(first_four[0, 1], (1+t[0])*(1+t[3])*(t[1]*t[2]-1)) assert_equal(first_four[1, 0], (1+t[2])*(1-t[0]*t[1])) assert_equal(last_three[0, 1], (1+t[4])*(1+t[6])*t[5]) assert_equal(last_three[1, 0], -t[4]*(1+t[5])) branch_factor = 1+t[0]+t[2]*(1-t[0]*t[1]) denominator = t[0]*(1+t[1])+t[3]*branch_factor tails = { t[4]: (1+t[2])*(1-t[0]*t[1])/denominator, t[5]: (1-t[1]*t[2])*denominator/((1+t[1])*(1+t[2])), t[6]: (1+t[1])*(1-t[2]*t[3])/denominator, } full = matrix_product(matrices) assert_equal(full[0, 1].subs(tails), 0) assert_equal(full[1, 0].subs(tails), 0) assert_equal((full[0, 0]-full[1, 1]).subs(tails), 0) assert_equal( full[0, 0].subs(tails), (1+t[0])*(1+t[3])*branch_factor*(1+t[1]+t[3]*(1-t[1]*t[2]))/denominator, ) print("PASS seven-arc branch signs, tail formulas, and positive monodromy") def forward_state_and_cap(): q = sp.Symbol("q", positive=True) state_transfer = sp.Matrix([[q, q-4], [q, q]]) / 2 assert_equal(state_transfer, sp.Matrix([[1,-3],[1,1]]) / 2 * sp.Matrix([[q,q-1],[0,1]])) basis = sp.Matrix([[-1,2],[1,0]]) root = sp.sqrt(q) full_factor = sp.Matrix([[0,root],[-1/root,root]]) assert_equal(state_transfer / root, basis * full_factor * basis.inv()) r = sp.symbols("r0:7", positive=True) half_product = matrix_product(sp.Matrix([[0,1],[-weight,1]]) for weight in reversed(r)) a, b, c, d, e = r[1:6] path = 1-a-b-c-d-e+a*c+a*d+a*e+b*d+b*e+c*e-a*c*e assert_equal(half_product[0, 1], path) assert_equal((basis * half_product * basis.inv())[1, 0], path / 2) assert_equal(path, (a+b-1)*d - (1-e)*(a+b-1+c*(1-a))) x, y, factor = sp.symbols("x y factor", positive=True) logarithm = sp.Symbol("log_factor", real=True) alpha = (x-y)/(x+y) cap = x/factor * ((factor*logarithm-factor+1)/(factor-1) - y*(factor-1)/(x+y)) defect = (factor-1/factor)/2 assert_equal(alpha*x*logarithm/2-cap, x*((defect-logarithm)/(factor-1) + alpha*(logarithm-1+1/factor)/2)) print("PASS seven-arc state transfer, oriented path determinant, and exterior cap identity") if __name__ == "__main__": if not __debug__: raise SystemExit("Run the exact checks with assertions enabled.") margins_and_hexagon() switches_and_disk() bridge_coordinate_change() signed_seven_arc_chart() forward_state_and_cap()