"""Finite exact checks for Factorization of easy Gram determinants.""" from collections import Counter from functools import lru_cache from itertools import combinations from math import comb, prod import sympy as sp from sympy.polys.matrices import DomainMatrix FAMILIES = ("S", "H", "H*", "O", "O*", "B", "O+", "B+", "S+", "H+") def check(actual, expected, description): if actual != expected: raise AssertionError(f"{description}: got {actual}, expected {expected}") def partitions(k): """Generate equality patterns, naming each new block in order of appearance.""" def extend(word, distinct): if len(word) == k: yield word return for label in range(distinct + 1): yield from extend(word + (label,), max(distinct, label + 1)) return extend((), 0) @lru_cache(None) def blocks(word): return tuple(tuple(i for i, a in enumerate(word) if a == label) for label in sorted(set(word))) @lru_cache(None) def noncrossing(word): for a, b, c, d in combinations(range(len(word)), 4): if word[a] == word[c] and word[b] == word[d] and word[a] != word[b]: return False return True def admissible(word, family): if family.endswith("+") and not noncrossing(word): return False parts = blocks(word) if family == "O*": return all(len(b) == 2 and (b[0] - b[1]) % 2 for b in parts) if family in ("O", "O+"): return all(len(b) == 2 for b in parts) if family in ("B", "B+"): return all(len(b) <= 2 for b in parts) if family == "H*": return all(sum(i % 2 for i in b) * 2 == len(b) for b in parts) if family in ("H", "H+"): return all(len(b) % 2 == 0 for b in parts) return True def join_size(left, right): """Connected components of the bipartite graph of the two block sets.""" offset = len(blocks(left)) parent = list(range(offset + len(blocks(right)))) def root(i): while i != parent[i]: parent[i] = parent[parent[i]] i = parent[i] return i for a, b in zip(left, right): parent[root(a)] = root(offset + b) return len({root(i) for i in range(len(parent))}) def gram_det(words, x): if not words: return 1 entries = [[x ** join_size(a, b) for b in words] for a in words] return int(DomainMatrix.from_list(entries, sp.ZZ).det()) def falling(x, b): return prod(x - i for i in range(b)) def insertion_shape(sequence): rows = [] for value in sequence: for row in rows: place = next((i for i, old in enumerate(row) if old > value), None) if place is None: row.append(value) break row[place], value = value, row[place] else: rows.append([value]) return tuple(map(len, rows)) def orthogonal_factor(word, x, half=False): if half: permutation = [0] * (len(word) // 2) for left, right in blocks(word): if left % 2: left, right = right, left permutation[left // 2] = right // 2 shape = insertion_shape(permutation) return prod(x + j - i for i, length in enumerate(shape) for j in range(length)) involution = [0] * len(word) for left, right in blocks(word): involution[left], involution[right] = right, left rows = insertion_shape(involution) columns = [sum(length > j for length in rows) for j in range(max(rows, default=0))] if any(length % 2 for length in columns): raise AssertionError("Odd insertion column for a fixed-point-free involution") return prod(x + 2*j - i for i, length in enumerate(columns) for j in range(length // 2)) def fatten(word): arches = [] for b in blocks(word): arches.append((2*b[0], 2*b[-1] + 1)) arches.extend((2*i + 1, 2*j) for i, j in zip(b, b[1:])) return arches def color(position): # Zero-based indexing for the boundary word abbaabba... return position % 4 in (1, 2) @lru_cache(None) def chebyshev(r, x): if r == 0: return sp.S.One if r == 1: return x return x * chebyshev(r - 1, x) - chebyshev(r - 2, x) def arch_factor(arches, q, colored=False): result = sp.S.One for i, j in arches: ancestors = sorted((a for a, b in arches if a <= i and j <= b), reverse=True) depth = len(ancestors) if colored: depth = next((h for h, a in enumerate(ancestors) if color(a) != color(i)), depth) result *= chebyshev(depth, q) / chebyshev(depth - 1, q) return result def factor(word, x, family): parts = blocks(word) if family in ("S", "H", "H*"): return falling(x, len(parts)) if family in ("O", "O*"): return orthogonal_factor(word, x, half=(family == "O*")) if family in ("B", "B+"): paired = [label for label in word if len(parts[label]) == 2] names = {label: i for i, label in enumerate(dict.fromkeys(paired))} reduced = tuple(names[label] for label in paired) return x ** (len(word) - len(paired)) * factor(reduced, x - 1, family.replace("B", "O")) if family == "O+": return arch_factor(parts, sp.Integer(x)) q = sp.sqrt(x) return q ** (2*len(parts) - len(word)) * arch_factor(fatten(word), q, colored=(family == "H+")) def check_families(): completed = 0 for k in range(1, 7): words = list(partitions(k)) for family in FAMILIES: if k == 6 and family in ("S", "S+"): continue diagrams = [w for w in words if admissible(w, family)] expected = prod(factor(w, 9, family) for w in diagrams) check(gram_det(diagrams, 9), expected, f"{family}, degree {k}") completed += 1 print(f"{completed} full Gram determinants agree at x = 9 (degrees 1 through 6).") def profile(word): arches = fatten(word) return tuple(sum(a <= cut < b for a, b in arches) for cut in range(2*len(word))) def diagram_label(word): return "|".join("".join(str(i + 1) for i in b) for b in blocks(word)) def check_free_hyperoctahedral(): x, q = sp.symbols("x q") # The twelve entries printed in the manuscript. table = { "12|34|56": x**3, "12|36|45": x**2*(x-1), "1456|23": (x-1)**2, "12|3456": x*(x-1), "14|23|56": x**2*(x-1), "16|23|45": x*(x-1)**2, "1234|56": x*(x-1), "1236|45": (x-1)**2, "16|2345": x*(x-2), "123456": (x-1)**2/x, "1256|34": x*(x-2), "16|25|34": x*(x-1)**2, } words = [w for w in partitions(6) if admissible(w, "H+")] check({diagram_label(w) for w in words}, set(table), "Degree-six table coverage") heights = {w: profile(w) for w in words} for w in words: label = diagram_label(w) symbolic = q ** (2*len(blocks(w)) - 6) * arch_factor(fatten(w), q, colored=True) check(sp.cancel(symbolic - table[label].subs(x, q**2)), 0, label) ideal = [v for v in words if all(a <= b for a, b in zip(heights[v], heights[w]))] smaller = [v for v in ideal if v != w] pivot = sp.Rational(gram_det(ideal, 9), gram_det(smaller, 9)) check(pivot, table[label].subs(x, 9), f"Principal-ideal ratio for {label}") print("All 12 degree-six weights agree symbolically, and all 12 Gram minor ratios agree at x = 9.") words = [w for w in partitions(8) if admissible(w, "H+")] check(len(words), 55, "Degree-eight family size") for x in (9, 16): expected = x**55 * (x-1)**86 * (x-2)**20 * (x*x - 3*x + 1)**2 check(prod(factor(w, x, "H+") for w in words), expected, "Degree-eight factor product") check(gram_det(words, x), expected, f"Degree-eight Gram determinant at {x}") print("The 55 by 55 degree-eight Gram determinant agrees at x = 9 and x = 16.") def tree_degrees(word): """Reconstruct the rooted tree, or return None for an inconsistent or open walk.""" edges, degree, vertex = {}, [0], 0 for label in word: if label not in edges: child = len(degree) edges[label] = (vertex, child) degree[vertex] += 1 degree.append(1) vertex = child else: a, b = edges[label] if vertex not in (a, b): return None vertex = b if vertex == a else a return degree if vertex == 0 else None def dyck_paths(n): def extend(path, up, down): if down == n: yield path return if up < n: yield from extend(path + (1,), up + 1, down) if down < up: yield from extend(path + (-1,), up, down + 1) return extend((), 0, 0) def plane_tree_degrees(path): stack, degree = [0], [0] for step in path: if step == 1: degree[stack[-1]] += 1 stack.append(len(degree)) degree.append(1) else: stack.pop() return degree def check_trees(): for k, size in ((4, 3), (6, 12), (8, 57)): words, expected = [], 1 for w in partitions(k): degrees = tree_degrees(w) if degrees is not None: words.append(w) expected *= 7 * prod(falling(6, d - 1) for d in degrees) check(len(words), size, f"Tree-walk count in degree {k}") check(gram_det(words, 7), expected, f"Tree-walk Gram determinant in degree {k}") print("Tree-walk Gram determinants agree in degrees 4, 6, 8 at x = 7 (3, 12, 57 diagrams).") for j in range(1, 5): count = sum(1 for path in dyck_paths(2*j + 1) if sum(d > j for d in plane_tree_degrees(path)) == 2) check(count, 2*j + 1, f"First multiplicity excess at j = {j}") print("The first tree-root multiplicity excess is 3, 5, 7, 9 for j = 1, 2, 3, 4.") def check_marked_arches(): for n in range(1, 6): tally = Counter() for path in dyck_paths(2*n): stack, marks = [], [] for i, step in enumerate(path): if step == 1: stack.append(i) else: start = stack.pop() if color(start) != color(i): break depth = 1 for parent in reversed(stack): if color(parent) != color(i): break depth += 1 marks.append((color(i), depth)) else: tally.update(marks) expected = Counter() for h in range(1, n + 1): k = n - h count = comb(3*n, k) - (comb(3*n, k-1) if k else 0) for c in (False, True): expected[c, h] = count check(tally, expected, f"Marked arches on {2*n} original points") print("The marked-arch counts agree for each color through 10 original points.") if __name__ == "__main__": check_families() check_free_hyperoctahedral() check_trees() check_marked_arches()