"""Certify the Taylor obstruction for a moving cubic profile, using SymPy.""" from itertools import combinations from math import prod from sympy import Matrix, QQ, Rational from sympy.polys.rings import ring INDICES = tuple(range(-8, 9)) TRIPLES = tuple(e for e in combinations(INDICES, 3) if sum(e) == 0) POSITIONS = tuple(tuple(INDICES.index(t) for t in e) for e in TRIPLES) N = len(INDICES) R, C, *parameters = ring(['C'] + [f'{p}{j}' for p in 'abc' for j in range(10)], QQ) ZERO = R.zero def add(left, right): return [a + b for a, b in zip(left, right)] def subtract(left, right): return [a - b for a, b in zip(left, right)] def multiply(left, right): return [sum((left[i] * right[k-i] for i in range(k+1)), ZERO) for k in range(len(left))] def multiply_by_s(series): return [ZERO, *series[:-1]] def dz_coeff(triple, t): if t not in triple: return Rational(0) return Rational(1, prod(t-u for u in triple if u != t)) def tangent_matrix(): return Matrix([[int(t in e) for t in INDICES] + [dz_coeff(e, t) for t in INDICES] for e in TRIPLES]) def tangent_kernel(): """Return the ten (horizontal, vertical correction) column pairs.""" zero = [Rational(0)] * N slope = list(map(Rational, INDICES)) columns_by_parameter = {0: zero + [Rational(1)] * N, 1: zero + slope, 8: slope + zero} for d, hcoeff in ((2, -Rational(1, 3)), (3, 0), (4, -Rational(1, 2)), (5, -Rational(1, 3)), (6, -Rational(1, 2)), (7, -Rational(2, 5)), (9, -Rational(3, 7))): columns_by_parameter[d] = [hcoeff*t**(d-2) for t in INDICES] + [t**d for t in INDICES] return Matrix([columns_by_parameter[j] for j in range(10)]).T def obstruction_columns(): rows = [] for e in TRIPLES: h = sum(i*i for i in e) // 2 j = prod(e) rows.append([j**4, h*j**4, j**5, h*h*j**4]) return Matrix(rows) def matrix_times_vec(matrix, vector): return [sum((c*v for c, v in zip(row, vector) if c), ZERO) for row in matrix.tolist()] class TaylorSystem: def __init__(self): self.tangent = tangent_matrix() self.kernel = tangent_kernel() assert self.kernel.rank() == 10 assert self.tangent * self.kernel == Matrix.zeros(len(TRIPLES), 10) self.column_indices = self.tangent.rref()[1] self.rank = len(self.column_indices) assert self.rank == 24 self.columns = self.tangent[:, list(self.column_indices)] followed_by_obstructions = self.columns.row_join(obstruction_columns()) self.row_indices = list(followed_by_obstructions.T.rref()[1]) assert len(self.row_indices) == self.rank + 4 self.inverse = followed_by_obstructions[self.row_indices, :].inv() self.extended = followed_by_obstructions def split(self, target): """Split a Taylor target into the tangent image and four given columns.""" coefficients = matrix_times_vec(self.inverse, [target[j] for j in self.row_indices]) assert matrix_times_vec(self.extended, coefficients) == target correction = [ZERO] * (2*N) for index, c in zip(self.column_indices, coefficients[:self.rank]): correction[index] = c return correction, coefficients[self.rank:] def free_terms(self, order): start = 10*(order-1) return matrix_times_vec(self.kernel, parameters[start:start+10]) def vertical_coordinates(order, horizontal, extra): x2 = multiply(horizontal, horizontal) x3 = multiply(x2, horizontal) return [C*x2[k] + x3[k] + extra[k] for k in range(order+1)] def target(jets, order): """Compute the negative residual at order `order`, with that jet zero.""" x = [[R(t), *(jet[i] for jet in jets), ZERO] for i, t in enumerate(INDICES)] z = [[ZERO, *(jet[N+i] for jet in jets), ZERO] for i in range(N)] y = [vertical_coordinates(order, a, b) for a, b in zip(x, z)] result = [] for triple, (a, b, c) in zip(TRIPLES, POSITIONS): xab, xac, xbc = subtract(x[b], x[a]), subtract(x[c], x[a]), subtract(x[c], x[b]) yab, yac, ybc = subtract(y[b], y[a]), subtract(y[c], y[a]), subtract(y[c], y[b]) determinant = subtract(multiply(xab, yac), multiply(xac, yab)) chord_squares = [add(multiply(dx, dx), multiply_by_s(multiply(dy, dy))) for dx, dy in zip((xab, xac, xbc), (yab, yac, ybc))] rhs = multiply(multiply(chord_squares[0], chord_squares[1]), chord_squares[2]) vandermonde = prod(v-u for u, v in combinations(triple, 2)) residual = multiply(determinant, determinant) assert all(residual[k] == C*C*rhs[k] for k in range(order)) result.append((C*C*rhs[order] - residual[order]).exquo(C) / (2*vandermonde**2)) return result def replace(poly, assignments): return poly.compose(assignments) def main(): system = TaylorSystem() # Take the fixed-horizontal particular solution at order one. first = add([ZERO]*N + [C**3*t**4 + Rational(3, 2)*C*t**6 for t in INDICES], system.free_terms(1)) assert matrix_times_vec(system.tangent, first) == target([], 1) second, obs2 = system.split(target([first], 2)) assert obs2 == [ZERO]*4 second = add(second, system.free_terms(2)) _, obs3 = system.split(target([first, second], 3)) a7, a9 = parameters[7], parameters[9] assert obs3 == [3*C*(-12*C*C*a9 + 2*a7-1)/32, 3*C*a9/8, ZERO, ZERO] assignments = [(a7, R(Rational(1, 2))), (a9, ZERO)] first = [replace(p, assignments) for p in first] second = [replace(p, assignments) for p in second] third, specialized = system.split(target([first, second], 3)) assert specialized == [ZERO]*4 third = add(third, system.free_terms(3)) _, obs4 = system.split(target([first, second, third], 4)) assert obs4[2] == 7*C*C/80 assert obs4[3] == 5*C/32 print('Ranks 24 and 28. Order three forces a7=1/2 and a9=0.') print('Order four has the nonzero coefficients 7*C^2/80 and 5*C/32.') if __name__ == '__main__': main()