-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh_patterns.sage
97 lines (74 loc) · 2.36 KB
/
mesh_patterns.sage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
load("../pattern-avoidance/sub_functions.sage")
def avoids_mpat(perm, mpat):
"""
Returns False if the mesh pattern mpat occurs in the permutation perm,
otherwise returns True
EXAMPLES
sage: avoids_mpat([1,2,3], ([1,2], [(0,0)]))
False
"""
pat = mpat[0]
R = mpat[1]
k = len(pat)
n = len(perm)
# If the pattern is longer than the permutation, return True
if k > n: return True
pat = G(pat)
perm = G(perm)
for H in Subwords(perm, k):
X = dict(G(sorted(i for (i,_) in H)))
Y = dict(G(sorted(j for (_,j) in H)))
if H == [ (X[i], Y[j]) for (i,j) in pat ]:
X[0], X[k+1] = 0, n+1
Y[0], Y[k+1] = 0, n+1
shady = ( X[i] < x < X[i+1] and Y[j] < y < Y[j+1]
for (i,j) in R
for (x,y) in perm
)
if not any(shady):
return False
return True
def avoids_mpats(perm, mpats):
"""
Returns False if any mesh pattern from mpats occurs in perm, otherwise
returns True
EXAMPLES
sage: avoids_mpat([1,2,3], [])
True
"""
for mpat in mpats:
if avoids_mpat(perm, mpat) == False:
return False
return True
def avoids_mpat_many_shadings(perm, pat, Rs):
"""
Returns False if (pat, R) occurs in perm for any R in Rs, otherwise returns True
"""
k = len(pat)
n = len(perm)
if k>n: return True
pat = G(pat)
perm = G(perm)
for H in Subwords(perm, k):
X = dict(G(sorted(i for (i,_) in H)))
Y = dict(G(sorted(j for (_,j) in H)))
if H == [ (X[i], Y[j]) for (i,j) in pat ]:
X[0], X[k+1] = 0, n+1
Y[0], Y[k+1] = 0, n+1
for R in Rs:
shady = ( X[i] < x < X[i+1] and Y[j] < y < Y[j+1]
for (i,j) in R
for (x,y) in perm
)
if not any(shady):
return False
return True
def avoids_mpats_many_shadings(perm,pats_w_shadings):
"""
Returns False if any pattern from pats occurs in perm, otherwise returns True
"""
for n in pats_w_shadings.keys():
for pat in pats_w_shadings[n].keys():
if avoids_mpat_many_shadings(perm,pat,pats_w_shadings[n][pat]) == False:
return False
return True