Skip to content

Commit 78f50ec

Browse files
author
Release Manager
committed
gh-36155: random elt for colored permutations this fixes #34925 ### 📝 Checklist - [x] The title is concise, informative, and self-explanatory. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation accordingly. URL: #36155 Reported by: Frédéric Chapoton Reviewer(s): Travis Scrimshaw
2 parents 24de879 + 87d1cf9 commit 78f50ec

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

src/sage/combinat/colored_permutations.py

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
generalized to `G \wr S_n`
88
"""
99
import itertools
10+
from random import choice
1011

1112
from sage.structure.element import MultiplicativeGroupElement
1213
from sage.structure.parent import Parent
@@ -313,13 +314,13 @@ def has_left_descent(self, i):
313314
[False, True, False, True]
314315
"""
315316
if self.parent()._m == 1:
316-
return self._perm[i-1] > self._perm[i]
317+
return self._perm[i - 1] > self._perm[i]
317318

318319
if i == self.parent()._n:
319320
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]
323324

324325
def reduced_word(self):
325326
r"""
@@ -563,7 +564,7 @@ def coxeter_matrix(self):
563564
"""
564565
from sage.combinat.root_system.cartan_type import CartanType
565566
if self._m == 1:
566-
return CartanType(['A', self._n-1]).coxeter_matrix()
567+
return CartanType(['A', self._n - 1]).coxeter_matrix()
567568
return CartanType(['B', self._n]).coxeter_matrix()
568569

569570
@cached_method
@@ -580,6 +581,23 @@ def one(self):
580581
return self.element_class(self, [self._C.zero()] * self._n,
581582
self._P.identity())
582583

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+
583601
def simple_reflection(self, i):
584602
r"""
585603
Return the ``i``-th simple reflection of ``self``.
@@ -883,7 +901,7 @@ def codegrees(self):
883901
True
884902
"""
885903
# 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
887905
return tuple(self._m * i for i in reversed(range(last)))
888906

889907
def number_of_reflection_hyperplanes(self):
@@ -979,8 +997,9 @@ def is_well_generated(self):
979997

980998
Element = ColoredPermutation
981999

1000+
9821001
#####################################################################
983-
## Signed permutations
1002+
# Signed permutations
9841003

9851004

9861005
class SignedPermutation(ColoredPermutation,
@@ -1122,11 +1141,11 @@ def __call__(self, i):
11221141
if i in ZZ and 1 <= abs(i) <= len(self):
11231142
i = ZZ(i)
11241143
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)))
11301149

11311150
def to_matrix(self):
11321151
"""
@@ -1218,8 +1237,8 @@ def to_cycles(self, singletons=True, use_min=True, negative_singletons=True):
12181237
s = self._colors[i]
12191238
while next_val != cycle_first:
12201239
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]
12231242
if s != 1:
12241243
cycle.extend([-e for e in cycle])
12251244

@@ -1336,6 +1355,23 @@ def one(self):
13361355
return self.element_class(self, [ZZ.one()] * self._n,
13371356
self._P.identity())
13381357

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+
13391375
def simple_reflection(self, i):
13401376
r"""
13411377
Return the ``i``-th simple reflection of ``self``.
@@ -1454,7 +1490,7 @@ def _coerce_map_from_(self, C):
14541490
False
14551491
"""
14561492
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)
14581494
if isinstance(C, ColoredPermutations) and C._n == self._n and C._m == 2:
14591495
return lambda P, x: P.element_class(P,
14601496
[1 if v == 0 else -1
@@ -1496,7 +1532,7 @@ def long_element(self, index_set=None):
14961532
Element = SignedPermutation
14971533

14981534
# TODO: Make this a subgroup
1499-
#class EvenSignedPermutations(SignedPermutations):
1535+
# class EvenSignedPermutations(SignedPermutations):
15001536
# """
15011537
# Group of even signed permutations.
15021538
# """

0 commit comments

Comments
 (0)