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
ENH: Function to generate all permutations of an array #26206
Comments
How do you know |
I've used the following to create an array using itertools:
Note: Not instantiating the intermediate list would save some time, but I compared to the numpy solutions proposed at https://stackoverflow.com/questions/64291076/generating-all-permutations-efficiently. They are about a factor 50 faster, which is not an uncommon Pure-Python -> numpy speedup given that Edit: This reference may be interesting for a fast algorithm as well: https://en.wikipedia.org/wiki/Heap%27s_algorithm#cite_note-3 |
FWIW, you can use
I think @stefanv also wanted this always and I could see adding it. There is maybe also a bit of a question of whether you need to have some option to gracefully deal with a huge number of permutations (since this can quickly explode in size). |
This is a good idea since people may not be aware that this becomes a significant issue with increasing N. One option would be a default size limit (in N or memory usage), and if people want bigger arrays, they have to consciously opt in. An advanced feature would be some batch mode where you receive a generator for a sequence of subarrays of the permutations. But to keep things simple that's likely not for the first shot. |
@seberg would you consider including other combinatorial indexing functions as well? Some options would be:
@timhoffm, your feature request is very useful. I'm skeptical about the expectations for a substantial increase in performance.
Here's a benchmark that aims to make a fairer comparison between faster_permutations and itertools.permutations. The uint8 version is approximately 5 times faster than itertools, while the int64 version offers similar performance on reduced functionality.
|
I would be inclined to make all those functions part of a separate package. Knuth has covered the topic in volumes 4A, 4B, 4C, 4D, 4E, and 4F of TAOCP, of which 4A and 4B have been published :) I think 4A covers what you want. |
I feel This is opinionated, but I regard The first priority is ease of use and expressiveness. Better performance than itertools is nice but only secondary. - It's likely also only relevant for a small range of elments (N~10-14). For lower numbers, every approach is fast enough (I don't expect people to call this many times in a loop). The combinatorical explosion limits towards the high numbers because of memory usage. |
I agree this definitely makes sense in an external package. Starting with an external implementation that we could think about importing into numpy would also motivate the discussion about including it in numpy. Note that a cython implementation would probably be less appealing than a C or C++ implementation - cython produces very large code sizes and I don't know if there's a ton of appetite for adding more cython code to numpy outside of the |
Are we talking about I would be willing to contribute a
As mentioned above, I see the first priority in ease of use and expressiveness. Getting the API and tests right would be the first step for me: e.g. to be discussed whether one wants one or both of permutations of the first N integers ( |
@seberg , is it possible that using The following code calculates permutations for sets of size n.
The conversion of generated tuples into a Python list remains stable, while conversions into numpy arrays deteriorate beyond set size n>10. |
Proposed new feature or change:
I would like to be able to generate all permutations of an array; e.g.
AFAICS, there's neither a dedicated function for that in numpy already nor is it easily expressable through a combination of existing functions. I've only found the
choice
andpermutation
functions from the random generators, but these only create samples not a complete set of permutations.Would such a function be of interest for numpy? If so, I could start with a PR proposal.
Note: The standard library provides
itertools.permutations(...)
but that's significantly slower that a pure numpy implementation.The text was updated successfully, but these errors were encountered: