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

Add a perm function into scipy.misc and an enhancement of comb #3094

Closed
cairijun opened this issue Nov 26, 2013 · 6 comments
Closed

Add a perm function into scipy.misc and an enhancement of comb #3094

cairijun opened this issue Nov 26, 2013 · 6 comments
Labels
enhancement A new feature or improvement scipy.special
Milestone

Comments

@cairijun
Copy link
Contributor

scipy.misc has a function comb to compute the numbers of combinations. It may be better to provide a perm function to compute the numbers of permutations.
In addition, we can add an optional parameter repetition to make comb support the numbers of combinations with repetition.
BTW: the docstring says that the type of the exact parameter is int but it should be bool.

@argriffing
Copy link
Contributor

It may be better to provide a perm function to compute the numbers of permutations.

It already has factorial.

@cairijun
Copy link
Contributor Author

I mean the k-permutations of n. Of course it is feasible to compute it with factorial like factorial(N, exact=True) // factorial(N - k, exact=True), but it would be better if we can use perm(N, k, exact=True). Besides, using factorial to compute permutations is slow especially when N is large and k is small.

In [37]: def perm(N, k):
   ....:     if (k > N) or (N < 0) or (k < 0):
   ....:         return 0
   ....:     val = 1
   ....:     for i in xrange(N - k + 1, N + 1):
   ....:         val *= i
   ....:     return val

In [38]: %timeit perm(10000, 10)
1000000 loops, best of 3: 1.2 µs per loop

In [39]: %timeit factorial(10000, True) // factorial(10000 - 10, True)
10 loops, best of 3: 42.3 ms per loop

@pv
Copy link
Member

pv commented Nov 26, 2013

This function looks like it belongs to scipy.special, not scipy.misc. (Also comb does not belong to scipy.misc, but it's there for historical reasons).

@pv
Copy link
Member

pv commented Nov 26, 2013

The exact=False implementation is scipy.special.poch(N-k+1, k), which will (in Scipy >= 0.13.0) deal with overflows etc. correctly.

@cairijun
Copy link
Contributor Author

I think they can be placed in scipy.special and comb can be imported in scipy.misc for the existing code.
Maybe I can make a PR.

@pv
Copy link
Member

pv commented Dec 3, 2013

PR merged.

@pv pv closed this as completed Dec 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.special
Projects
None yet
Development

No branches or pull requests

3 participants