#!/usr/bin/env python3 """Check the identities for the regular and six-arc mixed examples.""" import sympy as sp def equal(left, right): difference = left - right entries = difference if isinstance(difference, sp.MatrixBase) else (difference,) for entry in entries: assert sp.simplify(entry) == 0, entry def main(): q, q_reverse, q_turn = sp.symbols("q q_reverse q_turn", positive=True) rotation = sp.Matrix([[0, 1], [-1, 1]]) def transfer(factor, sign): return sp.diag(factor, 1/factor)*rotation**(-sign) half = transfer(q_reverse, -1)*transfer(q_turn, 1)*transfer(q_turn, -1) equal(sp.trace(half), (2 - q_turn**2)/q_reverse) equal(half.subs(q_turn, sp.sqrt(2))**2, -sp.eye(2)) equal(-rotation**(-2), rotation) v = sp.Symbol("v", positive=True) a = sp.sqrt(v) ratio = 1 + sp.sqrt(2) b = ratio*a cap = 4/((1 + ratio)**2 - 1) equal(2*b/(a+b), sp.sqrt(2)) factor = (v+4)/((1+ratio)**2*v) equal(factor.subs(v, cap), 1) # Positive log arguments permit these replacements in the area kernel. log_sum, log_square_b = sp.symbols("log_sum log_square_b", real=True) log_square_a = sp.log(factor) + 2*log_sum log_twice_b = log_sum + sp.log(2)/2 mixed_edge = 3*a*b-4-(v+b**2)*log_sum + (v*log_square_a+b**2*log_square_b)/2 forward_edge = 3*b**2-4-2*b**2*log_twice_b+b**2*log_square_b area = sp.sqrt(3)/2*(4+v*(3-ratio**2*sp.log(2)-sp.log(factor))) equal(sp.sqrt(3)/4*(2*forward_edge-4*mixed_edge), area) equal(sp.diff(area, v), sp.sqrt(3)/2*(3-ratio**2*sp.log(2)-sp.log(factor)+4/(v+4))) octagon = sp.sqrt(12)*(8-4*sp.sqrt(2)-sp.log(2))/(2*sp.sqrt(2)-1) equal(area.subs(v, cap), octagon) assert sp.simplify(ratio**2 - sp.Rational(29,5)).is_positive # The first two terms of 2*arctanh(1/3) give the strict lower bound. equal(2*(sp.Rational(1,3)+sp.Rational(1,3)**3/3), sp.Rational(56,81)) assert sp.Rational(29,5)*sp.Rational(56,81) > 4 print("PASS six-arc mixed transfer, area and strict limit comparison") equal(sp.trace(transfer(q, 1)), q) per_arc = sp.sqrt(3)*((3*q-q*sp.log(q))/(4-q)-1) equal(per_arc.subs(q, 1), 0) equal(sp.diff(per_arc, q).subs(q, 1), sp.sqrt(3)) angle_step = sp.Symbol("angle_step") regular_factor = 2*sp.cos(sp.pi/3-angle_step/3) equal(sp.diff(regular_factor, angle_step).subs(angle_step, 0), sp.sqrt(3)/3) switch_value = (2+q/4-sp.log(q))/(8*(4-q)) determinant = (16/(4-q))**2*switch_value**2*(1-q*q/4) equal(determinant.subs(q, 1), sp.Rational(3,16)) print("PASS regular-arc trace and exact area and determinant limits") if __name__ == "__main__": if not __debug__: raise SystemExit("Run the exact checks with assertions enabled.") main()