"""Monomials in ternary form coefficients, at fixed degree and weight.""" from functools import lru_cache import numpy as np def monomial_basis(a, k, y, t): terms = [(i, j) for i in range(min(y, k) + 1) for j in range(min(t, k - i) + 1)] @lru_cache(None) def count(start, slots, u, v): if slots == 0: return int(u == 0 and v == 0) total = 0 for pos in range(start, len(terms)): i, j = terms[pos] if i <= u and j <= v: total += count(pos, slots - 1, u - i, v - j) return total columns = np.empty((a, count(0, a, y, t)), dtype=np.int32) path = [] index = 0 def visit(start, slots, u, v): nonlocal index if slots == 0: columns[:, index] = path index += 1 return for pos in range(start, len(terms)): i, j = terms[pos] if i <= u and j <= v and count(pos, slots - 1, u - i, v - j): path.append(pos) visit(pos, slots - 1, u - i, v - j) path.pop() visit(0, a, y, t) return terms, columns def swap_orbits(terms, columns, k, swap): """One monomial and its partner per orbit of the indicated coordinate swap.""" positions = {term: i for i, term in enumerate(terms)} perm = [] for y, t in terms: partner = (k - y - t, t) if swap == 'xy' else (t, y) perm.append(positions.get(partner, -1)) others = np.sort(np.asarray(perm, dtype=np.int32)[columns], axis=0) if np.any(others < 0): raise ValueError('weight is not closed under the coordinate swap') equal = np.ones(columns.shape[1], dtype=bool) keep = np.zeros_like(equal) for row, other in zip(columns, others): keep |= equal & (row < other) equal &= row == other keep |= equal return np.ascontiguousarray(columns[:, keep]), np.ascontiguousarray(others[:, keep])