From ef68b895ca130c3b500bd86813dc9d0a513d1790 Mon Sep 17 00:00:00 2001 From: Sandstorm831 Date: Sun, 12 Feb 2023 23:29:48 +0530 Subject: [PATCH 1/6] allowing iterables as input --- src/sage/combinat/colored_permutations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sage/combinat/colored_permutations.py b/src/sage/combinat/colored_permutations.py index 4ace496fbdb..a725c8c869e 100644 --- a/src/sage/combinat/colored_permutations.py +++ b/src/sage/combinat/colored_permutations.py @@ -1361,7 +1361,7 @@ def _element_constructor_(self, x): True """ - if isinstance(x, list): + if isinstance(x, list) or isinstance(x, range): if x and isinstance(x[0], tuple): c = [] p = [] From 3ce4a625c2df8bdb8562605328889f17fa5c9ba7 Mon Sep 17 00:00:00 2001 From: Sandstorm831 Date: Mon, 13 Feb 2023 18:13:41 +0530 Subject: [PATCH 2/6] generalized fix --- src/sage/combinat/colored_permutations.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sage/combinat/colored_permutations.py b/src/sage/combinat/colored_permutations.py index a725c8c869e..eebfdd072ed 100644 --- a/src/sage/combinat/colored_permutations.py +++ b/src/sage/combinat/colored_permutations.py @@ -8,6 +8,7 @@ """ import itertools +from collections.abc import Iterable from sage.structure.element import MultiplicativeGroupElement from sage.structure.parent import Parent from sage.structure.unique_representation import UniqueRepresentation @@ -1361,7 +1362,7 @@ def _element_constructor_(self, x): True """ - if isinstance(x, list) or isinstance(x, range): + if isinstance(x, Iterable): if x and isinstance(x[0], tuple): c = [] p = [] From 3a0475fcacfbc24676d2c72a6a6c4d234fddab2a Mon Sep 17 00:00:00 2001 From: Sandstorm831 Date: Tue, 14 Feb 2023 19:37:51 +0530 Subject: [PATCH 3/6] converting iterables --- src/sage/combinat/colored_permutations.py | 94 ++++++++++++----------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/src/sage/combinat/colored_permutations.py b/src/sage/combinat/colored_permutations.py index eebfdd072ed..8376f0e481e 100644 --- a/src/sage/combinat/colored_permutations.py +++ b/src/sage/combinat/colored_permutations.py @@ -689,20 +689,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): """ @@ -1362,37 +1364,39 @@ def _element_constructor_(self, x): True """ - if isinstance(x, Iterable): - 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])) + 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): """ From 18e9e4ba8e0bc61cde28e7079bccfd466e892a19 Mon Sep 17 00:00:00 2001 From: Sandstorm831 Date: Wed, 15 Feb 2023 22:38:40 +0530 Subject: [PATCH 4/6] adding doctests --- src/sage/combinat/colored_permutations.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/sage/combinat/colored_permutations.py b/src/sage/combinat/colored_permutations.py index 8376f0e481e..74380ba2320 100644 --- a/src/sage/combinat/colored_permutations.py +++ b/src/sage/combinat/colored_permutations.py @@ -8,7 +8,6 @@ """ import itertools -from collections.abc import Iterable from sage.structure.element import MultiplicativeGroupElement from sage.structure.parent import Parent from sage.structure.unique_representation import UniqueRepresentation @@ -992,6 +991,18 @@ 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] + + TESTS:: + + sage: T = SignedPermutation(range(1,4)); type(T) + + sage: SignedPermutation(T) + [1, 2, 3] """ return SignedPermutations(len(list(pi)))(pi) From d0ceeed2caeaa99ac5340c29e7de8fa8214c04a6 Mon Sep 17 00:00:00 2001 From: Sandstorm831 Date: Fri, 17 Feb 2023 11:44:31 +0530 Subject: [PATCH 5/6] adding colored permutation doctests --- src/sage/combinat/colored_permutations.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/sage/combinat/colored_permutations.py b/src/sage/combinat/colored_permutations.py index 74380ba2320..1c5db846a53 100644 --- a/src/sage/combinat/colored_permutations.py +++ b/src/sage/combinat/colored_permutations.py @@ -435,8 +435,8 @@ class ColoredPermutations(Parent, UniqueRepresentation): sage: s2*t*s2 [[0, 1, 0], [1, 2, 3]] - We can also create a colored permutation by passing - either a list of tuples consisting of ``(color, element)``:: + We can also create a colored permutation by passing either + 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]] @@ -445,6 +445,8 @@ 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:: @@ -452,6 +454,14 @@ class ColoredPermutations(Parent, UniqueRepresentation): sage: C(P.an_element()) [[0, 0, 0], [3, 1, 2]] + + TESTS:: + + sage: T = ColoredPermutations(3,4); type(T[0]) + + sage: C(T[0]) + [[0, 0, 0], [1, 2, 3]] + REFERENCES: - :wikipedia:`Generalized_symmetric_group` From 0e9d4fc48e3af9f832f415d3cde8350abb2f2804 Mon Sep 17 00:00:00 2001 From: Sandstorm831 Date: Fri, 17 Feb 2023 16:48:15 +0530 Subject: [PATCH 6/6] doctests fixes --- src/sage/combinat/colored_permutations.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/sage/combinat/colored_permutations.py b/src/sage/combinat/colored_permutations.py index 1c5db846a53..28b20e502af 100644 --- a/src/sage/combinat/colored_permutations.py +++ b/src/sage/combinat/colored_permutations.py @@ -435,7 +435,7 @@ class ColoredPermutations(Parent, UniqueRepresentation): sage: s2*t*s2 [[0, 1, 0], [1, 2, 3]] - We can also create a colored permutation by passing either + We can also create a colored permutation by passing an iterable consisting of tuples consisting of ``(color, element)``:: sage: x = C([(2,1), (3,3), (3,2)]); x @@ -455,12 +455,10 @@ class ColoredPermutations(Parent, UniqueRepresentation): [[0, 0, 0], [3, 1, 2]] - TESTS:: + A colored permutation:: - sage: T = ColoredPermutations(3,4); type(T[0]) - - sage: C(T[0]) - [[0, 0, 0], [1, 2, 3]] + sage: C(C.an_element()) == C.an_element() + True REFERENCES: @@ -1006,13 +1004,6 @@ def __classcall_private__(cls, pi): sage: SignedPermutation(range(1,4)) [1, 2, 3] - - TESTS:: - - sage: T = SignedPermutation(range(1,4)); type(T) - - sage: SignedPermutation(T) - [1, 2, 3] """ return SignedPermutations(len(list(pi)))(pi) @@ -1384,6 +1375,9 @@ def _element_constructor_(self, x): sage: S([]) == list(S)[0] True + 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