#!/usr/bin/env python3 """Check algebra in the reduced maximum principle and the elliptic area bound.""" import sympy as sp def commutator(left, right): return left * right - right * left 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 traceless(prefix): a, b, c = sp.symbols(f"{prefix}a {prefix}b {prefix}c", real=True) return sp.Matrix([[a, b], [c, -a]]) def reduced_costate(): x, y = sp.symbols("x y", real=True, nonzero=True) state = sp.Matrix([[x, -x**2 - y**2], [1, -x]]) / y adjoint = traceless("L") assert_equal( commutator(state, commutator(state, adjoint)), -4 * adjoint - 2 * sp.trace(state * adjoint) * state, ) side = 1 / sp.sqrt(3) vertices = ( sp.Matrix([[0, sp.Rational(1, 3)], [1, 0]]), sp.Matrix([[side, -sp.Rational(2, 3)], [0, -side]]), sp.Matrix([[-side, -sp.Rational(2, 3)], [0, side]]), ) weights = (1 / y, 3 * ((x + side)**2 + y**2) / (4*y), 3 * ((x - side)**2 + y**2) / (4*y)) combination = sum((weight * vertex for weight, vertex in zip(weights, vertices)), sp.zeros(2)) assert_equal(combination, state) control, area_term, direction = traceless("P"), traceless("G"), traceless("Y") infinitesimal = sp.Symbol("s") moving_control = control / (1 + infinitesimal * sp.trace(direction * control)) moving_state = state + infinitesimal * direction field_derivative = commutator(moving_control, moving_state).diff(infinitesimal).subs(infinitesimal, 0) expected_field_derivative = commutator(control, direction) - sp.trace(control * direction) * commutator(control, state) assert_equal(field_derivative, expected_field_derivative) reduced = -commutator(state, adjoint) scalar = sp.trace(reduced * control) assert_equal(sp.trace(adjoint * commutator(control, state)), -scalar) adjoint_derivative = commutator(control, adjoint) - scalar * control - area_term reduced_derivative = -commutator(commutator(control, state), adjoint) - commutator(state, adjoint_derivative) expected_reduced_derivative = ( commutator(control, reduced) - scalar * commutator(control, state) - commutator(area_term, state) ) assert_equal(reduced_derivative, expected_reduced_derivative) print("PASS reduced PMP, orbit costate representation, and Hamiltonian test control") def polar_bound(): scale = sp.Symbol("lambda", positive=True) a, r, b = sp.symbols("a r b", real=True) state = sp.Matrix([[a, -r-b], [r-b, -a]]) frame = sp.diag(scale, 1/scale) rotation = sp.Matrix([[0, -1], [1, 0]]) polar_real = sp.trace(frame) / 2 derivative = frame * state polar_imag_derivative = (derivative[1, 0] - derivative[0, 1]) / 2 r_frame = -sp.trace(rotation * frame * state * frame.inv()) / 2 shear = (scale**2 - scale**-2) / 2 assert_equal(polar_imag_derivative / polar_real, (r + r_frame) / (2 * polar_real**2)) assert_equal(4 * polar_real**2 * r * r_frame - (r+r_frame)**2, shear**2 * (r**2-b**2)) assert_equal(state.det(), r**2 - b**2 - a**2) print("PASS polar angular speed and determinant-one area inequality") def collapsing_forward_arc(): factor = sp.Symbol("h", positive=True) logarithm = sp.log(factor) # Specialize the forward stationarity row to nu=3, z=w=q=1. central_row = sp.Rational(9, 4) neighbor_row = 2*factor + sp.Rational(3, 4) - 1/(2*factor) - logarithm ratio = 2*factor - 1 weight = 2*(factor-1) left_norm_floor = 1 + factor/4 - factor*logarithm/(2*(factor-1)) residual = ratio*central_row - neighbor_row - weight*(left_norm_floor + sp.Rational(3, 4)) defect = (factor - 1/factor)/2 - logarithm assert_equal(residual, -(factor+1)*defect) assert_equal(defect.diff(factor), (factor-1)**2/(2*factor**2)) print("PASS limiting forward stationarity residual and its strict sign for h>1") if __name__ == "__main__": if not __debug__: raise SystemExit("Run the exact checks with assertions enabled.") reduced_costate() polar_bound() collapsing_forward_arc()