# partitions.py # Basic tools: set partitions, kernels of multi-indices, the usual maps T_pi, # and the signature of the partitions having even blocks, computed from its # definition, as the parity of the number of switches needed for making the # partition noncrossing. import itertools from collections import deque import numpy as np def set_partitions(n): # all partitions of {0,...,n-1}, as tuples of block labels, in restricted growth form def rec(prefix, m): if len(prefix) == n: yield tuple(prefix) return for b in range(m + 1): yield from rec(prefix + [b], max(m, b + 1)) yield from rec([], 0) def kernel(word): # kernel of a multi-index, in restricted growth form seen = {} return tuple(seen.setdefault(x, len(seen)) for x in word) def blocks(p): d = {} for pos, b in enumerate(p): d.setdefault(b, []).append(pos) return list(d.values()) def is_even(p): return all(len(b) % 2 == 0 for b in blocks(p)) def is_pairing(p): return all(len(b) == 2 for b in blocks(p)) def is_noncrossing(p): # p is a tuple of labels, the points being on a line (or on a circle) n = len(p) for a in range(n): for b in range(a + 1, n): for c in range(b + 1, n): for d in range(c + 1, n): if p[a] == p[c] and p[b] == p[d] and p[a] != p[b]: return False return True def coarser(tau, pi): # tau is obtained from pi by merging blocks m = {} for t, q in zip(tau, pi): if m.setdefault(q, t) != t: return False return True def signature(tau, k): # tau is a partition of k upper points and l lower points, given by labels # (upper row from left to right, then lower row from left to right). # A switch exchanges 2 neighbors of the same row, belonging to different blocks. # We return (-1)^c, with c being the minimal number of switches needed for # making tau noncrossing, found by breadth first search. def circular(state): return state[:k] + state[k:][::-1] start = tuple(tau) seen = {start: 0} queue = deque([start]) while queue: s = queue.popleft() if is_noncrossing(circular(s)): return (-1) ** seen[s] for row in (range(0, k - 1), range(k, len(s) - 1)): for a in row: if s[a] != s[a + 1]: t = list(s) t[a], t[a + 1] = t[a + 1], t[a] t = tuple(t) if t not in seen: seen[t] = seen[s] + 1 queue.append(t) raise ValueError('no noncrossing form found') def T(pi, k, N): # the usual linear map T_pi, from k tensors to l tensors, for a partition pi of k+l points l = len(pi) - k M = np.zeros((N ** l, N ** k)) for idx in itertools.product(range(N), repeat=k + l): if coarser(kernel(idx), pi): i, j = idx[:k], idx[k:] M[number(j, N), number(i, N)] = 1 return M def number(word, N): x = 0 for a in word: x = x * N + a return x