Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SignedPermutation should allow iterables as input #34974

Merged
merged 6 commits into from
Feb 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 67 additions & 47 deletions src/sage/combinat/colored_permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ class ColoredPermutations(Parent, UniqueRepresentation):
[[0, 1, 0], [1, 2, 3]]

We can also create a colored permutation by passing
either a list of tuples consisting of ``(color, element)``::
an iterable consisting of tuples consisting of ``(color, element)``::

sage: x = C([(2,1), (3,3), (3,2)]); x
[[2, 3, 3], [1, 3, 2]]
Expand All @@ -445,13 +445,21 @@ class ColoredPermutations(Parent, UniqueRepresentation):

sage: C([[3,3,1], [1,3,2]])
[[3, 3, 1], [1, 3, 2]]
sage: C(([3,3,1], [1,3,2]))
[[3, 3, 1], [1, 3, 2]]

There is also the natural lift from permutations::

sage: P = Permutations(3)
sage: C(P.an_element())
[[0, 0, 0], [3, 1, 2]]


A colored permutation::

sage: C(C.an_element()) == C.an_element()
True

REFERENCES:

- :wikipedia:`Generalized_symmetric_group`
Expand Down Expand Up @@ -688,20 +696,22 @@ def _element_constructor_(self, x):
sage: x == C([[2,3,3], [1,3,2]])
True
"""
if isinstance(x, list):
if isinstance(x[0], tuple):
c = []
p = []
for k in x:
if len(k) != 2:
raise ValueError("input must be pairs (color, element)")
c.append(self._C(k[0]))
p.append(k[1])
return self.element_class(self, c, self._P(p))

if len(x) != 2:
raise ValueError("input must be a pair of a list of colors and a permutation")
return self.element_class(self, [self._C(v) for v in x[0]], self._P(x[1]))
if isinstance(x, self.element_class) and x.parent() is self:
return self
x = list(x)
if isinstance(x[0], tuple):
c = []
p = []
for k in x:
if len(k) != 2:
raise ValueError("input must be pairs (color, element)")
c.append(self._C(k[0]))
p.append(k[1])
return self.element_class(self, c, self._P(p))

if len(x) != 2:
raise ValueError("input must be a pair of a list of colors and a permutation")
return self.element_class(self, [self._C(v) for v in x[0]], self._P(x[1]))

def _coerce_map_from_(self, C):
"""
Expand Down Expand Up @@ -989,6 +999,11 @@ def __classcall_private__(cls, pi):
sage: SignedPermutation([2, 1, -3])
[2, 1, -3]

sage: SignedPermutation((2,1,-3))
[2, 1, -3]

sage: SignedPermutation(range(1,4))
[1, 2, 3]
"""
return SignedPermutations(len(list(pi)))(pi)

Expand Down Expand Up @@ -1360,38 +1375,43 @@ def _element_constructor_(self, x):
sage: S([]) == list(S)[0]
True

"""
if isinstance(x, list):
if x and isinstance(x[0], tuple):
c = []
p = []
for k in x:
if len(k) != 2:
raise ValueError("input must be pairs (sign, element)")
if k[0] != 1 and k[0] != -1:
raise ValueError("the sign must be +1 or -1")
c.append(ZZ(k[0]))
p.append(k[1])
return self.element_class(self, c, self._P(p))

if len(x) == self._n:
c = []
p = []
one = ZZ.one()
for v in x:
if v > 0:
c.append(one)
p.append(v)
else:
c.append(-one)
p.append(-v)
return self.element_class(self, c, self._P(p))

if len(x) != 2:
raise ValueError("input must be a pair of a list of signs and a permutation")
if any(s != 1 and s != -1 for s in x[0]):
raise ValueError("the sign must be +1 or -1")
return self.element_class(self, [ZZ(v) for v in x[0]], self._P(x[1]))
sage: T = SignedPermutation(range(1,4))
sage: SignedPermutations(3)(T)
[1, 2, 3]
"""
if isinstance(x, self.element_class) and x.parent() is self:
return self
x = list(x)
if x and isinstance(x[0], tuple):
c = []
p = []
for k in x:
if len(k) != 2:
raise ValueError("input must be pairs (sign, element)")
if k[0] != 1 and k[0] != -1:
raise ValueError("the sign must be +1 or -1")
c.append(ZZ(k[0]))
p.append(k[1])
return self.element_class(self, c, self._P(p))

if len(x) == self._n:
c = []
p = []
one = ZZ.one()
for v in x:
if v > 0:
c.append(one)
p.append(v)
else:
c.append(-one)
p.append(-v)
return self.element_class(self, c, self._P(p))

if len(x) != 2:
raise ValueError("input must be a pair of a list of signs and a permutation")
if any(s != 1 and s != -1 for s in x[0]):
raise ValueError("the sign must be +1 or -1")
return self.element_class(self, [ZZ(v) for v in x[0]], self._P(x[1]))

def __iter__(self):
"""
Expand Down