Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
richardtsai opened this Issue · 6 comments

3 participants

@richardtsai

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
Collaborator

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

It already has factorial.

@richardtsai

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
Owner
pv commented

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
Owner
pv commented

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

@richardtsai

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
Owner
pv commented

PR merged.

@pv pv closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.