from viewLatex import viewLatex

def contact_vector(dw):
x = 0
y = 0
vector = [dw.number_of_touch_points()]
for i,step in enumerate(dw):
if step == 1:
y += 1
d = y - x
c = 0
xx = x
yy = y
for k in xrange(i+1,len(dw)):
dd = yy - xx
if dd == d:
c+=1
elif dd < d:
break
xx, yy = (xx+1, yy) if dw[k] == 0 else (xx, yy+1)
c -= 1
vector.append(c)

else:
x += 1
return vector[:-1]

def rise_vector(dw):
vector = []
r = 0
for step in dw:
if step == 0:
vector.append(r)
r = 0
else:
r+=1
return vector


# The Rise contact involution on Tamari Intervals¶

Viviane Pons -- July, 24th 2018 -- ICERM

tip = TamariIntervalPoset(10, [(2,5), (3,4), (4,5), (6,7), (8,10), (9,10), (3,2), (4,2), (6,5), (7,5), (8,5), (10,5), (9,8)])
viewLatex(tip)


## The Tamari lattice¶

P = Poset({dw: [bt.to_dyck_word_tamari() for bt in dw.to_binary_tree_tamari().tamari_succ()] for dw in DyckWords(3)})
viewLatex(P)


## Some statistics on Dyck Paths¶

dw = DyckWord([1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,0,0,0,0])
viewLatex(dw)

print (dw.number_of_touch_points())
print (dw.number_of_initial_rises())

2 2
print (contact_vector(dw))
print (rise_vector(dw))

[2, 2, 0, 1, 0, 2, 0, 2, 0, 1] [2, 2, 0, 0, 2, 2, 2, 0, 0, 0]
n = 3
sorted([contact_vector(dw) for dw in DyckWords(n)]) == sorted([rise_vector(dw) for dw in DyckWords(n)])

True
n = 3
sorted([[contact_vector(dw), rise_vector(dw)] for dw in DyckWords(n)]) == \
sorted([[rise_vector(dw), contact_vector(dw)] for dw in DyckWords(n)])

False
def contact_partition(dw):
v = contact_vector(dw)
v.sort()
v.reverse()
return v

def rise_partition(dw):
v = rise_vector(dw)
v.sort()
v.reverse()
return v

dw = DyckWord([1,1,0,1,1,0,0,0,1,1,0,1,1,0,1,1,0,0,0,0])
print (contact_vector(dw))
print (contact_partition(dw))

[2, 2, 0, 1, 0, 2, 0, 2, 0, 1] [2, 2, 2, 2, 1, 1, 0, 0, 0, 0]
n = 3
sorted([[contact_partition(dw), rise_monomial(dw)] for dw in DyckWords(n)]) == \
sorted([[rise_monomial(dw), contact_partition(dw)] for dw in DyckWords(n)])

True
def involution(dw):
return dw.reverse().to_binary_tree_tamari().left_right_symmetry().to_dyck_word_tamari().reverse()

n = 3
all(contact_partition(dw) == rise_partition(involution(dw)) for dw in DyckWords(n))

True

## Statistics on Tamari intervals¶

TIP3 = TamariIntervalPosets(3)
print TIP3.cardinality()
[(tip.lower_dyck_word(), tip.upper_dyck_word()) for tip in TIP3]

13
[([1, 0, 1, 0, 1, 0], [1, 1, 1, 0, 0, 0]), ([1, 0, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0]), ([1, 0, 1, 0, 1, 0], [1, 1, 0, 1, 0, 0]), ([1, 0, 1, 0, 1, 0], [1, 1, 0, 0, 1, 0]), ([1, 1, 0, 0, 1, 0], [1, 1, 1, 0, 0, 0]), ([1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0]), ([1, 1, 0, 1, 0, 0], [1, 1, 1, 0, 0, 0]), ([1, 1, 0, 0, 1, 0], [1, 1, 0, 1, 0, 0]), ([1, 1, 0, 1, 0, 0], [1, 1, 0, 1, 0, 0]), ([1, 1, 0, 0, 1, 0], [1, 1, 0, 0, 1, 0]), ([1, 0, 1, 0, 1, 0], [1, 0, 1, 1, 0, 0]), ([1, 0, 1, 1, 0, 0], [1, 0, 1, 1, 0, 0]), ([1, 0, 1, 0, 1, 0], [1, 0, 1, 0, 1, 0])]
def contacts_interval(tip):
return tip.lower_dyck_word().number_of_touch_points()

def rises_interval(tip):
return tip.upper_dyck_word().number_of_initial_rises()

def contact_vector_interval(tip):
return contact_vector(tip.lower_dyck_word())

def rise_vector_interval(tip):
return rise_vector(tip.upper_dyck_word())

def contact_partition_interval(tip):
return contact_monomial(tip.lower_dyck_word())

def rise_partition_interval(tip):
return rise_monomial(tip.upper_dyck_word())

tip = TamariIntervalPoset(10, [(2,5), (3,4), (4,5), (6,7), (8,10), (9,10), (3,2), (4,2), (6,5), (7,5), (8,5), (10,5), (9,8)])

tip.lower_dyck_word()

[1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0]
viewLatex(tip.lower_dyck_word())

tip.upper_dyck_word()

[1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0]
viewLatex(tip.upper_dyck_word())

contact_vector_interval(tip)

[3, 0, 2, 0, 0, 4, 0, 0, 1, 0]
contact_partition_interval(tip)

[4, 3, 2, 1, 0, 0, 0, 0, 0, 0]
rise_vector_interval(tip)

[3, 1, 0, 2, 3, 0, 1, 0, 0, 0]
rise_partition_interval(tip)

[3, 3, 2, 1, 1, 0, 0, 0, 0, 0]
n = 3
sorted([[contact_partition_interval(tip), rise_partition_interval(tip)] for tip in TamariIntervalPosets(n)]) == \
sorted([[rise_partition_interval(tip), contact_partition_interval(tip)] for tip in TamariIntervalPosets(n)])

True

## Tamari Interval-Posets¶

tip = TamariIntervalPoset(10, [(2,5), (3,4), (4,5), (6,7), (8,10), (9,10), (3,2), (4,2), (6,5), (7,5), (8,5), (10,5), (9,8)])

viewLatex(tip)

(tip.lower_dyck_word(), tip.upper_dyck_word())

([1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0], [1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0])
def dc(tip):
L = [len(tip.decreasing_children(i)) for i in tip]
return [len(tip.decreasing_roots())] + L[:-1]

def ic(tip):
L = [len(tip.increasing_children(i)) for i in tip]
L.reverse()
return [len(tip.increasing_roots())] + L[:-1]

print (dc(tip))
print (ic(tip))

[3, 0, 2, 0, 0, 4, 0, 0, 1, 0] [4, 2, 0, 0, 1, 0, 2, 1, 0, 0]
gt = tip.grafting_tree()
viewLatex(gt)

gt_im = gt.left_border_symmetry()
viewLatex(gt_im)

tip2 = TamariIntervalPosets.from_grafting_tree(gt_im)

viewLatex(tip2)

print (dc(tip2))
print (ic(tip2))

[3, 0, 4, 0, 0, 1, 0, 0, 2, 0] [3, 1, 0, 2, 3, 0, 1, 0, 0, 0]
tip3 = tip2.complement()
viewLatex(tip3)

gt = tip3.grafting_tree()
viewLatex(gt)

gt_im = gt.left_border_symmetry()
viewLatex(gt_im)

tip_im = TamariIntervalPosets.from_grafting_tree(gt_im)
viewLatex(tip_im)

print (contact_partition_interval(tip_im))
print (rise_partition_interval(tip_im))

[3, 3, 2, 1, 1, 0, 0, 0, 0, 0] [4, 3, 2, 1, 0, 0, 0, 0, 0, 0]
viewLatex(tip.rise_contact_involution())

n = 3
all(contact_partition_interval(tip) == rise_partition_interval(tip.rise_contact_involution()) for tip in TamariIntervalPosets(n))

True