SignedPermutation should allow iterables as input#34974
SignedPermutation should allow iterables as input#34974vbraun merged 6 commits intosagemath:developfrom
Conversation
|
I don't understand the reason for the |
|
Ping? |
|
Sorry sir, I was away for some time. Actually I have used try and except just to point error at that moment only rather than calling error on further calls, but yes it is definitely not necessary. |
Codecov ReportBase: 88.60% // Head: 88.59% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## develop #34974 +/- ##
===========================================
- Coverage 88.60% 88.59% -0.01%
===========================================
Files 2136 2140 +4
Lines 396142 396963 +821
===========================================
+ Hits 350990 351698 +708
- Misses 45152 45265 +113
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
|
So, could you please fix the pull request? Please make sure that and both work. I don't think you should catch the exception. BTW, there is a bug in The code in |
|
It makes no sense to generate an iterator that you never use. It also does not fix the underlying problem. You need to change the |
Sir, but doesn't that applying just the list function beforehand will also raise the same issue of giving an exception in |
Sorry sir, but I didn't well understood your point and what needs to be joined. |
|
All you are doing is hiding the problem instead of actually fixing it by changing the |
|
Ok, I got your point, let me look into it more thoroughly |
f69a3b0 to
ef68b89
Compare
|
Thank You @tscrim and @mantepse for your detailed review, I was going in wrong direction, there is small big in Please look at my changes, and please tell if there anything else needs to be done. |
|
Sorry, no, this is not the correct fix. Not every iterable is a range object |
|
Try casting the input to a |
|
That won't work because an iterable is not something that is guaranteed to have a |
|
I have checked the type of input, it is same as we passing, that is "range". Although I have tried converting the input to list, but it is throwing some error on that, so thought of this. |
|
What I am saying is very simple code that would go like this for input A similar change should be done for |
May I ask why we have to check if |
Speed. You don't need to do anything to the input if it is already an element of |
|
Thank you @tscrim for your reply, I have made the necessary changes. Please tell if there any other changes to be done. |
tscrim
left a comment
There was a problem hiding this comment.
You also need to add doctests showing that the issue is fixed. Not only with range, but also a tuple and a signed/colored permutation.
|
@tscrim sir, I have added doctests, please tell if there is anything that I have missed, or else that needs to be done. |
|
You also need to add doctests for colored permutations. |
|
@tscrim Sir, I have tested colored permutations that whenever we passed |
This isn't always the case. It should be, but I have come across this from time to time. Please listen to people with some experience. The other thing that should be done is signed permutations should handle 2-colored permutations as a special case and vice versa.
You should add a doctest for signed and colored permutations as input as well, as I said previously.
This makes no sense. |
Actually, this is not necessary because of the coercion map. Although only one of them should be a coercion map, the other should be a conversion through |
Sorry sir for my mistake. As during testing, I was unable to find any case where
So does it mean that I don't have to add anything as one of the argument is handled through coercion map and other by |
No, nothing needs to be added for these cases. |
ba4165f to
d0ceeed
Compare
|
Documentation preview for this PR is ready! 🎉 |
Thank you, both! I only see two extremely minor possible improvements (i.e., I am not sure whether they actually would be improvements), which certainly should not hold up the ticket, so I only mention them here. My next, more real, concern is #34925 (comment). One remark: the error messages are apparently not tested. diff --git a/src/sage/combinat/colored_permutations.py b/src/sage/combinat/colored_permutations.py
index 28b20e502af..0ef0575a36d 100644
--- a/src/sage/combinat/colored_permutations.py
+++ b/src/sage/combinat/colored_permutations.py
@@ -436,7 +436,7 @@ class ColoredPermutations(Parent, UniqueRepresentation):
[[0, 1, 0], [1, 2, 3]]
We can also create a colored permutation by passing
- an iterable consisting of tuples consisting of ``(color, element)``::
+ an iterable consisting of tuples ``(color, element)``::
sage: x = C([(2,1), (3,3), (3,2)]); x
[[2, 3, 3], [1, 3, 2]]
@@ -685,8 +685,8 @@ class ColoredPermutations(Parent, UniqueRepresentation):
INPUT:
- Either a list of pairs (color, element)
- or a pair of lists (colors, elements).
+ Either a list of pairs ``(color, element)``
+ or a pair of lists ``(colors, elements)``.
TESTS::
@@ -700,13 +700,11 @@ class ColoredPermutations(Parent, UniqueRepresentation):
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])
+ try:
+ c = [self._C(c) for c, _ in x]
+ p = [e for _, e in x]
+ except ValueError:
+ raise ValueError("input must be pairs (color, element)")
return self.element_class(self, c, self._P(p))
if len(x) != 2:
@@ -1383,28 +1381,19 @@ class SignedPermutations(ColoredPermutations):
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])
+ try:
+ c = [ZZ(e) for e, _ in x]
+ except ValueError:
+ raise ValueError("input must be pairs (sign, element)")
+ if any(e != 1 and e != -1 for e in c):
+ raise ValueError("the sign must be +1 or -1")
+ p = [e for _, e in x]
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)
+ c = [one if e > 0 else -one for e in x]
+ p = [e if e > 0 else -e for e in x]
return self.element_class(self, c, self._P(p))
if len(x) != 2: |
|
@mantepse sir, so should I make these above changes ? |
I leave that decision to Travis. |
|
@mantepse I will take care of it on a followup PR. I see some other improvements that I want to do anyways (e.g., having only one coercion map and making the other a conversion). |
This is my pull request originally which addresses the issue #34923
Fixes #34923