-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Patch of itertools.{combinations,permutations} for empty combinations #49066
Comments
This is a patch for the Python 3.1 build checked out from The current behavior of itertools.combinations(iterable,r) and As for my argument for acceptance, while the original behavior is not a In mathematics the "choose" function is defined as "(n choose r)=0" for For functionality I'll cite my own case as anecdote. In writing all(triangle_respected(*triple) for triple in If len(group)<=2, that's fine, since with no triangles there is no The patch modifies Modules/itertoolsmodule.c slightly, changing Obviously, this would break code that depends upon Python throwing Sorry if this belongs in a PEP -- it seems quite minor, but I don't |
I agree that the proposed behaviour seems more correct: the collection of I guess the counterargument is that the current ValueError might catch In any case, this is Raymond's call. |
Will spend a while mulling this over and taking it under advisement. Initially, I am disinclined for several reasons.
"""
For the case where r = n it has just been shown that P(n, r) = n!. The
""" That discussion is typical and I think it important the number of items Also see the wikipedia article on combinations (the "choose" function)
or if you factor-out the unvarying constant expression in the inner-loop: len(group)>=3 or all(triangle_respected(*triple) for triple in For other cases, it may be preferable to write your own wrapper: def myperm(pool, r=None):
'custom version of perm returning an empty iterator when r>n'
if r is not None and r > len(pool):
return iter([])
return itertools.permutations(pool, r) I like this because it is explicit about its intended behavior but
On the flip side, I do like Mark's thought that the r>n case being empty Those are my initial thoughts. Will ponder it a bit more this week. |
Quick notes so I don't forget: The two main pure python equivalents in the docs would need to be The Mathematica definition of permutations does not discuss or allow for |
Hi Raymond, thanks for your well reasoned and thorough reply. Just a
The closest I can think of right now might be key/index errors on dicts In a larger sense, if a result computed by a function is sensible and
Hmmm. Are you looking at the definition of "choose function" under
Saying the fix is more explicit suggests there's something implicit
A third and extraneous comment, if you do wind up changing the docs
|
In the math module, it is a virtue that math.sqrt(-1) raises a
BTW, we don't really have a difference of opinion. My mind is open. I In choosing, there is some bias toward sticking the API as it was Am still thinking this one through and will let you know in a week or |
Another thought before I forget: The piecewise definition of the choose |
IIRC, in the 'Concrete Mathematics' book, Knuth and co use something (n choose k) = (n-to-the-k-falling)/k! to get around this: this definition works for all k >= 0 and all http://mathworld.wolfram.com/FallingFactorial.html
I'd say not: in the context of sets of combinations/permutations, (If we were talking about the integer-valued function nCr, then I'd My own guess would be that having combinations(range(n), r) I have access to the Magma algebra package at work; I'll |
Mathematica returns an empty list. In[1]:= Permutations[{1,2},{1}] Out[1]= {{1}, {2}} In[2]:= Permutations[{1,2},{4}] Out[2]= {} In[3]:= |
David, thanks for the data point. What does it return for In[1]:= Permutations[{a, b, c}, {-1}] |
Mathematica indicates for the user to define it later. An error. In[3]:= Permutations[{1,2},{-2}] Permutations::nninfseq: Out[4]= Permutations[{1, 2}, {-2}] |
Results from Magma, Sage and Gap: Magma provides functions "Subsets(S, k)" and "Permutations(S, k)". From Subsets(S, k) : The set of subsets of the set S of size k. If k is Permutations(S, k) : The set of permutations (stored as sequences) of GAP has the same behaviour: even if you've never encountered GAP before, gap> Combinations([1,2,3,4], 3); Permutations work the same way (but the function is called gap> Arrangements([1,2,3,4], 3); Combinations([1,2,3,4], -1) gives an error. Interestingly, GAP also has a NrCombinations function returning the number of gap> NrCombinations([1,2,3,4], 5); My Sage installation currently seems to be broken, but from the |
Got Sage working again. It also returns an empty list for r > n. For r sage: Combinations(range(4), 6) IndexError Traceback (most recent call last)
[... traceback snipped ...] |
Data point: Microsoft Excel's PERMUT() and COMBIN() return #NUM! for |
I try to "not know" excel. Does it have any other means to represent an |
Excel is returning the count, not the set itself. So, it could have The HP32SII also has nCr and nPr functions that return INVALID DATA for The Combinatorics section of CRC Standard Mathematical Formulae (30th |
Summary of functions/definitions expected to return a number: r>n r<0 Source Summary for tools that return the individual subsets: r>n r<0 Source |
David, I know the OPs position and Mark's opinion. What is your |
Attached an updated patch which expands the tests and docs. Am thinking that if we do this, it should be treated as a bug and posted |
I had thought highly of the "mull it over for a week" plan. After a |
Thanks David. Looks like we're all in agreement.
Weighing against it:
|
Fixed in r68394 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: