Reputation: 101
I want a function in Python that generates all permutations of a possible array, where the unique outputs have the following form and constraints:
3 rows, N columns (let's say 63)
Each column is all 0s except one element is either 1 or -1
each row sums to 0, 1 or -1
I know of the itertools and combinations/products modules.
I could use "products" for arrangements of the 6 possible columns, 1: (1 0 0). .... 6: (0 0 -1), but the row sum constraint wouldn't be satisfied.
Plus starting with all possible combinations/products and filtering out the unwanted means starting with an excessively large set. It would be better if the initial set followed the rules to begin with.
Upvotes: 0
Views: 64
Reputation: 25895
Seems like you are looking for combinations_with_replacement
:
[np.array(c).T for c in combinations_with_replacement(((0, 0, 1), (0, 1, 0), (1, 0, 0)), N)]
This will create a sorted list of all possible such matrices with N
columns. Note this is a lot of arrays for N=63
, 2080 to be exact. You can also make this a 3D array directly.
Upvotes: 1