Skip to content

Commit 3a0475f

Browse files
committed
converting iterables
1 parent 3ce4a62 commit 3a0475f

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

src/sage/combinat/colored_permutations.py

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -689,20 +689,22 @@ def _element_constructor_(self, x):
689689
sage: x == C([[2,3,3], [1,3,2]])
690690
True
691691
"""
692-
if isinstance(x, list):
693-
if isinstance(x[0], tuple):
694-
c = []
695-
p = []
696-
for k in x:
697-
if len(k) != 2:
698-
raise ValueError("input must be pairs (color, element)")
699-
c.append(self._C(k[0]))
700-
p.append(k[1])
701-
return self.element_class(self, c, self._P(p))
702-
703-
if len(x) != 2:
704-
raise ValueError("input must be a pair of a list of colors and a permutation")
705-
return self.element_class(self, [self._C(v) for v in x[0]], self._P(x[1]))
692+
if isinstance(x, self.element_class) and x.parent() is self:
693+
return self
694+
x = list(x)
695+
if isinstance(x[0], tuple):
696+
c = []
697+
p = []
698+
for k in x:
699+
if len(k) != 2:
700+
raise ValueError("input must be pairs (color, element)")
701+
c.append(self._C(k[0]))
702+
p.append(k[1])
703+
return self.element_class(self, c, self._P(p))
704+
705+
if len(x) != 2:
706+
raise ValueError("input must be a pair of a list of colors and a permutation")
707+
return self.element_class(self, [self._C(v) for v in x[0]], self._P(x[1]))
706708

707709
def _coerce_map_from_(self, C):
708710
"""
@@ -1362,37 +1364,39 @@ def _element_constructor_(self, x):
13621364
True
13631365
13641366
"""
1365-
if isinstance(x, Iterable):
1366-
if x and isinstance(x[0], tuple):
1367-
c = []
1368-
p = []
1369-
for k in x:
1370-
if len(k) != 2:
1371-
raise ValueError("input must be pairs (sign, element)")
1372-
if k[0] != 1 and k[0] != -1:
1373-
raise ValueError("the sign must be +1 or -1")
1374-
c.append(ZZ(k[0]))
1375-
p.append(k[1])
1376-
return self.element_class(self, c, self._P(p))
1377-
1378-
if len(x) == self._n:
1379-
c = []
1380-
p = []
1381-
one = ZZ.one()
1382-
for v in x:
1383-
if v > 0:
1384-
c.append(one)
1385-
p.append(v)
1386-
else:
1387-
c.append(-one)
1388-
p.append(-v)
1389-
return self.element_class(self, c, self._P(p))
1390-
1391-
if len(x) != 2:
1392-
raise ValueError("input must be a pair of a list of signs and a permutation")
1393-
if any(s != 1 and s != -1 for s in x[0]):
1394-
raise ValueError("the sign must be +1 or -1")
1395-
return self.element_class(self, [ZZ(v) for v in x[0]], self._P(x[1]))
1367+
if isinstance(x, self.element_class) and x.parent() is self:
1368+
return self
1369+
x = list(x)
1370+
if x and isinstance(x[0], tuple):
1371+
c = []
1372+
p = []
1373+
for k in x:
1374+
if len(k) != 2:
1375+
raise ValueError("input must be pairs (sign, element)")
1376+
if k[0] != 1 and k[0] != -1:
1377+
raise ValueError("the sign must be +1 or -1")
1378+
c.append(ZZ(k[0]))
1379+
p.append(k[1])
1380+
return self.element_class(self, c, self._P(p))
1381+
1382+
if len(x) == self._n:
1383+
c = []
1384+
p = []
1385+
one = ZZ.one()
1386+
for v in x:
1387+
if v > 0:
1388+
c.append(one)
1389+
p.append(v)
1390+
else:
1391+
c.append(-one)
1392+
p.append(-v)
1393+
return self.element_class(self, c, self._P(p))
1394+
1395+
if len(x) != 2:
1396+
raise ValueError("input must be a pair of a list of signs and a permutation")
1397+
if any(s != 1 and s != -1 for s in x[0]):
1398+
raise ValueError("the sign must be +1 or -1")
1399+
return self.element_class(self, [ZZ(v) for v in x[0]], self._P(x[1]))
13961400

13971401
def __iter__(self):
13981402
"""

0 commit comments

Comments
 (0)