-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.pyx
314 lines (244 loc) · 8.89 KB
/
utilities.pyx
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from sage.structure.element cimport Element
from sage.rings.polynomial.polydict cimport ETuple
from sage.groups.perm_gps.permgroup_element cimport PermutationGroupElement
import sage.combinat.tableau
from sage.combinat.words.word import Word
from sage.combinat.free_module import CombinatorialFreeModule
cpdef items_of_vector(Element v):
"""
Return an iterator over the pairs ``(index, coefficient)`` for `v`.
INPUT::
- ``v`` -- an element of some some vector space or free module
EXAMPLES:
This handles indexed free module elements::
sage: E = CombinatorialFreeModule(QQ, [1,2,4,8,16])
sage: v = E.an_element(); v
2*B[1] + 2*B[2] + 3*B[4]
sage: list(items_of_vector(v))
[(1, 2), (2, 2), (4, 3)]
free module elements::
sage: v = vector([4,0,1,2])
sage: list(items_of_vector(v))
[(0, 4), (2, 1), (3, 2)]
sage: v = vector([4,0,1,2], sparse=True)
sage: list(items_of_vector(v))
[(0, 4), (2, 1), (3, 2)]
multivariate polynomials::
sage: P = QQ['x,y,z']
sage: x,y,z = P.gens()
sage: p = (x+y+1)^2; p
x^2 + 2*x*y + y^2 + 2*x + 2*y + 1
sage: sorted(items_of_vector(p))
[((0, 0, 0), 1),
((0, 1, 0), 2),
((0, 2, 0), 1),
((1, 0, 0), 2),
((1, 1, 0), 2),
((2, 0, 0), 1)]
univariate polynomials::
sage: P = ZZ['x']
sage: x = P.gen()
sage: (x+2)^3
x^3 + 6*x^2 + 12*x + 8
sage: list(items_of_vector(_))
[(0, 8), (1, 12), (2, 6), (3, 1)]
elements of quotients::
sage: C = CyclotomicField(5)
sage: z = C.gen()
sage: p = (z+2)^2; p
zeta5^2 + 4*zeta5 + 4
sage: list(items_of_vector(p))
[(0, 4), (1, 4), (2, 1)]
"""
if isinstance(v, CombinatorialFreeModule.Element):
return v
else:
try:
return v.dict().items()
except AttributeError:
return items_of_vector(v.lift())
cpdef act_on_polynomial(p, PermutationGroupElement sigma):
"""
EXAMPLES::
sage: x,y,z,t = QQ['x,y,z,t'].gens()
sage: s = PermutationGroupElement([(1,2,3,4)])
sage: p = 2*x^2*y+3*z
sage: p2 = p^10
sage: p3 = p^100
sage: act_on_polynomial(p, s)
2*x*t^2 + 3*y
Current implementation in Sage::
sage: %timeit p*s # not tested
10000 loops, best of 3: 65.4 µs per loop
sage: %timeit p2*s # not tested
10000 loops, best of 3: 73.3 µs per loop
sage: %timeit p3*s # not tested
10000 loops, best of 3: 188 µs per loop
sage: %timeit s._act_on_(p,0) # not tested
10000 loops, best of 3: 66.4 µs per loop
sage: %timeit s._act_on_(p2,0) # not tested
10000 loops, best of 3: 73.4 µs per loop
sage: %timeit s._act_on_(p3,0) # not tested
10000 loops, best of 3: 189 µs per loop
After Cythonization:
sage: %timeit act_on_polynomial(p, s) # not tested
10000 loops, best of 3: 24.5 µs per loop
sage: %timeit act_on_polynomial(p2, s) # not tested
10000 loops, best of 3: 86.2 µs per loop
sage: %timeit act_on_polynomial(p3, s) # not tested
1000 loops, best of 3: 730 µs per loop
"""
R = p.parent()
# This should be a map_support
return R({ETuple(sigma._act_on_list_on_position(list(<ETuple>t))): c
for t, c in p.dict().iteritems() })
#n = R.ngens()
#return R({tuple(t[sigma(i)-1] for i in range(1,n+1)): c
# for t,c in p.dict().iteritems() })
cpdef list diagonal_swap(list exponents, int n, int r, int j1, int j2):
"""
Swap in place two columns of a diagonal exponent vector.
INPUT:
- ``exponents `` -- a list, seen as an `r\times n` array
- ``r``, ``n`` -- nonnegative integers
- ``j1``, ``j2`` -- integers in `0,\ldots,n-1`
Swap inplace the columnss ``j1`` and ``j2`` in the list ``exponnents``,
seen as an `r\times n` array.
EXAMPLES::
sage: l = [1,2,3,4,5,6,7,8]
sage: diagonal_swap(l, 4, 2, 1, 3)
sage: l
[1, 4, 3, 2, 5, 8, 7, 6]
sage: l = [1,2,3,4,5,6,7,8]
sage: diagonal_swap(l, 2, 4, 0, 1)
sage: l
[2, 1, 4, 3, 6, 5, 8, 7]
"""
cdef int i
for i in range(r):
exponents[i*n+j1], exponents[i*n+j2] = exponents[i*n+j2], exponents[i*n+j1]
cpdef int diagonal_cmp(list exponents, int n, int r, int j1, int j2):
"""
Compare lexicographically two columns of a diagonal exponent vector.
INPUT:
- ``exponents `` -- a list, seen as an `r\times n` array
- ``r``, ``n`` -- nonnegative integers
- ``j1``, ``j2`` -- integers in `0,\ldots,n-1`
Compare lexicographically the columns ``j1`` and ``j2`` in the
list ``exponnents``, seen as an `r\times n` array.
EXAMPLES::
sage: l = [1, 1, 2, 2, 0, 1, 1, 0]
sage: diagonal_cmp(l, 4, 2, 0, 1)
-1
sage: diagonal_cmp(l, 4, 2, 1, 0)
1
sage: diagonal_cmp(l, 4, 2, 2, 3)
1
sage: diagonal_cmp(l, 4, 2, 3, 2)
-1
sage: diagonal_cmp(l, 4, 2, 3, 3)
0
"""
cdef int i
cdef int c
cdef int a
cdef int b
for i in range(r):
a = exponents[i*n+j1]
b = exponents[i*n+j2]
if a != b:
# Using trick from https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons
return (a > b) - (a < b)
return 0
cpdef reverse_sorting_permutation(t): # TODO: put "stable sorting" as keyword somewhere
r"""
Return a permutation `p` such that is decreasing
INPUT:
- `t` -- a list/tuple/... of numbers
OUTPUT:
a minimal permutation `p` such that `w \circ p` is sorted decreasingly
EXAMPLES::
sage: t = [3, 3, 1, 2]
sage: s = reverse_sorting_permutation(t); s
[1, 2, 4, 3]
sage: [t[s[i]-1] for i in range(len(t))]
[3, 3, 2, 1]
sage: t = [4, 2, 3, 2, 1, 3]
sage: s = reverse_sorting_permutation(t); s
[1, 3, 6, 2, 4, 5]
sage: [t[s[i]-1] for i in range(len(t))]
[4, 3, 3, 2, 2, 1]
"""
return ~(Word([-i for i in t]).standard_permutation())
cpdef destandardize(self):
"""
Return the smallest word whose standard permutation is ``self``
INPUT:
- ``self`` -- a permutation of 1...n
OUTPUT: a word in the alphabet 0,...,
EXAMPLES::
sage: for p in Permutations(3): print(p, destandardize(p))
[1, 2, 3] [0, 0, 0]
[1, 3, 2] [0, 1, 0]
[2, 1, 3] [1, 0, 1]
[2, 3, 1] [1, 1, 0]
[3, 1, 2] [1, 0, 0]
[3, 2, 1] [2, 1, 0]
sage: for p in Permutations(4):
....: assert Word(destandardize(p)).standard_permutation() == p
"""
n = len(self)
sigma = ~self
c = 0
w = [None] * n
for i in range(1,n+1):
w[sigma(i)-1] = c
if i < n and sigma(i+1) < sigma(i):
c += 1
return w
cpdef index_filling(t):
"""
Return the index filling of this standard tableau.
INPUT:
- ``t`` -- a standard tableau
The index filling of `t` is the semi standard tableau with lowest
content whose standardized row reading coincides with the row
reading of `t`.
Reference: Higher Specht Polynomials for the symmetric group and
the wreath product, S. Ariki, T. Terasoma, H. Yamada.
Note: in the above reference, the reading word is instead the
reverse of the row reading of the transpose of `t`.
.. TODO::
Check whether this is the most desirable convention.
EXAMPLES::
sage: Tableaux.options.convention="french"
sage: t = StandardTableau([[1,2,4], [3,5]])
sage: ascii_art(t, index_filling(t), sep = " --> ")
3 5 1 2
1 2 4 --> 0 0 1
sage: for t in StandardTableaux([3,2,1]): #not tested
....: print ascii_art(t, index_filling(t), sep=" --> "); print #not tested
3 2
2 5 1 3
1 4 6 --> 0 2 3
<BLANKLINE>
4 2
2 5 1 2
1 3 6 --> 0 1 2
<BLANKLINE>
4 2
3 5 1 2
1 2 6 --> 0 0 2
...
6 3
2 4 1 2
1 3 5 --> 0 1 2
...
6 2
4 5 1 1
1 2 3 --> 0 0 0
The sum of the entries of the index filling is the cocharge of `t`::
sage: for t in StandardTableaux(6):
....: assert t.cocharge() == sum(i for row in index_filling(t) for i in row)
"""
return sage.combinat.tableau.from_shape_and_word(t.shape(), destandardize(t.reading_word_permutation()))