File: lrcalc.pyx

package info (click to toggle)
python-lrcalc 2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,956 kB
  • sloc: ansic: 5,447; sh: 4,470; makefile: 34; python: 14
file content (311 lines) | stat: -rw-r--r-- 8,303 bytes parent folder | download | duplicates (2)
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
"""Python bindings for the Littlewood-Richardson Calculator."""


from liblrcalc cimport *

cdef ivector *iv_newpy(pv):
    cdef ivector *v
    cdef int i
    v = iv_new(len(pv))
    if v is NULL:
        raise MemoryError()
    for i in range(len(pv)):
        v.array[i] = pv[i]
    return v


cdef tuple iv_tuple(ivector *v):
    cdef int i
    return tuple(v.array[i] for i in range(v.length))

cdef dict ivlc_dict_tuple(ivlincomb *lc):
    cdef ivlc_iter itr
    res = dict()
    ivlc_first(lc, &itr)
    while ivlc_good(&itr):
        res[iv_tuple(ivlc_key(&itr))] = ivlc_value(&itr)
        ivlc_next(&itr)
    return res


cdef tuple iv_part(ivector *v):
    cdef int i, n
    n = v.length
    while n > 0 and v.array[n-1] == 0:
        n -= 1
    return tuple(v.array[i] for i in range(n))

cdef dict ivlc_dict_part(ivlincomb *lc):
    cdef ivlc_iter itr
    res = dict()
    ivlc_first(lc, &itr)
    while ivlc_good(&itr):
        res[iv_part(ivlc_key(&itr))] = ivlc_value(&itr)
        ivlc_next(&itr)
    return res


cdef tuple iv_quantum(ivector *v, int level, bint degrees):
    cdef int i, d, n
    cdef tuple p
    d = part_qdegree(v, level)
    n = v.length
    while n > 0 and part_qentry(v, n-1, d, level) == 0:
        n -= 1
    p = tuple(part_qentry(v, i, d, level) for i in range(n))
    return (p, d) if degrees else p

cdef dict ivlc_dict_quantum(ivlincomb *lc, int level, bint degrees):
    cdef ivlc_iter itr
    res = dict()
    ivlc_first(lc, &itr)
    while ivlc_good(&itr):
        res[iv_quantum(ivlc_key(&itr), level, degrees)] = ivlc_value(&itr)
        ivlc_next(&itr)
    return res


cdef tuple iv_pair(ivector *v, int rows, int cols):
    cdef int i;
    p1 = tuple(v.array[i] - cols for i in range(rows) if v.array[i] != cols)
    p2 = tuple(v.array[i] for i in range(rows, v.length) if v.array[i] != 0)
    return (p1, p2)

cdef dict ivlc_dict_pair(ivlincomb *lc, int rows, int cols):
    cdef ivlc_iter itr
    res = dict()
    ivlc_first(lc, &itr)
    while ivlc_good(&itr):
        res[iv_pair(ivlc_key(&itr), rows, cols)] = ivlc_value(&itr)
        ivlc_next(&itr)
    return res


def lrcoef(out, inn1, inn2):
    """Compute a single Littlewood-Richardson coefficient."""

    cdef ivector *cout = NULL
    cdef ivector *cinn1 = NULL
    cdef ivector *cinn2 = NULL
    try:
        cout = iv_newpy(out)
        cinn1 = iv_newpy(inn1)
        cinn2 = iv_newpy(inn2)
        return schur_lrcoef(cout, cinn1, cinn2)
    finally:
        if cinn2 is not NULL:
            iv_free(cinn2)
        if cinn1 is not NULL:
            iv_free(cinn1)
        if cout is not NULL:
            iv_free(cout)


def mult(sh1, sh2, int rows=-1, int cols=-1):
    """Compute the product of two Schur functions."""

    cdef ivector *csh1 = NULL
    cdef ivector *csh2 = NULL
    cdef ivlincomb *cprd = NULL
    try:
        csh1 = iv_newpy(sh1)
        csh2 = iv_newpy(sh2)
        cprd = schur_mult(csh1, csh2, rows, cols, -1)
        if cprd is NULL:
            raise MemoryError()
        return ivlc_dict_part(cprd)
    finally:
        if cprd is not NULL:
            ivlc_free_all(cprd)
        if csh2 is not NULL:
            iv_free(csh2)
        if csh1 is not NULL:
            iv_free(csh1)


def mult_fusion(sh1, sh2, int rows, int level):
    """Compute a product in the fusion ring of type A."""

    cdef ivector *csh1 = NULL
    cdef ivector *csh2 = NULL
    cdef ivlincomb *cprd = NULL
    try:
        csh1 = iv_newpy(sh1)
        csh2 = iv_newpy(sh2)
        cprd = schur_mult_fusion(csh1, csh2, rows, level)
        if cprd is NULL:
            raise MemoryError()
        return ivlc_dict_part(cprd)
    finally:
        if cprd is not NULL:
            ivlc_free_all(cprd)
        if csh2 is not NULL:
            iv_free(csh2)
        if csh1 is not NULL:
            iv_free(csh1)


