"""Compute mixed invariant derivatives on Sym^3(y²=x⁷−1) by the chain rule. The evaluator uses the Abel–Jacobi vector fields over Z/p² and no Cartier or Frobenius shortcut. The predictions are used only after evaluation. """ from math import comb, factorial class TruncatedPolynomials: """Coefficients in (Z/m)[s]/s^width, packed for exact convolution.""" def __init__(self, modulus, width, max_order): self.m = modulus self.width = width # A Leibniz sum has at most (max_order+1)*width terms per slot. # Binomial coefficients are reduced modulo m before multiplication. self.bits = ((max_order + 1) * width * modulus**3 + 1).bit_length() self.mask = (1 << self.bits) - 1 def pack(self, values): out = 0 for c in reversed(values): out = (out << self.bits) | (c % self.m) return out def reduce(self, raw): if self.width == 1: return raw % self.m terms = [] for _ in range(self.width): terms.append((raw & self.mask) % self.m) raw >>= self.bits return self.pack(terms) def coefficient(self, value, exponent): return (value >> (self.bits * exponent)) & self.mask def evaluate_flow(direction, observed, initial, order, ring, form=4): """Return ordinary iterated derivatives of each variable and the observable.""" m = ring.m # Entries 0..8 are the x_i, y_i and the three reciprocal differences. instructions = [] series = [[v] for v in initial] def node(op, left, right): instructions.append((op, left, right)) series.append([]) return len(series) - 1 def add(a, b): return node('add', a, b) def mul(a, b): return node('mul', a, b) def scale(k, a): return node('scale', a, k % m) def sub(a, b): return add(a, scale(-1, b)) x = [0, 1, 2] y = [3, 4, 5] pairs = [(0, 1), (0, 2), (1, 2)] reciprocal = dict(zip(pairs, [6, 7, 8])) for i, j in pairs: reciprocal[j, i] = scale(-1, reciprocal[i, j]) dx, dy = [], [] for i in range(3): j, k = [a for a in range(3) if a != i] cofactor = mul(x[j], x[k]) if direction == 1 else scale(-1, add(x[j], x[k])) weight = mul(cofactor, mul(reciprocal[i, j], reciprocal[i, k])) dx.append(mul(y[i], weight)) cube = mul(mul(x[i], x[i]), x[i]) dy.append(scale(7 * pow(2, -1, m), mul(mul(cube, cube), weight))) dr = [scale(-1, mul(mul(reciprocal[i, j], reciprocal[i, j]), sub(dx[i], dx[j]))) for i, j in pairs] rhs = dx + dy + dr e1 = add(add(x[0], x[1]), x[2]) e2 = add(add(mul(x[0], x[1]), mul(x[1], x[2])), mul(x[0], x[2])) e3 = mul(mul(x[0], x[1]), x[2]) # Coefficients of X^(form-1) modulo the divisor polynomial. if form == 4: function = e3 if observed == 1 else scale(-1, e2) elif form == 5: function = mul(e1, e3) if observed == 1 else sub(e3, mul(e1, e2)) else: raise ValueError(form) binomials = [1] for n in range(order + 1): weights = [(i, weight) for i, weight in enumerate(binomials) if weight] for index, (op, left, right) in enumerate(instructions, start=9): lhs = series[left] if op == 'add': raw = lhs[n] + series[right][n] elif op == 'scale': raw = lhs[n] * right else: other = series[right] raw = sum(weight * lhs[i] * other[n-i] for i, weight in weights) series[index].append(ring.reduce(raw)) if n < order: for i, derivative in enumerate(rhs): series[i].append(series[derivative][n]) binomials = [1] + [(a + b) % m for a, b in zip(binomials, binomials[1:])] + [1] return series[:9], series[function] def mixed_values(p, points, a, b, observed, orders, precision=2, form=4): m = p**precision xs = [x for x, y in points] ys = [y for x, y in points] assert all((y*y - x**7 + 1) % m == 0 for x, y in points) initial = xs + ys + [pow(xs[i]-xs[j], -1, m) for i, j in ((0, 1), (0, 2), (1, 2))] max_a = max(r for r, s in orders) max_b = max(s for r, s in orders) assert max_b < p # For the inner direction ordinary Taylor coefficients to degree