Skip to content

Commit b3ca60a

Browse files
author
Release Manager
committed
gh-pr-34974: SignedPermutation should allow iterables as input This is my pull request originally which addresses the issue #34923 Fixes #34923 URL: #34974 Reported by: Rohan Garg Reviewer(s): Travis Scrimshaw
2 parents 43fcd94 + 0e9d4fc commit b3ca60a

File tree

1 file changed

+67
-47
lines changed

1 file changed

+67
-47
lines changed

src/sage/combinat/colored_permutations.py

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ class ColoredPermutations(Parent, UniqueRepresentation):
436436
[[0, 1, 0], [1, 2, 3]]
437437
438438
We can also create a colored permutation by passing
439-
either a list of tuples consisting of ``(color, element)``::
439+
an iterable consisting of tuples consisting of ``(color, element)``::
440440
441441
sage: x = C([(2,1), (3,3), (3,2)]); x
442442
[[2, 3, 3], [1, 3, 2]]
@@ -445,13 +445,21 @@ class ColoredPermutations(Parent, UniqueRepresentation):
445445
446446
sage: C([[3,3,1], [1,3,2]])
447447
[[3, 3, 1], [1, 3, 2]]
448+
sage: C(([3,3,1], [1,3,2]))
449+
[[3, 3, 1], [1, 3, 2]]
448450
449451
There is also the natural lift from permutations::
450452
451453
sage: P = Permutations(3)
452454
sage: C(P.an_element())
453455
[[0, 0, 0], [3, 1, 2]]
454456
457+
458+
A colored permutation::
459+
460+
sage: C(C.an_element()) == C.an_element()
461+
True
462+
455463
REFERENCES:
456464
457465
- :wikipedia:`Generalized_symmetric_group`
@@ -688,20 +696,22 @@ def _element_constructor_(self, x):
688696
sage: x == C([[2,3,3], [1,3,2]])
689697
True
690698
"""
691-
if isinstance(x, list):
692-
if isinstance(x[0], tuple):
693-
c = []
694-
p = []
695-
for k in x:
696-
if len(k) != 2:
697-
raise ValueError("input must be pairs (color, element)")
698-
c.append(self._C(k[0]))
699-
p.append(k[1])
700-
return self.element_class(self, c, self._P(p))
701-
702-
if len(x) != 2:
703-
raise ValueError("input must be a pair of a list of colors and a permutation")
704-
return self.element_class(self, [self._C(v) for v in x[0]], self._P(x[1]))
699+
if isinstance(x, self.element_class) and x.parent() is self:
700+
return self
701+
x = list(x)
702+
if isinstance(x[0], tuple):
703+
c = []
704+
p = []
705+
for k in x:
706+
if len(k) != 2:
707+
raise ValueError("input must be pairs (color, element)")
708+
c.append(self._C(k[0]))
709+
p.append(k[1])
710+
return self.element_class(self, c, self._P(p))
711+
712+
if len(x) != 2:
713+
raise ValueError("input must be a pair of a list of colors and a permutation")
714+
return self.element_class(self, [self._C(v) for v in x[0]], self._P(x[1]))
705715

706716
def _coerce_map_from_(self, C):
707717
"""
@@ -989,6 +999,11 @@ def __classcall_private__(cls, pi):
989999
sage: SignedPermutation([2, 1, -3])
9901000
[2, 1, -3]
9911001
1002+
sage: SignedPermutation((2,1,-3))
1003+
[2, 1, -3]
1004+
1005+
sage: SignedPermutation(range(1,4))
1006+
[1, 2, 3]
9921007
"""
9931008
return SignedPermutations(len(list(pi)))(pi)
9941009

@@ -1360,38 +1375,43 @@ def _element_constructor_(self, x):
13601375
sage: S([]) == list(S)[0]
13611376
True
13621377
1363-
"""
1364-
if isinstance(x, list):
1365-
if x and isinstance(x[0], tuple):
1366-
c = []
1367-
p = []
1368-
for k in x:
1369-
if len(k) != 2:
1370-
raise ValueError("input must be pairs (sign, element)")
1371-
if k[0] != 1 and k[0] != -1:
1372-
raise ValueError("the sign must be +1 or -1")
1373-
c.append(ZZ(k[0]))
1374-
p.append(k[1])
1375-
return self.element_class(self, c, self._P(p))
1376-
1377-
if len(x) == self._n:
1378-
c = []
1379-
p = []
1380-
one = ZZ.one()
1381-
for v in x:
1382-
if v > 0:
1383-
c.append(one)
1384-
p.append(v)
1385-
else:
1386-
c.append(-one)
1387-
p.append(-v)
1388-
return self.element_class(self, c, self._P(p))
1389-
1390-
if len(x) != 2:
1391-
raise ValueError("input must be a pair of a list of signs and a permutation")
1392-
if any(s != 1 and s != -1 for s in x[0]):
1393-
raise ValueError("the sign must be +1 or -1")
1394-
return self.element_class(self, [ZZ(v) for v in x[0]], self._P(x[1]))
1378+
sage: T = SignedPermutation(range(1,4))
1379+
sage: SignedPermutations(3)(T)
1380+
[1, 2, 3]
1381+
"""
1382+
if isinstance(x, self.element_class) and x.parent() is self:
1383+
return self
1384+
x = list(x)
1385+
if x and isinstance(x[0], tuple):
1386+
c = []
1387+
p = []
1388+
for k in x:
1389+
if len(k) != 2:
1390+
raise ValueError("input must be pairs (sign, element)")
1391+
if k[0] != 1 and k[0] != -1:
1392+
raise ValueError("the sign must be +1 or -1")
1393+
c.append(ZZ(k[0]))
1394+
p.append(k[1])
1395+
return self.element_class(self, c, self._P(p))
1396+
1397+
if len(x) == self._n:
1398+
c = []
1399+
p = []
1400+
one = ZZ.one()
1401+
for v in x:
1402+
if v > 0:
1403+
c.append(one)
1404+
p.append(v)
1405+
else:
1406+
c.append(-one)
1407+
p.append(-v)
1408+
return self.element_class(self, c, self._P(p))
1409+
1410+
if len(x) != 2:
1411+
raise ValueError("input must be a pair of a list of signs and a permutation")
1412+
if any(s != 1 and s != -1 for s in x[0]):
1413+
raise ValueError("the sign must be +1 or -1")
1414+
return self.element_class(self, [ZZ(v) for v in x[0]], self._P(x[1]))
13951415

13961416
def __iter__(self):
13971417
"""

0 commit comments

Comments
 (0)