def mult_quantum(sh1, sh2, int rows, int cols, bint degrees=False):
    """Compute quantum product of Schubert classes on a Grassmannian."""

    cdef ivector *csh1 = NULL
    cdef ivector *csh2 = NULL
    cdef ivlincomb *cprd = NULL
    try:
        csh1 = iv_newpy(sh1)
        csh2 = iv_newpy(sh2)
        cprd = schur_mult_fusion(csh1, csh2, rows, cols)
        if cprd is NULL:
            raise MemoryError()
        return ivlc_dict_quantum(cprd, cols, degrees)
    finally:
        if cprd is not NULL:
            ivlc_free_all(cprd)
        if csh2 is not NULL:
            iv_free(csh2)
        if csh1 is not NULL:
            iv_free(csh1)


def skew(outer, inner, int rows=-1):
    """Compute the Schur expansion of a skew Schur function."""

    cdef ivector *cout = NULL
    cdef ivector *cinn = NULL
    cdef ivlincomb *cres = NULL
    try:
        cout = iv_newpy(outer)
        cinn = iv_newpy(inner)
        cres = schur_skew(cout, cinn, rows, -1)
        if cres is NULL:
            raise MemoryError()
        return ivlc_dict_part(cres)
    finally:
        if cres is not NULL:
            ivlc_free_all(cres)
        if cinn is not NULL:
            iv_free(cinn)
        if cout is not NULL:
            iv_free(cout)


def coprod(sh, bint all=False):
    """Compute the coproduct of a Schur function."""

    cdef ivector *csh = NULL
    cdef ivlincomb *cres = NULL
    cdef int rows, cols
    try:
        csh = iv_newpy(sh)
        rows = csh.length
        while rows > 0 and csh.array[rows - 1] == 0:
            rows -= 1
        cols = 0 if rows == 0 else csh.array[0]
        cres = schur_coprod(csh, rows, cols, -1, all)
        if cres is NULL:
            raise MemoryError()
        return ivlc_dict_pair(cres, rows, cols)
    finally:
        if cres is not NULL:
            ivlc_free_all(cres)
        if csh is not NULL:
            iv_free(csh)


def schubert_poly(w):
    """Compute the Schubert polynomial of a permutation."""

    cdef ivector *cw = NULL
    cdef ivlincomb *cres = NULL
    try:
        cw = iv_newpy(w)
        cres = trans(cw, 0)
        if cres is NULL:
            raise MemoryError()
        return ivlc_dict_tuple(cres)
    finally:
        if cres is not NULL:
            ivlc_free_all(cres)
        if cw is not NULL:
            iv_free(cw)


def schubmult(w1, w2, int rank=0):
    """Compute the product of two Schubert polynomials."""

    cdef ivector *cw1 = NULL
    cdef ivector *cw2 = NULL
    cdef ivlincomb *cres = NULL
    try:
        cw1 = iv_newpy(w1)
        cw2 = iv_newpy(w2)
        cres = mult_schubert(cw1, cw2, rank)
        if cres is NULL:
            raise MemoryError()
        return ivlc_dict_tuple(cres)
    finally:
        if cres is not NULL:
            ivlc_free_all(cres)
        if cw2 is not NULL:
            iv_free(cw2)
        if cw1 is not NULL:
            iv_free(cw1)


def schubmult_str(str1, str2):
    """Compute product of Schubert polynomials using string notation."""

    cdef ivector *cs1 = NULL
    cdef ivector *cs2 = NULL
    cdef ivlincomb *cres = NULL
    try:
        cs1 = iv_newpy(str1)
        cs2 = iv_newpy(str2)
        cres = mult_schubert_str(cs1, cs2)
        if cres is NULL:
            raise MemoryError()
        return ivlc_dict_tuple(cres)
    finally:
        if cres is not NULL:
            ivlc_free_all(cres)
        if cs2 is not NULL:
            iv_free(cs2)
        if cs1 is not NULL:
            iv_free(cs1)


cdef class lr_iterator:
    """Iterate through column words of LR tableaux of given skew shape."""

    cdef lrtab_iter *_itr

    def __cinit__(self, outer, inner, int rows=-1):
        cdef ivector *out = NULL
        cdef ivector *inn = NULL
        try:
            out = iv_newpy(outer)
            inn = iv_newpy(inner)
            self._itr = lrit_new(out, inn, NULL, rows, -1, -1)
            if self._itr is NULL:
                raise MemoryError()
        finally:
            if inn is not NULL:
                iv_free(inn)
            if out is not NULL:
                iv_free(out)

    def __iter__(self):
        return self

    def __next__(self):
        cdef int i
        if not lrit_good(self._itr):
            raise StopIteration
        word = tuple(self._itr.array[i].value
                     for i in range(self._itr.size))
        lrit_next(self._itr)
        return word

    def __dealloc(self):
        if self._itr is not NULL:
            lrit_free(self._itr)