"""Exact checks for the reverse-window and same-sign switch comparisons.""" import sympy as sp from sympy.polys.matrices import DomainMatrix def check_zero(expression, description): if sp.cancel(expression) != 0: raise AssertionError(description) print(f"PASS {description}", flush=True) def transport_terms(active, growing, shrinking, q, log_q, root3): """Additive terms F_0, F_+, F_-, G_+, G_- in Lemma B.4.""" a, b, c = active, growing, shrinking half_scale = sp.Rational(1, 2) * root3 * a f_zero = half_scale * ((a + c) * (1 - 1 / q) - (a + b) * (q - 1)) f_grow = half_scale * (a * (q**2 - 1) - (a + b) * (q**2 - q)) f_shrink = half_scale * ((a + c) * (1 / q - 1 / q**2) - a * (1 - 1 / q**2)) g_grow = root3 * a * ((a + b) * q * log_q - 2 * a * (q - 1)) g_shrink = root3 * a / q * (2 * a * (q - 1) - (a + c) * log_q) return f_zero, f_grow, f_shrink, g_grow, g_shrink def transport(u, v, label, margins, q, log_q, root3): """Apply the arc map to the signed U,V coordinates in label order.""" grow, shrink = (label + 1) % 3, (label - 1) % 3 f_zero, f_grow, f_shrink, g_grow, g_shrink = transport_terms(*margins, q, log_q, root3) u_end, v_end = [None] * 3, [None] * 3 u_end[label], v_end[label] = u[label] + f_zero, v[label] - 2 * f_zero for index, factor, f_term, g_term in ( (grow, q, f_grow, g_grow), (shrink, 1 / q, f_shrink, g_shrink), ): u_end[index] = factor**2 * (u[label] + u[index]) + f_term - u_end[label] v_end[index] = factor * (v[label] + v[index]) + g_term - v_end[label] return u_end, v_end def weighted(u, v): return tuple(2 * u_j + v_j for u_j, v_j in zip(u, v, strict=True)) def minimum_obstruction(first, second, unused, q, q_next, log_q, log_next): """The function N in the proof of Lemma C.2.""" eta, eta_next = q - 1 - log_q, q_next - 1 - log_next zeta, zeta_next = q * log_q - q + 1, q_next * log_next - q_next + 1 return ( q * (q - 1) * second * (second * q_next * eta_next - unused * zeta_next) + (q_next - 1) * ( q * second * (unused + q * first) * eta - unused * (q * second + first) * zeta ) ) def check_affine_consequence(residuals, target, coordinates, factors, description): """Synthesize a multiplier identity and verify it at every positive arc factor.""" def affine_row(expression): row = sp.Poly(expression, *coordinates) if row.total_degree() > 1: raise AssertionError(f"Nonaffine residual in {description}") return [row.coeff_monomial(variable) for variable in coordinates] + [row.coeff_monomial(1)] affine_rows = sp.Matrix([affine_row(expression) for expression in (*residuals, target)]) parameters = sorted(affine_rows.free_symbols, key=str) field = sp.QQ.frac_field(*parameters) reduced, pivots = DomainMatrix.from_Matrix(affine_rows.T).convert_to(field).rref() if pivots != tuple(range(len(residuals))): raise AssertionError(f"Affine row-span check failed for {description}") multipliers = reduced.to_Matrix()[:len(residuals), -1] for multiplier in multipliers: _, denominator = sp.fraction(sp.cancel(multiplier)) monomials = sp.Poly(denominator, *factors, domain=sp.QQ) if len(monomials.terms()) != 1: raise AssertionError(f"Unexpected pole in {description}") check_zero(target - sum(c * residual for c, residual in zip(multipliers, residuals, strict=True)), description) def check_reverse_window(): p0, p1, beta_one, q1, q2, log_q1, log_q2, root3 = sp.symbols( "p0 p1 beta_one q1 q2 log_q1 log_q2 root3" ) u1, v1 = sp.symbols("u0:3"), sp.symbols("v0:3") p2, beta_two = (p1 + beta_one) / q1 - p1, (p1 + p0) * q1 - p1 u2, v2 = transport(u1, v1, 2, (p1, p0, beta_one), q1, log_q1, root3) u3, v3 = transport(u2, v2, 1, (p2, p1, beta_two), q2, log_q2, root3) chi1, chi2, chi3 = weighted(u1, v1), weighted(u2, v2), weighted(u3, v3) orthogonality = sum(v_j * pair for v_j, pair in zip( v1, (beta_one + p1, p0 + p1, p0 + beta_one), strict=True )) residuals = (chi1[0], chi1[2], chi2[1], chi3[0], orthogonality) weight = p2 * (q2 - 1) + p1 * (q1 - 1) first = ( beta_two * (q2 - 1) * chi1[1] + q1 * weight * chi2[0] + root3 * p1 * minimum_obstruction(p1, p2, beta_two, q1, q2, log_q1, log_q2) ) second = ( (q1 - 1) * beta_two * chi3[2] + q2 * weight * chi2[0] + root3 * p2 * minimum_obstruction(p2, p1, beta_two, q2, q1, log_q2, log_q1) ) check_affine_consequence(residuals, first, (*u1, *v1), (q1, q2), "first weighted reverse identity") check_affine_consequence(residuals, second, (*u1, *v1), (q1, q2), "second weighted reverse identity") boundary = q1 * p2 + (q1 - 1) * p1 check_zero( (q1 + 2) * q1 * p2 * (boundary + q1 * p1) - (2 * q1 + 1) * boundary * (q1 * p2 + p1) + (q1 - 1) * (q1**2 * p2**2 + 2 * q1 * p1 * (p1 - p2) + p1**2), "reverse minimum sign factorization", ) def check_turn_endpoints(): q = sp.symbols("q", positive=True) beta, u_tie = sp.symbols("beta u_tie") # Divide weighted values and signed costate coordinates by sqrt(3) p[k+1]^2. _, f_grow, _, g_grow, _ = transport_terms(1, beta, 0, q, sp.log(q), 1) _, _, f_shrink, _, g_shrink = transport_terms(1, 0, beta, q, sp.log(q), 1) chi_grow = 2 * q * (q - 1) * u_tie + 2 * f_grow + g_grow chi_shrink = 2 * (q**-2 - q**-1) * u_tie + 2 * f_shrink + g_shrink d_grow = (1 + beta) * q**2 * sp.log(q) - (q - 1) * (2 * q - 1 + beta * q) d_shrink = (1 + beta) * sp.log(q) - (q - 1) - beta * (1 - 1 / q) check_zero( q * (q - 1) * sp.diff(chi_grow, q) - (2 * q - 1) * chi_grow + d_grow, "growing turn endpoint obstruction", ) check_zero( (1 - 1 / q) * sp.diff(chi_shrink, q).subs(q, 1) - q * chi_shrink - d_shrink, "shrinking turn endpoint obstruction", ) def check_pairing_comparison(): a, b, c = sp.symbols("a b c", positive=True) action = 3 * a * b - 4 - (a**2 + b**2) * sp.log(a + b) + ( a**2 * sp.log(a**2 + 4) + b**2 * sp.log(b**2 + 4) ) / 2 action_first = sp.diff(action, a) incoming_bound = (a**2 + a * b + b**2) / (8 * (a + b)**2) outgoing_bound = incoming_bound.subs(b, c) from_stationarity = ( (a + b) * incoming_bound + (a + c) * outgoing_bound - (action_first + action_first.subs(b, c)) / 16 ) / (2 * a) from_margins = ( -1 + 2 * a / (a + b) + 2 * a / (a + c) - a**2 / (a**2 + 4) + sp.log((a + b) * (a + c) / (a**2 + 4)) ) / 16 check_zero(sp.expand_log(from_stationarity - from_margins, force=True), "same-sign comparison") if __name__ == "__main__": check_reverse_window() check_turn_endpoints() check_pairing_comparison()