Path: blob/main/GeneratingDiagonalsViaShift/diagonals_via_shift.py
1017 views
'''1A quick script for generating diagonals using the shift method of SU from2"Comparing Diagonals on the Associahedra"345Use the two natural shift pairs6- (L,R) i.e the SU original7- (L',R') a symmetry of the prior pair, and we now know these are LA diagonals.8'''9from itertools import permutations, chain, combinations, product10from copy import deepcopy11import sys1213def powerset(iterable):14'''15https://stackoverflow.com/questions/1482308/how-to-get-all-subsets-of-a-set-powerset16returns a generator for the powerset17'''18s = list(iterable)19return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))2021def vertex_generator(n):22'''23returns a generator (iterable) for the permutations of size n,24which correspond to vertices.25'''26return permutations(range(1,n+1))2728def strong_complimentary_pair(v):29'''30returns the strong complimentary pair of a given vertex31'''32merge_desc = [[v[0]]]33merge_asc = [[v[0]]]3435prev = v[0]36for x in v[1:]:37if x>prev:38merge_asc[-1].append(x)39merge_desc.append([x])40if x<prev:41merge_desc[-1].append(x)42merge_asc.append([x])43prev = x4445#Sort the subpartitions so in standard form46merge_asc = [sorted(x) for x in merge_asc]47merge_desc = [sorted(x) for x in merge_desc]4849return merge_desc, merge_asc5051def R_shifts(lcp,j=0):52'''53returns all possibe R shifts of the left elem of complementary pair when j=054'''55shifts = [lcp]5657if j == len(lcp)-1:58#no right shift possible59return shifts6061feasible_Ms = powerset(lcp[j][1:])6263for M in feasible_Ms:64if len(M)==0:65#If M is emptyset go to next.66shifts = R_shifts(lcp,j+1)67#min M_j > max A_{j+1} then perform shift68elif M[0]>lcp[j+1][-1]:69shift = deepcopy(lcp) #copy the contents not the pointer70shift[j] = list(set(lcp[j]) - set(M))71shift[j+1] = sorted(shift[j+1]+list(M))7273#Recurse74shifts = shifts + R_shifts(shift,j+1)7576return shifts777879def L_shifts(rcp,j=None):80'''81returns all possibe L shifts of the right elem of complementary pair8283We use the indexing B_1...B_q (instead of Bq...B1)84Also refer to Ns as Ms85'''86shifts = [rcp]87if j is None:88j = len(rcp)-189if j==0:90#No left shift possible91return shifts9293feasible_Ms = powerset(rcp[j][1:])9495for M in feasible_Ms:96if len(M)==0:97#If M is emptyset go to next.98shifts = L_shifts(rcp,j-1)99#min M_j > max A_{j-1} then perform shift100elif M[0]>rcp[j-1][-1]:101shift = deepcopy(rcp) #copy the contents not the pointer102shift[j] = list(set(rcp[j]) - set(M))103shift[j-1] = sorted(shift[j-1]+list(M))104105#Recurse106shifts = shifts + L_shifts(shift,j-1)107108return shifts109110def LR_shift_of_v(v):111'''112returns A_v x B_v113'''114lcp, rcp = strong_complimentary_pair(v)115pairs = []116for l in R_shifts(lcp):117for r in L_shifts(rcp):118pairs.append([l,r])119120return pairs121122def diagonal_via_LR_shift(n):123pairs = []124for v in vertex_generator(n):125pairs = pairs + LR_shift_of_v(v)126return pairs127128129def R_prime_shifts(lcp,j=None):130'''131returns all possibe R shifts of the left elem of complementary pair when j=0132'''133shifts = [lcp]134135if j is None:136j = len(lcp)-1137138if j == 0:139#no right prime shift possible140return shifts141142feasible_Ms = powerset(lcp[j][:-1])143144for M in feasible_Ms:145if len(M)==0:146#If M is emptyset go to next.147shifts = R_prime_shifts(lcp,j-1)148#max M_j < min A_{j-1} then perform shift149elif M[-1]<lcp[j-1][0]:150shift = deepcopy(lcp) #copy the contents not the pointer151shift[j] = list(set(lcp[j]) - set(M))152shift[j-1] = sorted(shift[j-1]+list(M))153154#Recurse155shifts = shifts + R_prime_shifts(shift,j-1)156157return shifts158159160def L_prime_shifts(rcp,j=None):161'''162returns all possibe L shifts of the right elem of complementary pair163164We use the indexing B_1...B_q (instead of Bq...B1)165Also refer to Ns as Ms166'''167shifts = [rcp]168if j is None:169j = 0170if j==len(rcp)-1:171#No left shift possible172return shifts173174feasible_Ms = powerset(rcp[j][:-1])175176for M in feasible_Ms:177if len(M)==0:178shifts = L_prime_shifts(rcp,j+1)179#max M_j < min A_{j+1} then perform shift180elif M[-1]<rcp[j+1][0]:181shift = deepcopy(rcp) #copy the contents not the pointer182shift[j] = list(set(rcp[j]) - set(M))183shift[j+1] = sorted(shift[j+1]+list(M))184185#Recurse186shifts = shifts + L_prime_shifts(shift,j+1)187188return shifts189190def LR_prime_shift_of_v(v):191'''192returns A_v x B_v193'''194lcp,rcp = strong_complimentary_pair(v)195pairs = []196for l in R_prime_shifts(lcp):197for r in L_prime_shifts(rcp):198pairs.append([l,r])199200return pairs201202def diagonal_via_LR_prime_shift(n):203pairs = []204for v in vertex_generator(n):205pairs = pairs + LR_prime_shift_of_v(v)206return pairs207208def SU_diag(n):209return diagonal_via_LR_shift(n)210211def LA_diag(n):212return diagonal_via_LR_prime_shift(n)213214if __name__ == "__main__":215v = list(vertex_generator(3))[3]216print(v)217lcp,rcp = strong_complimentary_pair(v)218print(lcp,rcp)219print(R_prime_shifts([[2],[1,3,4]]))220print()221222print(diagonal_via_LR_shift(3))223print()224print(diagonal_via_LR_prime_shift(3))225226for k in range(1,7):227n1 = len(diagonal_via_LR_shift(k))228n2 = len(diagonal_via_LR_prime_shift(k))229print(n1,n2)230231232#Printing out lists for comparison.233for k in range(1,7):234s = "./Examples/Shift/n={}.txt".format(k)235with open(s, 'w') as f:236sys.stdout = f # Change the standard output to file.237print(diagonal_via_LR_shift(k))238239s = "./Examples/PrimeShift/n={}.txt".format(k)240with open(s, 'w') as f:241sys.stdout = f # Change the standard output to file.242print(diagonal_via_LR_prime_shift(k))243244