This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
shuffle_algebra.py
961 lines (766 loc) · 30.4 KB
/
shuffle_algebra.py
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# -*- coding: utf-8 -*-
r"""
Shuffle algebras
AUTHORS:
- Frédéric Chapoton (2013-03): Initial version
- Matthieu Deneufchatel (2013-07): Implemented dual PBW basis
"""
# ****************************************************************************
# Copyright (C) 2013 Frédéric Chapoton <chapoton-math-univ-lyon1-fr>
#
# Distributed under the terms of the GNU General Public License (GPL)
# https://www.gnu.org/licenses/
# ****************************************************************************
from sage.categories.rings import Rings
from sage.categories.graded_hopf_algebras_with_basis import GradedHopfAlgebrasWithBasis
from sage.combinat.free_module import CombinatorialFreeModule
from sage.combinat.words.alphabet import Alphabet
from sage.combinat.words.words import Words
from sage.combinat.words.word import Word
from sage.combinat.words.finite_word import FiniteWord_class
from sage.misc.cachefunc import cached_method
from sage.misc.lazy_attribute import lazy_attribute
from sage.misc.misc_c import prod
from sage.sets.family import Family
from sage.rings.integer_ring import ZZ
class ShuffleAlgebra(CombinatorialFreeModule):
r"""
The shuffle algebra on some generators over a base ring.
Shuffle algebras are commutative and associative algebras, with a
basis indexed by words. The product of two words `w_1 \cdot w_2` is given
by the sum over the shuffle product of `w_1` and `w_2`.
.. SEEALSO::
For more on shuffle products, see
:mod:`~sage.combinat.words.shuffle_product` and
:meth:`~sage.combinat.words.finite_word.FiniteWord_class.shuffle()`.
REFERENCES:
- :wikipedia:`Shuffle algebra`
INPUT:
- ``R`` -- ring
- ``names`` -- generator names (string or an alphabet)
EXAMPLES::
sage: F = ShuffleAlgebra(QQ, 'xyz'); F
Shuffle Algebra on 3 generators ['x', 'y', 'z'] over Rational Field
sage: mul(F.gens())
B[word: xyz] + B[word: xzy] + B[word: yxz] + B[word: yzx] + B[word: zxy] + B[word: zyx]
sage: mul([ F.gen(i) for i in range(2) ]) + mul([ F.gen(i+1) for i in range(2) ])
B[word: xy] + B[word: yx] + B[word: yz] + B[word: zy]
sage: S = ShuffleAlgebra(ZZ, 'abcabc'); S
Shuffle Algebra on 3 generators ['a', 'b', 'c'] over Integer Ring
sage: S.base_ring()
Integer Ring
sage: G = ShuffleAlgebra(S, 'mn'); G
Shuffle Algebra on 2 generators ['m', 'n'] over Shuffle Algebra on 3 generators ['a', 'b', 'c'] over Integer Ring
sage: G.base_ring()
Shuffle Algebra on 3 generators ['a', 'b', 'c'] over Integer Ring
Shuffle algebras commute with their base ring::
sage: K = ShuffleAlgebra(QQ,'ab')
sage: a,b = K.gens()
sage: K.is_commutative()
True
sage: L = ShuffleAlgebra(K,'cd')
sage: c,d = L.gens()
sage: L.is_commutative()
True
sage: s = a*b^2 * c^3; s
(12*B[word:abb]+12*B[word:bab]+12*B[word:bba])*B[word: ccc]
sage: parent(s)
Shuffle Algebra on 2 generators ['c', 'd'] over Shuffle Algebra on 2 generators ['a', 'b'] over Rational Field
sage: c^3 * a * b^2
(12*B[word:abb]+12*B[word:bab]+12*B[word:bba])*B[word: ccc]
Shuffle algebras are commutative::
sage: c^3 * b * a * b == c * a * c * b^2 * c
True
We can also manipulate elements in the basis and coerce elements from our
base field::
sage: F = ShuffleAlgebra(QQ, 'abc')
sage: B = F.basis()
sage: B[Word('bb')] * B[Word('ca')]
B[word: bbca] + B[word: bcab] + B[word: bcba] + B[word: cabb] + B[word: cbab] + B[word: cbba]
sage: 1 - B[Word('bb')] * B[Word('ca')] / 2
B[word: ] - 1/2*B[word: bbca] - 1/2*B[word: bcab] - 1/2*B[word: bcba] - 1/2*B[word: cabb] - 1/2*B[word: cbab] - 1/2*B[word: cbba]
TESTS::
sage: R = ShuffleAlgebra(QQ,'x')
sage: R.is_commutative()
True
sage: R = ShuffleAlgebra(QQ,'xy')
sage: R.is_commutative()
True
"""
@staticmethod
def __classcall_private__(cls, R, names):
"""
Normalize input to ensure a unique representation.
EXAMPLES::
sage: F1 = ShuffleAlgebra(QQ, 'xyz')
sage: F2 = ShuffleAlgebra(QQ, ['x','y','z'])
sage: F3 = ShuffleAlgebra(QQ, Alphabet('xyz'))
sage: F1 is F2 and F1 is F3
True
"""
return super(ShuffleAlgebra, cls).__classcall__(cls, R, Alphabet(names))
def __init__(self, R, names):
r"""
Initialize ``self``.
EXAMPLES::
sage: F = ShuffleAlgebra(QQ, 'xyz'); F
Shuffle Algebra on 3 generators ['x', 'y', 'z'] over Rational Field
sage: TestSuite(F).run()
TESTS::
sage: ShuffleAlgebra(24, 'toto')
Traceback (most recent call last):
...
TypeError: argument R must be a ring
"""
if R not in Rings():
raise TypeError("argument R must be a ring")
self._alphabet = names
self.__ngens = self._alphabet.cardinality()
CombinatorialFreeModule.__init__(self, R, Words(names, infinite=False),
latex_prefix="",
category=GradedHopfAlgebrasWithBasis(R).Commutative())
def variable_names(self):
r"""
Return the names of the variables.
EXAMPLES::
sage: R = ShuffleAlgebra(QQ,'xy')
sage: R.variable_names()
{'x', 'y'}
"""
return self._alphabet
def _repr_(self):
r"""
Text representation of this shuffle algebra.
EXAMPLES::
sage: F = ShuffleAlgebra(QQ,'xyz')
sage: F # indirect doctest
Shuffle Algebra on 3 generators ['x', 'y', 'z'] over Rational Field
sage: ShuffleAlgebra(ZZ,'a')
Shuffle Algebra on one generator ['a'] over Integer Ring
"""
if self.__ngens == 1:
gen = "one generator"
else:
gen = "%s generators" % self.__ngens
return "Shuffle Algebra on " + gen + " %s over %s" % (
self._alphabet.list(), self.base_ring())
@cached_method
def one_basis(self):
r"""
Return the empty word, which index of `1` of this algebra,
as per :meth:`AlgebrasWithBasis.ParentMethods.one_basis`.
EXAMPLES::
sage: A = ShuffleAlgebra(QQ,'a')
sage: A.one_basis()
word:
sage: A.one()
B[word: ]
"""
return self.basis().keys()([])
def product_on_basis(self, w1, w2):
r"""
Return the product of basis elements ``w1`` and ``w2``, as per
:meth:`AlgebrasWithBasis.ParentMethods.product_on_basis()`.
INPUT:
- ``w1``, ``w2`` -- Basis elements
EXAMPLES::
sage: A = ShuffleAlgebra(QQ,'abc')
sage: W = A.basis().keys()
sage: A.product_on_basis(W("acb"), W("cba"))
B[word: acbacb] + B[word: acbcab] + 2*B[word: acbcba]
+ 2*B[word: accbab] + 4*B[word: accbba] + B[word: cabacb]
+ B[word: cabcab] + B[word: cabcba] + B[word: cacbab]
+ 2*B[word: cacbba] + 2*B[word: cbaacb] + B[word: cbacab]
+ B[word: cbacba]
sage: (a,b,c) = A.algebra_generators()
sage: a * (1-b)^2 * c
2*B[word: abbc] - 2*B[word: abc] + 2*B[word: abcb] + B[word: ac]
- 2*B[word: acb] + 2*B[word: acbb] + 2*B[word: babc]
- 2*B[word: bac] + 2*B[word: bacb] + 2*B[word: bbac]
+ 2*B[word: bbca] - 2*B[word: bca] + 2*B[word: bcab]
+ 2*B[word: bcba] + B[word: ca] - 2*B[word: cab] + 2*B[word: cabb]
- 2*B[word: cba] + 2*B[word: cbab] + 2*B[word: cbba]
"""
return sum(self.basis()[u] for u in w1.shuffle(w2))
def antipode_on_basis(self, w):
"""
Return the antipode on the basis element ``w``.
EXAMPLES::
sage: A = ShuffleAlgebra(QQ,'abc')
sage: W = A.basis().keys()
sage: A.antipode_on_basis(W("acb"))
-B[word: bca]
"""
mone = -self.base_ring().one()
return self.term(w.reversal(), mone**len(w))
def gen(self, i):
r"""
Return the ``i``-th generator of the algebra.
INPUT:
- ``i`` -- an integer
EXAMPLES::
sage: F = ShuffleAlgebra(ZZ,'xyz')
sage: F.gen(0)
B[word: x]
sage: F.gen(4)
Traceback (most recent call last):
...
IndexError: argument i (= 4) must be between 0 and 2
"""
n = self.__ngens
if i < 0 or not i < n:
raise IndexError("argument i (= %s) must be between 0 and %s" % (i, n - 1))
return self.algebra_generators()[i]
def some_elements(self):
"""
Return some typical elements.
EXAMPLES::
sage: F = ShuffleAlgebra(ZZ,'xyz')
sage: F.some_elements()
[0, B[word: ], B[word: x], B[word: y], B[word: z], B[word: xz] + B[word: zx]]
"""
gens = list(self.algebra_generators())
if gens:
gens.append(gens[0] * gens[-1])
return [self.zero(), self.one()] + gens
def coproduct_on_basis(self, w):
"""
Return the coproduct of the element of the basis indexed by
the word ``w``.
The coproduct is given by deconcatenation.
INPUT:
- ``w`` -- a word
EXAMPLES::
sage: F = ShuffleAlgebra(QQ,'ab')
sage: F.coproduct_on_basis(Word('a'))
B[word: ] # B[word: a] + B[word: a] # B[word: ]
sage: F.coproduct_on_basis(Word('aba'))
B[word: ] # B[word: aba] + B[word: a] # B[word: ba]
+ B[word: ab] # B[word: a] + B[word: aba] # B[word: ]
sage: F.coproduct_on_basis(Word())
B[word: ] # B[word: ]
TESTS::
sage: F = ShuffleAlgebra(QQ,'ab')
sage: S = F.an_element(); S
B[word: ] + 2*B[word: a] + 3*B[word: b] + B[word: bab]
sage: F.coproduct(S)
B[word: ] # B[word: ] + 2*B[word: ] # B[word: a]
+ 3*B[word: ] # B[word: b] + B[word: ] # B[word: bab]
+ 2*B[word: a] # B[word: ] + 3*B[word: b] # B[word: ]
+ B[word: b] # B[word: ab] + B[word: ba] # B[word: b]
+ B[word: bab] # B[word: ]
sage: F.coproduct(F.one())
B[word: ] # B[word: ]
"""
TS = self.tensor_square()
return TS.sum_of_terms([((w[:i], w[i:]), 1)
for i in range(len(w) + 1)], distinct=True)
def counit(self, S):
"""
Return the counit of ``S``.
EXAMPLES::
sage: F = ShuffleAlgebra(QQ,'ab')
sage: S = F.an_element(); S
B[word: ] + 2*B[word: a] + 3*B[word: b] + B[word: bab]
sage: F.counit(S)
1
"""
W = self.basis().keys()
return S.coefficient(W())
def degree_on_basis(self, w):
"""
Return the degree of the element ``w``.
EXAMPLES::
sage: A = ShuffleAlgebra(QQ, 'ab')
sage: [A.degree_on_basis(x.leading_support()) for x in A.some_elements() if x != 0]
[0, 1, 1, 2]
"""
return ZZ(len(w))
@cached_method
def algebra_generators(self):
r"""
Return the generators of this algebra.
EXAMPLES::
sage: A = ShuffleAlgebra(ZZ,'fgh'); A
Shuffle Algebra on 3 generators ['f', 'g', 'h'] over Integer Ring
sage: A.algebra_generators()
Family (B[word: f], B[word: g], B[word: h])
sage: A = ShuffleAlgebra(QQ, ['x1','x2'])
sage: A.algebra_generators()
Family (B[word: x1], B[word: x2])
"""
Words = self.basis().keys()
return Family([self.monomial(Words([a])) for a in self._alphabet])
# FIXME: use this once the keys argument of FiniteFamily will be honoured
# for the specifying the order of the elements in the family
# return Family(self._alphabet, lambda a: self.term(self.basis().keys()(a)))
gens = algebra_generators
def _element_constructor_(self, x):
r"""
Convert ``x`` into ``self``.
EXAMPLES::
sage: R = ShuffleAlgebra(QQ,'xy')
sage: x, y = R.gens()
sage: R(3)
3*B[word: ]
sage: R(x)
B[word: x]
sage: R('xyy')
B[word: xyy]
sage: R(Word('xyx'))
B[word: xyx]
"""
if isinstance(x, (str, FiniteWord_class)):
W = self.basis().keys()
return self.monomial(W(x))
P = x.parent()
if isinstance(P, ShuffleAlgebra):
if P is self:
return x
if not (P is self.base_ring()):
return self.element_class(self, x.monomial_coefficients())
if isinstance(P, DualPBWBasis):
return self(P.expansion(x))
# ok, not a shuffle algebra element (or should not be viewed as one).
if isinstance(x, str):
from sage.misc.sage_eval import sage_eval
return sage_eval(x, locals=self.gens_dict())
R = self.base_ring()
# coercion via base ring
x = R(x)
if x == 0:
return self.element_class(self, {})
else:
return self.from_base_ring_from_one_basis(x)
def _coerce_map_from_(self, R):
r"""
Return ``True`` if there is a coercion from ``R`` into ``self``
and ``False`` otherwise.
The things that coerce into ``self`` are
- Shuffle Algebras in the same variables over a base with a coercion
map into ``self.base_ring()``.
- Anything with a coercion into ``self.base_ring()``.
EXAMPLES::
sage: F = ShuffleAlgebra(GF(7), 'xyz'); F
Shuffle Algebra on 3 generators ['x', 'y', 'z'] over Finite Field of size 7
Elements of the shuffle algebra canonically coerce in::
sage: x, y, z = F.gens()
sage: F.coerce(x*y) # indirect doctest
B[word: xy] + B[word: yx]
Elements of the integers coerce in, since there is a coerce map
from `\ZZ` to GF(7)::
sage: F.coerce(1) # indirect doctest
B[word: ]
There is no coerce map from `\QQ` to `\GF{7}`::
sage: F.coerce(2/3) # indirect doctest
Traceback (most recent call last):
...
TypeError: no canonical coercion from Rational Field to Shuffle Algebra on 3 generators ['x', 'y', 'z'] over Finite Field of size 7
Elements of the base ring coerce in::
sage: F.coerce(GF(7)(5))
5*B[word: ]
The shuffle algebra over `\ZZ` on `x, y, z` coerces in, since
`\ZZ` coerces to `\GF{7}`::
sage: G = ShuffleAlgebra(ZZ,'xyz')
sage: Gx,Gy,Gz = G.gens()
sage: z = F.coerce(Gx**2 * Gy);z
2*B[word: xxy] + 2*B[word: xyx] + 2*B[word: yxx]
sage: z.parent() is F
True
However, `\GF{7}` does not coerce to `\ZZ`, so the shuffle
algebra over `\GF{7}` does not coerce to the one over `\ZZ`::
sage: G.coerce(x^3*y)
Traceback (most recent call last):
...
TypeError: no canonical coercion from Shuffle Algebra on 3 generators
['x', 'y', 'z'] over Finite Field of size 7 to Shuffle Algebra on 3
generators ['x', 'y', 'z'] over Integer Ring
TESTS::
sage: F = ShuffleAlgebra(ZZ, 'xyz')
sage: G = ShuffleAlgebra(QQ, 'xyz')
sage: H = ShuffleAlgebra(ZZ, 'y')
sage: F._coerce_map_from_(G)
False
sage: G._coerce_map_from_(F)
True
sage: F._coerce_map_from_(H)
False
sage: F._coerce_map_from_(QQ)
False
sage: G._coerce_map_from_(QQ)
True
sage: F.has_coerce_map_from(PolynomialRing(ZZ, 3, 'x,y,z'))
False
sage: F._coerce_map_from_(F.dual_pbw_basis())
True
"""
# shuffle algebras in the same variable over any base that coerces in:
if isinstance(R, ShuffleAlgebra):
if R.variable_names() == self.variable_names():
if self.base_ring().has_coerce_map_from(R.base_ring()):
return True
else:
return False
if isinstance(R, DualPBWBasis):
return self.has_coerce_map_from(R._alg)
return self.base_ring().has_coerce_map_from(R)
def dual_pbw_basis(self):
"""
Return the dual PBW of ``self``.
EXAMPLES::
sage: A = ShuffleAlgebra(QQ, 'ab')
sage: A.dual_pbw_basis()
The dual Poincare-Birkhoff-Witt basis of Shuffle Algebra on 2 generators ['a', 'b'] over Rational Field
"""
return DualPBWBasis(self.base_ring(), self._alphabet)
def to_dual_pbw_element(self, w):
"""
Return the element `w` of ``self`` expressed in the dual PBW basis.
INPUT:
- ``w`` -- an element of the shuffle algebra
EXAMPLES::
sage: A = ShuffleAlgebra(QQ, 'ab')
sage: f = 2 * A(Word()) + A(Word('ab')); f
2*B[word: ] + B[word: ab]
sage: A.to_dual_pbw_element(f)
2*S[word: ] + S[word: ab]
sage: A.to_dual_pbw_element(A.one())
S[word: ]
sage: S = A.dual_pbw_basis()
sage: elt = S.expansion_on_basis(Word('abba')); elt
2*B[word: aabb] + B[word: abab] + B[word: abba]
sage: A.to_dual_pbw_element(elt)
S[word: abba]
sage: A.to_dual_pbw_element(2*A(Word('aabb')) + A(Word('abab')))
S[word: abab]
sage: S.expansion(S('abab'))
2*B[word: aabb] + B[word: abab]
"""
D = self.dual_pbw_basis()
l = {}
W = self.basis().keys()
while w != self.zero():
support = [W(i[0]) for i in list(w)]
min_elt = W(support[0])
if len(support) > 1:
for word in support[1:len(support) - 1]:
if min_elt.lex_less(word):
min_elt = W(word)
coeff = list(w)[support.index(min_elt)][1]
l[min_elt] = l.get(min_elt, 0) + coeff
w = w - coeff * D.expansion_on_basis(W(min_elt))
return D.sum_of_terms((m, c) for m, c in l.items() if c != 0)
class DualPBWBasis(CombinatorialFreeModule):
r"""
The basis dual to the Poincaré-Birkhoff-Witt basis of the free algebra.
We recursively define the dual PBW basis as the basis of the
shuffle algebra given by
.. MATH::
S_w = \begin{cases}
w & |w| = 1, \\
x S_u & w = xu \text{ and } w \in \mathrm{Lyn}(X), \\
\displaystyle \frac{S_{\ell_{i_1}}^{\ast \alpha_1} \ast \cdots
\ast S_{\ell_{i_k}}^{\ast \alpha_k}}{\alpha_1! \cdots \alpha_k!} &
w = \ell_{i_1}^{\alpha_1} \cdots \ell_{i_k}^{\alpha_k} \text{ with }
\ell_1 > \cdots > \ell_k \in \mathrm{Lyn}(X).
\end{cases}
where `S \ast T` denotes the shuffle product of `S` and `T` and
`\mathrm{Lyn}(X)` is the set of Lyndon words in the alphabet `X`.
The definition may be found in Theorem 5.3 of [Reu1993]_.
INPUT:
- ``R`` -- ring
- ``names`` -- names of the generators (string or an alphabet)
EXAMPLES::
sage: S = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: S
The dual Poincare-Birkhoff-Witt basis of Shuffle Algebra on 2 generators ['a', 'b'] over Rational Field
sage: S.one()
S[word: ]
sage: S.one_basis()
word:
sage: T = ShuffleAlgebra(QQ, 'abcd').dual_pbw_basis(); T
The dual Poincare-Birkhoff-Witt basis of Shuffle Algebra on 4 generators ['a', 'b', 'c', 'd'] over Rational Field
sage: T.algebra_generators()
(S[word: a], S[word: b], S[word: c], S[word: d])
TESTS:
We check conversion between the bases::
sage: A = ShuffleAlgebra(QQ, 'ab')
sage: S = A.dual_pbw_basis()
sage: W = Words('ab', 5)
sage: all(S(A(S(w))) == S(w) for w in W)
True
sage: all(A(S(A(w))) == A(w) for w in W)
True
"""
@staticmethod
def __classcall_private__(cls, R, names):
"""
Normalize input to ensure a unique representation.
EXAMPLES::
sage: from sage.algebras.shuffle_algebra import DualPBWBasis
sage: D1 = DualPBWBasis(QQ, 'ab')
sage: D2 = DualPBWBasis(QQ, Alphabet('ab'))
sage: D1 is D2
True
"""
return super(DualPBWBasis, cls).__classcall__(cls, R, Alphabet(names))
def __init__(self, R, names):
"""
Initialize ``self``.
EXAMPLES::
sage: D = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: TestSuite(D).run()
"""
self._alphabet = names
self._alg = ShuffleAlgebra(R, names)
CombinatorialFreeModule.__init__(self, R, Words(names), prefix='S',
category=GradedHopfAlgebrasWithBasis(R).Commutative())
def _repr_(self):
"""
Return a string representation of ``self``.
EXAMPLES::
sage: ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
The dual Poincare-Birkhoff-Witt basis of Shuffle Algebra on 2 generators ['a', 'b'] over Rational Field
"""
return "The dual Poincare-Birkhoff-Witt basis of {}".format(self._alg)
def _element_constructor_(self, x):
"""
Construct an element of ``self`` from ``x``.
EXAMPLES::
sage: A = ShuffleAlgebra(QQ, 'ab')
sage: S = A.dual_pbw_basis()
sage: S('abaab')
S[word: abaab]
sage: S(Word('aba'))
S[word: aba]
sage: S(A('ab'))
S[word: ab]
"""
if isinstance(x, (str, FiniteWord_class)):
W = self.basis().keys()
x = W(x)
elif isinstance(x.parent(), ShuffleAlgebra):
return self._alg.to_dual_pbw_element(self._alg(x))
return super(DualPBWBasis, self)._element_constructor_(x)
def _coerce_map_from_(self, R):
"""
Return ``True`` if there is a coercion from ``R`` into ``self`` and
``False`` otherwise. The things that coerce into ``self`` are:
- Anything that coerces into the associated shuffle algebra of ``self``
TESTS::
sage: F = ShuffleAlgebra(ZZ, 'xyz').dual_pbw_basis()
sage: G = ShuffleAlgebra(QQ, 'xyz').dual_pbw_basis()
sage: H = ShuffleAlgebra(ZZ, 'y').dual_pbw_basis()
sage: F._coerce_map_from_(G)
False
sage: G._coerce_map_from_(F)
True
sage: F._coerce_map_from_(H)
False
sage: F._coerce_map_from_(QQ)
False
sage: G._coerce_map_from_(QQ)
True
sage: F.has_coerce_map_from(PolynomialRing(ZZ, 3, 'x,y,z'))
False
sage: F._coerce_map_from_(F._alg)
True
"""
return self._alg.has_coerce_map_from(R)
def one_basis(self):
"""
Return the indexing element of the basis element `1`.
EXAMPLES::
sage: S = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: S.one_basis()
word:
"""
W = self.basis().keys()
return W([])
def counit(self, S):
"""
Return the counit of ``S``.
EXAMPLES::
sage: F = ShuffleAlgebra(QQ,'ab').dual_pbw_basis()
sage: (3*F.gen(0)+5*F.gen(1)**2).counit()
0
sage: (4*F.one()).counit()
4
"""
W = self.basis().keys()
return S.coefficient(W())
def algebra_generators(self):
"""
Return the algebra generators of ``self``.
EXAMPLES::
sage: S = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: S.algebra_generators()
(S[word: a], S[word: b])
"""
W = self.basis().keys()
return tuple(self.monomial(W(a)) for a in self._alphabet)
gens = algebra_generators
def gen(self, i):
"""
Return the ``i``-th generator of ``self``.
EXAMPLES::
sage: S = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: S.gen(0)
S[word: a]
sage: S.gen(1)
S[word: b]
"""
return self.algebra_generators()[i]
def some_elements(self):
"""
Return some typical elements.
EXAMPLES::
sage: F = ShuffleAlgebra(QQ,'xyz').dual_pbw_basis()
sage: F.some_elements()
[0, S[word: ], S[word: x], S[word: y], S[word: z], S[word: zx]]
"""
gens = list(self.algebra_generators())
if gens:
gens.append(gens[0] * gens[-1])
return [self.zero(), self.one()] + gens
def shuffle_algebra(self):
"""
Return the associated shuffle algebra of ``self``.
EXAMPLES::
sage: S = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: S.shuffle_algebra()
Shuffle Algebra on 2 generators ['a', 'b'] over Rational Field
"""
return self._alg
def product(self, u, v):
"""
Return the product of two elements ``u`` and ``v``.
EXAMPLES::
sage: S = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: a,b = S.gens()
sage: S.product(a, b)
S[word: ba]
sage: S.product(b, a)
S[word: ba]
sage: S.product(b^2*a, a*b*a)
36*S[word: bbbaaa]
TESTS:
Check that multiplication agrees with the multiplication in the
shuffle algebra::
sage: A = ShuffleAlgebra(QQ, 'ab')
sage: S = A.dual_pbw_basis()
sage: a,b = S.gens()
sage: A(a*b)
B[word: ab] + B[word: ba]
sage: A(a*b*a)
2*B[word: aab] + 2*B[word: aba] + 2*B[word: baa]
sage: S(A(a)*A(b)*A(a)) == a*b*a
True
"""
return self(self.expansion(u) * self.expansion(v))
def antipode(self, elt):
"""
Return the antipode of the element ``elt``.
EXAMPLES::
sage: A = ShuffleAlgebra(QQ, 'ab')
sage: S = A.dual_pbw_basis()
sage: w = S('abaab').antipode(); w
S[word: abaab] - 2*S[word: ababa] - S[word: baaba]
+ 3*S[word: babaa] - 6*S[word: bbaaa]
sage: w.antipode()
S[word: abaab]
"""
return self(self.expansion(elt).antipode())
def coproduct(self, elt):
"""
Return the coproduct of the element ``elt``.
EXAMPLES::
sage: A = ShuffleAlgebra(QQ, 'ab')
sage: S = A.dual_pbw_basis()
sage: S('ab').coproduct()
S[word: ] # S[word: ab] + S[word: a] # S[word: b]
+ S[word: ab] # S[word: ]
sage: S('ba').coproduct()
S[word: ] # S[word: ba] + S[word: a] # S[word: b]
+ S[word: b] # S[word: a] + S[word: ba] # S[word: ]
TESTS::
sage: all(A.tensor_square()(S(x).coproduct()) == x.coproduct()
....: for x in A.some_elements())
True
sage: all(S.tensor_square()(A(x).coproduct()) == x.coproduct()
....: for x in S.some_elements())
True
"""
return self.tensor_square()(self.expansion(elt).coproduct())
def degree_on_basis(self, w):
"""
Return the degree of the element ``w``.
EXAMPLES::
sage: S = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: [S.degree_on_basis(x.leading_support()) for x in S.some_elements() if x != 0]
[0, 1, 1, 2]
"""
return ZZ(len(w))
@lazy_attribute
def expansion(self):
"""
Return the morphism corresponding to the expansion into words of
the shuffle algebra.
EXAMPLES::
sage: S = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: f = S('ab') + S('aba')
sage: S.expansion(f)
2*B[word: aab] + B[word: ab] + B[word: aba]
"""
return self.module_morphism(self.expansion_on_basis, codomain=self._alg)
@cached_method
def expansion_on_basis(self, w):
r"""
Return the expansion of `S_w` in words of the shuffle algebra.
INPUT:
- ``w`` -- a word
EXAMPLES::
sage: S = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: S.expansion_on_basis(Word())
B[word: ]
sage: S.expansion_on_basis(Word()).parent()
Shuffle Algebra on 2 generators ['a', 'b'] over Rational Field
sage: S.expansion_on_basis(Word('abba'))
2*B[word: aabb] + B[word: abab] + B[word: abba]
sage: S.expansion_on_basis(Word())
B[word: ]
sage: S.expansion_on_basis(Word('abab'))
2*B[word: aabb] + B[word: abab]
"""
from sage.functions.other import factorial
if not w:
return self._alg.one()
if len(w) == 1:
return self._alg.monomial(w)
if w.is_lyndon():
W = self.basis().keys()
letter = W(w[0])
expansion = self.expansion_on_basis(W(w[1:]))
return self._alg.sum_of_terms((letter * i, c)
for i, c in expansion)
lf = w.lyndon_factorization()
powers = {}
for i in lf:
powers[i] = powers.get(i, 0) + 1
denom = prod(factorial(p) for p in powers.values())
result = self._alg.prod(self.expansion_on_basis(i) for i in lf)
return self._alg(result / denom)
class Element(CombinatorialFreeModule.Element):
"""
An element in the dual PBW basis.
"""
def expand(self):
"""
Expand ``self`` in words of the shuffle algebra.
EXAMPLES::
sage: S = ShuffleAlgebra(QQ, 'ab').dual_pbw_basis()
sage: f = S('ab') + S('bab')
sage: f.expand()
B[word: ab] + 2*B[word: abb] + B[word: bab]
"""
return self.parent().expansion(self)