"""One-diagram factors for four turns and exact checks for the next exceptional degree.""" from collections import Counter, defaultdict from functools import lru_cache from itertools import combinations from math import prod import sympy as sp from diamond import depth, distance, finer, inverse_column, matrix_entry, normalize, profile from diamond import rotations, tree_walks, weight, x from next_degree import TreeProfiles def turning_times(word): return tuple(i for i,a in enumerate(word) if a == word[i-1]) def leg_lengths(times, k): return tuple((b-a)%k for a,b in zip(times,times[1:]+times[:1])) def quartet(word, route, parent): times = turning_times(word) if len(times) != 4: raise ValueError("A quartet needs exactly four turns.") sides = leg_lengths(times,len(word)) u,v,w,z = sides if u+w != v+z: return sides, None a,b,c,d = [route[t] for t in times] i = (distance(parent,a,c)-abs(u-v))//2 j = (distance(parent,b,d)-abs(u-z))//2 m = min(sides)-i-j return sides,(i,j,m) def four_turn_factor(word, route, parent, stage): sides, geometry = quartet(word,route,parent) P = weight(profile(parent)) if geometry is None or min(sides) <= stage: return P i,j,m = geometry if m > stage: raise ValueError("Diagram omitted at this stage.") if (m == stage and i > 0 and j > 0) or (m == stage-1 and j > 0): return sp.cancel(P*(x-1)/(x-2)) return P def join_word(a,b): # When all equalities of b are imposed on the block labels of a. root = list(range(max(a)+1)) first = {} def find(i): while root[i] != i: i = root[i] return i for i,j in zip(a,b): root[find(i)] = find(first.setdefault(j,i)) return normalize(tuple(find(i) for i in a)) @lru_cache(None) def chromatic(word, gaps, value): # A proper coloring of the join graph. Adding each vertex to a color # class or opening a new class counts colorings without enumerating N^k. edges = [tuple(sorted((word[i-1],word[i]))) for i in gaps] if any(i==j for i,j in edges): return 0 size = max(word)+1 earlier = [set() for _ in range(size)] for i,j in edges: earlier[j].add(i) labels = [] def visit(i, used): if i==size: return 1 forbidden = {labels[h] for h in earlier[i]} total = 0 for c in range(used+1): if c in forbidden or c==value: continue labels.append(c) total += (value-used if c==used else 1)*visit(i+1,max(used,c+1)) labels.pop() return total return visit(0,0) def test_quartet_sector(rows,stage,times,sides,value): k=sum(sides) ordered=sorted(rows) indices={pair:t for t,pair in enumerate(ordered)} words=[rows[pair][0] for pair in ordered] gaps=tuple(i for i in range(k) if i not in times) gram=sp.Matrix([[chromatic(join_word(a,b),gaps,value) for b in words] for a in words]) transform=sp.eye(len(words)) cutoff=min(sides)-stage norms=[] for p,(i,j) in enumerate(ordered): if i+j==cutoff: transform[p,indices[i,j+1]]= -sp.Rational(1,value-1) elif i+j==cutoff+1: if i: transform[p,indices[i-1,j]]= -1 else: for pair,sign in (((i-1,j),-1),((i,j-1),-1),((i-1,j-1),1)): if pair in indices: transform[p,indices[pair]]=sign word,route,parent=rows[i,j] norms.append(four_turn_factor(word,route,parent,stage).subs(x,value)) assert transform*gram*transform.T == sp.diag(*norms) return len(words) def check_quartets(): counts = Counter() checked = set() for k in (8,10,12,14,16): sectors = defaultdict(dict) for word,route,parent in tree_walks(k): turns=turning_times(word) if len(turns)!=4: continue sides,geo=quartet(word,route,parent) assert depth(route,parent)==(geo[2] if geo else 0) if geo is None:continue i,j,m=geo for stage in range(2,min(sides)): if m<=stage: sectors[turns,sides,stage][i,j]=(word,route,parent) for (turns,sides,stage),rows in sectors.items(): key=(sides,stage) if key not in checked: counts[k]+=test_quartet_sector(rows,stage,turns,sides,9) checked.add(key) print("Four-turn weights diagonalize the projected join graphs at x=9;", len(checked),"ordered length/stage cases,", sum(counts.values()),"individual diagonal entries through degree 16.") class SameTurns(TreeProfiles): def __init__(self,turns): super().__init__() self.turns=turns def __contains__(self,word): return turning_times(word)==self.turns and super().__contains__(word) def check_second_sector(r): path=tuple(range(r)) basic=(path+path[::-1])*2 reps=set() for i,a in enumerate(basic): for insert in ((a,a),(r,r)): reps.add(min(rotations(basic[:i]+insert+basic[i:]))) missing={w for rep in reps for w in rotations(rep)} by_turn=defaultdict(list) for w in missing: by_turn[turning_times(w)].append(w) k=4*r+2 assert Counter(map(len,by_turn.values()))=={1:k*(2*r-3),2:k} checked=set() for turns,words in by_turn.items(): key=min(min(rotations(w)) for w in words) if key in checked: continue checked.add(key) profiles=SameTurns(turns) cols=[inverse_column(w,profiles) for w in words] matrix=sp.Matrix([[matrix_entry(c,col,profiles[w],profiles) for w,col in zip(words,cols)] for c in cols]) a=x-2 expected=(sp.Matrix([[(x-1)**2/a**2]]) if len(words)==1 else (x-1)/(x-3)*sp.Matrix([[1,1/a],[1/a,1]])) assert (matrix-expected).applyfunc(sp.cancel)==sp.zeros(len(words)) print(f"r={r}: {k*(2*r-3)} singleton turn classes and {k} paired classes agree symbolically.") if __name__=="__main__": check_quartets() for r in range(3,7): check_second_sector(r)