'M' is a numpy ndarray, which dimension 'dim' number is variable (previously generated) but each dimension is of equal size 'size'. In my code it will be more like dim = 5, size = 7.
ex: (dim = 3,size = 4).
M = np.arange(size**dim).reshape((size,)*dim)
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
[[16 17 18 19]
[20 21 22 23]
[24 25 26 27]
[28 29 30 31]]
[[32 33 34 35]
[36 37 38 39]
[40 41 42 43]
[44 45 46 47]]
[[48 49 50 51]
[52 53 54 55]
[56 57 58 59]
[60 61 62 63]]]
And I have a permutation generator of my own, 'per', that generates specific permutations (not random) of range(size).
print(next(per))
(1,0,3,2)
My need: transform M, by moving its elements according to as many permutations as I need. In the example: 21 (1 permutation for first dimension, 4 for second, 16 for third - generalised: size**d for d in range(dim) permutations). My permutations are not random, but they are independant, different from each other.
A result may be:
[[[36 39 37 38]
[33 34 32 35]
[46 44 45 47]]
[41 43 40 42]
[[9 10 11 8]
[2 1 3 0]
[6 7 5 4]
[13 12 14 15]]
[[56 59 57 58]
[63 61 62 60]
[53 54 52 55]
[51 50 49 48]]
[[28 30 29 31]
[27 25 24 26]
[17 18 16 19]
[23 21 20 22]]]
How can I do it directly from M as numpy array, whith my code remaining dynamic?