"""Finite diamond categories. Vertices are named by the order of edge discovery.""" from bisect import bisect_right from collections import Counter from itertools import combinations, product from functools import lru_cache from math import prod import sympy as sp x = sp.Symbol("x") def tree_walks(length): # A vertex other than the root is named by its incoming edge plus one. parent, children, route, word = [-1], [[]], [0], [] def visit(remaining, v, depth): if not remaining: yield tuple(word), tuple(route[:-1]), tuple(parent) return if depth: word.append(v - 1) route.append(parent[v]) yield from visit(remaining - 1, parent[v], depth - 1) route.pop() word.pop() if depth + 2 <= remaining: for w in children[v]: word.append(w - 1) route.append(w) yield from visit(remaining - 1, w, depth + 1) route.pop() word.pop() w = len(parent) parent.append(v) children.append([]) children[v].append(w) word.append(w - 1) route.append(w) yield from visit(remaining - 1, w, depth + 1) route.pop() word.pop() children[v].pop() children.pop() parent.pop() yield from visit(length, 0, 0) def distance(parent, a, b): length = 0 while a != b: if a < b: a, b = b, a a = parent[a] length += 1 return length def depth(route, parent): visits = [[] for _ in parent] for i, v in enumerate(route): visits[v].append(i) longest = 0 for a, b in combinations((v for v in range(len(parent)) if len(visits[v]) > 1), 2): # The b visits must fall in at least two cyclic gaps between a visits. gaps = {bisect_right(visits[a], i) % len(visits[a]) for i in visits[b]} if len(gaps) > 1: longest = max(longest, distance(parent, a, b)) return longest def profile(parent): if len(parent) == 1: return () degrees = Counter(parent[1:]) # P = x times a product of (x-j), with one factor at v for 1 <= j < deg(v). result = Counter({0: 1}) for v in range(len(parent)): d = degrees[v] + int(v != 0) result.update(range(1, d)) return tuple(result[j] for j in range(max(result)+1)) @lru_cache(None) def weight(profile_): return prod(((x-j)**e for j,e in enumerate(profile_)),start=sp.S.One) def normalize(w): names = {} return tuple(names.setdefault(a, len(names)) for a in w) def finer(a, b): block = {} return all(block.setdefault(i, j) == j for i, j in zip(a, b)) @lru_cache(None) def even_splits(n): def generate(w, counts): if sum(v % 2 for v in counts) > n - len(w): return if len(w) == n: yield w return for i in range(len(counts)): counts[i] += 1 yield from generate(w + (i,), counts) counts[i] -= 1 counts.append(1) yield from generate(w + (len(counts)-1,), counts) counts.pop() return tuple(generate((), [])) def inverse_column(w, profiles): """Mobius column in the full tree-walk poset, via even refinements.""" blocks = [tuple(i for i,v in enumerate(w) if v == a) for a in range(max(w)+1)] candidates = [] for split in product(*(even_splits(len(b)) for b in blocks)): labels = [None]*len(w) for a,(b,sub) in enumerate(zip(blocks, split)): for pos, part in zip(b, sub): labels[pos] = (a, part) v = normalize(labels) if v in profiles: candidates.append(v) support = {} for v in sorted(candidates, key=max): value = 1 if v == w else -sum(c for u,c in support.items() if finer(v,u)) if value: support[v] = value return support def census(k, s): profiles, missing, roots = {}, [], Counter() for w,route,parent in tree_walks(k): prof = profile(parent) profiles[w] = prof if depth(route,parent) > s: missing.append(w) else: for j,e in enumerate(prof): roots[j] += e return profiles, missing, roots def matrix_entry(first, second, second_profile, profiles): coeff = Counter() for diagram, a in first.items(): b = second.get(diagram, 0) if b: coeff[profiles[diagram]] += a*b return sp.factor(sum(c*weight(second_profile)/weight(p) for p,c in coeff.items() if c)) def rotations(word): return [normalize(word[t:]+word[:t]) for t in range(len(word))] def letter_word(word): return "".join(chr(ord("a")+v) for v in word) def check_first_degree(): # In a refinement of p_r each four-block admits only three possible pair # splittings in addition to staying intact. Test tree consistency directly. from verify import tree_degrees for r in range(2, 8): forward = tuple(range(r)) word = (forward + forward[::-1])*2 positions = [tuple(i for i,v in enumerate(word) if v == a) for a in forward] profiles = {} for splits in product(even_splits(4), repeat=r): new = [None]*len(word) for a,(indices, split) in enumerate(zip(positions,splits)): for i,b in zip(indices,split): new[i] = a,b new = normalize(new) degrees = tree_degrees(new) if degrees: exponents = Counter({0:1}) for d in degrees: exponents.update(range(1,d)) profiles[new] = tuple(exponents[j] for j in range(max(exponents)+1)) assert len(profiles) == (r+1)*(r+2)//2 c = inverse_column(word,profiles) assert sorted(c.values()) == [-1,-1,1,1] answer = (x-1)**2/(x-2)**2 if r>=3 else (x*x-3*x+1)/((x-2)*(x-3)) assert sp.cancel(matrix_entry(c,c,profiles[word],profiles)-answer) == 0 print("All tree refinements of p_r and their Mobius corrections agree for r = 2 through 7.") def cyclic_blocks(orbits, profiles, columns, z): out = sp.zeros(len(orbits)) for i, a in enumerate(orbits): for j, b in enumerate(orbits): out[i,j] = sum(z**t*matrix_entry(columns[a[0]],columns[w],profiles[w],profiles) for t,w in enumerate(b)) return out def check_degree_fourteen(profiles, missing, columns): z = sp.Symbol("z") unseen = set(missing) orbits = [] while unseen: orbit = rotations(min(unseen)) assert len(set(orbit)) == 14 unseen.difference_update(orbit) orbits.append(orbit) assert len(orbits) == 5 matrix = cyclic_blocks(orbits, profiles, columns, z) active = list(range(5)) pivots = [] for j in (4,2,3,0,1): active.remove(j) pivot = matrix[j,j] assert not pivot.has(z) pivots.append(pivot) for i in active: for h in active: val = matrix[i,h] - matrix[i,j]*matrix[j,h]/pivot num, den = sp.cancel(val).as_numer_denom() assert not den.has(z) matrix[i,h] = sp.factor(sp.rem(num,z**14-1,z)/den) print(f" {letter_word(orbits[j][0])} {sp.factor(pivot)}") expected = (x-1)**9/((x-2)**8*(x-3)) assert sp.cancel(prod(pivots)-expected) == 0 def check_square_commuting(): from verify import noncrossing p3 = (0,1,2,2,1,0)*2 for k in (6,8,10,12,14): profiles, missing, roots = census(k,2) assert len(profiles)-len(missing) == {6:12,8:57,10:303,12:1744,14:10657}[k] if k <= 10: for w,route,parent in tree_walks(k): assert (depth(route,parent)<=1) == noncrossing(w) if k == 12: assert set(missing) == set(rotations(p3)) roots.update({1:6,2:-6}) if k == 14: cols = {w:inverse_column(w,profiles) for w in missing} check_degree_fourteen(profiles,missing,cols) roots.update({1:126,2:-112,3:-14}) print(f"k = {k}, tree walks = {len(profiles)}, omitted = {len(missing)}, " f"exponents = {[roots[j] for j in sorted(roots)]}") def diagram_factors(k, stage, value=x): """The orthogonal norms at an indeterminate or a dimension greater than k/2.""" profiles, missing, _ = census(k,stage) omitted = set(missing) kept = sorted((w for w in profiles if w not in omitted), key=lambda w:(max(w,default=-1),w)) def tree_weight(w): return weight(profiles[w]).subs(x,value) size = len(missing) K = sp.diag(*(tree_weight(w) for w in missing)) nonzero_rows = [] factors = {} for w in kept: column = sp.zeros(size,1) for j, v in enumerate(missing): if finer(w,v): column[j] = 1 if any(column): for coarser, previous in nonzero_rows: if finer(w, coarser): column -= previous norm = tree_weight(w) if any(column): nonzero_rows.append((w,column)) update = K*column norm = sp.cancel(norm + (column.T*update)[0]) K = (K-update*update.T/norm).applyfunc(sp.cancel) factors[w] = norm return factors def check_norms(): from verify import gram_det assert diagram_factors(0,2) == {():1} and diagram_factors(3,2) == {} for k, stage in ((6,2),(8,1)): factors = diagram_factors(k,stage,7) assert prod(factors.values()) == gram_det(list(factors),7) profiles, _, _ = census(12,2) factors = diagram_factors(12,2,9) ratios = Counter(sp.Rational(v,weight(profiles[w]).subs(x,9)) for w,v in factors.items()) assert ratios == {1:1735, sp.Rational(8,7):3, sp.Rational(9,8):3, sp.Rational(64,63):3} print("The norm recursion agrees with direct small determinants and isolates nine corrected rows at degree 12.") def check_coarsenings(): for k in (8,10): walks = list(tree_walks(k)) depths = {w:depth(route,parent) for w,route,parent in walks} for word,route,parent in walks: dist = [[distance(parent,a,b) for b in range(len(parent))] for a in range(len(parent))] largest = 0 for i,j,l,m in combinations(range(k),4): if (l-i)%2 or (m-j)%2: continue gap = min(dist[route[a]][route[b]] for a in (i,l) for b in (j,m)) largest = max(largest,gap) assert largest == max(h for w,h in depths.items() if finer(word,w)) print("The four-time distance test agrees with every tree coarsening in degrees 8 and 10.") if __name__ == "__main__": check_first_degree() check_square_commuting() check_coarsenings() check_norms()