Suppose we have n
bins in which we are throwing k
balls. What is a fast (i.e. using numpy/scipy instead of python code) way to generate all possible outcomes as a matrix?
For example, if n = 4
and k = 3
, we'd want the following numpy.array
:
3 0 0 0
2 1 0 0
2 0 1 0
2 0 0 1
1 2 0 0
1 1 1 0
1 1 0 1
1 0 2 0
1 0 1 1
1 0 0 2
0 3 0 0
0 2 1 0
0 2 0 1
0 1 2 0
0 1 1 1
0 1 0 2
0 0 3 0
0 0 2 1
0 0 1 2
0 0 0 3
Apologies if any permutation was missed, but this is the general idea. The generated permutations don't have to be in any particular order, but the above list was convenient for categorically iterating through them mentally.
Better yet, is there a way to map every integer from 1 to the multiset number (the cardinality of this list) directly to a given permutation?
This question is related to the following ones, which are implemented in R with very different facilities:
- Generating all permutations of N balls in M bins
- Generate a matrix of all possible outcomes for throwing n dice (ignoring order)
Also related references: