7
7
generalized to `G \wr S_n`
8
8
"""
9
9
import itertools
10
+ from random import choice
10
11
11
12
from sage .structure .element import MultiplicativeGroupElement
12
13
from sage .structure .parent import Parent
@@ -313,13 +314,13 @@ def has_left_descent(self, i):
313
314
[False, True, False, True]
314
315
"""
315
316
if self .parent ()._m == 1 :
316
- return self ._perm [i - 1 ] > self ._perm [i ]
317
+ return self ._perm [i - 1 ] > self ._perm [i ]
317
318
318
319
if i == self .parent ()._n :
319
320
return self ._colors [- 1 ] != 0
320
- if self ._colors [i - 1 ] != 0 :
321
- return self ._colors [i ] == 0 or self ._perm [i - 1 ] < self ._perm [i ]
322
- return self ._colors [i ] == 0 and self ._perm [i - 1 ] > self ._perm [i ]
321
+ if self ._colors [i - 1 ] != 0 :
322
+ return self ._colors [i ] == 0 or self ._perm [i - 1 ] < self ._perm [i ]
323
+ return self ._colors [i ] == 0 and self ._perm [i - 1 ] > self ._perm [i ]
323
324
324
325
def reduced_word (self ):
325
326
r"""
@@ -563,7 +564,7 @@ def coxeter_matrix(self):
563
564
"""
564
565
from sage .combinat .root_system .cartan_type import CartanType
565
566
if self ._m == 1 :
566
- return CartanType (['A' , self ._n - 1 ]).coxeter_matrix ()
567
+ return CartanType (['A' , self ._n - 1 ]).coxeter_matrix ()
567
568
return CartanType (['B' , self ._n ]).coxeter_matrix ()
568
569
569
570
@cached_method
@@ -580,6 +581,23 @@ def one(self):
580
581
return self .element_class (self , [self ._C .zero ()] * self ._n ,
581
582
self ._P .identity ())
582
583
584
+ def random_element (self ):
585
+ """
586
+ Return an element drawn uniformly at random.
587
+
588
+ EXAMPLES::
589
+
590
+ sage: C = ColoredPermutations(4, 3)
591
+ sage: s = C.random_element(); s # random
592
+ [[0, 2, 1], [2, 1, 3]]
593
+ sage: s in C
594
+ True
595
+ """
596
+ return self .element_class (self ,
597
+ [self ._C .random_element ()
598
+ for _ in range (self ._n )],
599
+ self ._P .random_element ())
600
+
583
601
def simple_reflection (self , i ):
584
602
r"""
585
603
Return the ``i``-th simple reflection of ``self``.
@@ -883,7 +901,7 @@ def codegrees(self):
883
901
True
884
902
"""
885
903
# Special case for the usual symmetric group
886
- last = self ._n - 1 if self ._m == 1 else self ._n
904
+ last = self ._n - 1 if self ._m == 1 else self ._n
887
905
return tuple (self ._m * i for i in reversed (range (last )))
888
906
889
907
def number_of_reflection_hyperplanes (self ):
@@ -979,8 +997,9 @@ def is_well_generated(self):
979
997
980
998
Element = ColoredPermutation
981
999
1000
+
982
1001
#####################################################################
983
- ## Signed permutations
1002
+ # Signed permutations
984
1003
985
1004
986
1005
class SignedPermutation (ColoredPermutation ,
@@ -1122,11 +1141,11 @@ def __call__(self, i):
1122
1141
if i in ZZ and 1 <= abs (i ) <= len (self ):
1123
1142
i = ZZ (i )
1124
1143
if i < 0 :
1125
- return - self ._colors [- i - 1 ] * self ._perm [- i - 1 ]
1126
- return self ._colors [i - 1 ] * self ._perm [i - 1 ]
1127
- else :
1128
- raise TypeError ("i (= %s) must equal +/- an integer between %s and %s"
1129
- % (i ,1 , len (self )))
1144
+ return - self ._colors [- i - 1 ] * self ._perm [- i - 1 ]
1145
+ return self ._colors [i - 1 ] * self ._perm [i - 1 ]
1146
+
1147
+ raise TypeError ("i (= %s) must equal +/- an integer between %s and %s"
1148
+ % (i , 1 , len (self )))
1130
1149
1131
1150
def to_matrix (self ):
1132
1151
"""
@@ -1218,8 +1237,8 @@ def to_cycles(self, singletons=True, use_min=True, negative_singletons=True):
1218
1237
s = self ._colors [i ]
1219
1238
while next_val != cycle_first :
1220
1239
cycle .append (s * next_val )
1221
- s *= self ._colors [next_val - 1 ]
1222
- l [next_val - 1 ], next_val = False , l [next_val - 1 ]
1240
+ s *= self ._colors [next_val - 1 ]
1241
+ l [next_val - 1 ], next_val = False , l [next_val - 1 ]
1223
1242
if s != 1 :
1224
1243
cycle .extend ([- e for e in cycle ])
1225
1244
@@ -1336,6 +1355,23 @@ def one(self):
1336
1355
return self .element_class (self , [ZZ .one ()] * self ._n ,
1337
1356
self ._P .identity ())
1338
1357
1358
+ def random_element (self ):
1359
+ """
1360
+ Return an element drawn uniformly at random.
1361
+
1362
+ EXAMPLES::
1363
+
1364
+ sage: C = SignedPermutations(7)
1365
+ sage: s = C.random_element(); s # random
1366
+ [7, 6, -4, -5, 2, 3, -1]
1367
+ sage: s in C
1368
+ True
1369
+ """
1370
+ return self .element_class (self ,
1371
+ [choice ([ZZ .one (), - ZZ .one ()])
1372
+ for _ in range (self ._n )],
1373
+ self ._P .random_element ())
1374
+
1339
1375
def simple_reflection (self , i ):
1340
1376
r"""
1341
1377
Return the ``i``-th simple reflection of ``self``.
@@ -1454,7 +1490,7 @@ def _coerce_map_from_(self, C):
1454
1490
False
1455
1491
"""
1456
1492
if isinstance (C , Permutations ) and C .n == self ._n :
1457
- return lambda P , x : P .element_class (P , [1 ]* P ._n , x )
1493
+ return lambda P , x : P .element_class (P , [1 ] * P ._n , x )
1458
1494
if isinstance (C , ColoredPermutations ) and C ._n == self ._n and C ._m == 2 :
1459
1495
return lambda P , x : P .element_class (P ,
1460
1496
[1 if v == 0 else - 1
@@ -1496,7 +1532,7 @@ def long_element(self, index_set=None):
1496
1532
Element = SignedPermutation
1497
1533
1498
1534
# TODO: Make this a subgroup
1499
- #class EvenSignedPermutations(SignedPermutations):
1535
+ # class EvenSignedPermutations(SignedPermutations):
1500
1536
# """
1501
1537
# Group of even signed permutations.
1502
1538
# """
0 commit comments