Reorder the for-loops order with customization

I work with arrays having the following structure / records (for a master's project in quantum information games); Records of the first column {0,1}, second col {0,1}, third col {0,2**(d-1)}, last col {0,d-1}. As follows for d=3:

G = 
[[0 0 0 0]
 [0 0 0 1]
 [0 0 0 2]
 [0 0 1 0]
 [0 0 1 1]
 [0 0 1 2]
 [0 0 2 0]
 [0 0 2 1]
 [0 0 2 2]
 [0 0 3 0]
 [0 0 3 1]
 [0 0 3 2]
 [0 1 0 0]
 [0 1 0 1]
 [0 1 0 2]
 [0 1 1 0]
 [0 1 1 1]
 [0 1 1 2]
 [0 1 2 0]
 [0 1 2 1]
 [0 1 2 2]
 [0 1 3 0]
 [0 1 3 1]
 [0 1 3 2]
 [1 0 0 0]
 [1 0 0 1]
 [1 0 0 2]
 [1 0 1 0]
 [1 0 1 1]
 [1 0 1 2]
 [1 0 2 0]
 [1 0 2 1]
 [1 0 2 2]
 [1 0 3 0]
 [1 0 3 1]
 [1 0 3 2]
 [1 1 0 0]
 [1 1 0 1]
 [1 1 0 2]
 [1 1 1 0]
 [1 1 1 1]
 [1 1 1 2]
 [1 1 2 0]
 [1 1 2 1]
 [1 1 2 2]
 [1 1 3 0]
 [1 1 3 1]
 [1 1 3 2]]

I use the following function to build this array:

def games(d = 3):
    res = np.empty(0).astype(int)
    for a in range(2):
        for b in range(2):
            for x in range(2**(d-1)):
                for y in range(d):
                    res = np.append(res,[a,b,x,y],axis=0)
    res = np.reshape(res,(-1,4))    
    return res

Now, what I would like to do is easily choose in which order the entries in the columns begin to count. (Above its right column to the left.)

For example, say Id as the first column to start counting, then the third column, then the fourth column, and finally the second. I can get this by rearranging for-loopsinto a function:

def games(d = 3):
    res = np.empty(0).astype(int)

    for b in range(2):
        for y in range(d):        
            for x in range(2**(d-1)):
                for a in range(2):
                    res = np.append(res,[a,b,x,y],axis=0)
    res = np.reshape(res,(-1,4))    
    return res

What gives:

G=
[[0 0 0 0]
 [1 0 0 0]
 [0 0 1 0]
 [1 0 1 0]
 [0 0 2 0]
 [1 0 2 0]
 [0 0 3 0]
 [1 0 3 0]
 [0 0 0 1]
 [1 0 0 1]
 [0 0 1 1]
 [1 0 1 1]
 [0 0 2 1]
 [1 0 2 1]
 [0 0 3 1]
 [1 0 3 1]
 [0 0 0 2]
 [1 0 0 2]
 [0 0 1 2]
 [1 0 1 2]
 [0 0 2 2]
 [1 0 2 2]
 [0 0 3 2]
 [1 0 3 2]
 [0 1 0 0]
 [1 1 0 0]
 [0 1 1 0]
 [1 1 1 0]
 [0 1 2 0]
 [1 1 2 0]
 [0 1 3 0]
 [1 1 3 0]
 [0 1 0 1]
 [1 1 0 1]
 [0 1 1 1]
 [1 1 1 1]
 [0 1 2 1]
 [1 1 2 1]
 [0 1 3 1]
 [1 1 3 1]
 [0 1 0 2]
 [1 1 0 2]
 [0 1 1 2]
 [1 1 1 2]
 [0 1 2 2]
 [1 1 2 2]
 [0 1 3 2]
 [1 1 3 2]]

for-loops , 24 , . , , , solution/approach?

+4
3

, , " " itertools module . , itertools.product, . , Numpy.

import itertools

def make_games(d=3, perm=[3,2,1,0]):
    entries = [range(2),
               range(2),
               range(2**(d-1)),
               range(d)]
    # Python3 compatibility
    entries = [list(entry) for entry in entries]

    # Cartesian product with columns count-order by `perm`
    permuted_entries = [entries[px] for px in perm[::-1]]
    games_list = list(itertools.product(*permuted_entries))

    # Move the columns around to the original ordering
    sorter = np.argsort(perm[::-1])
    games = np.take(games_list, sorter, axis=1)

    return games

, , , make_games(3, [0, 2, 3, 1]). , , itertools.permutations(range(4)).


, Numpy ( d):

def make_games_np(d=3, perm=[3,2,1,0]):
    entries = [range(2),
               range(2),
               range(2**(d-1)),
               range(d)]
    # Python3 compatability
    entries = [list(entry) for entry in entries]

    n = len(entries)
    entries_grid = np.array(np.meshgrid(*entries, indexing='ij'))
    entries_grid = np.rollaxis(entries_grid, 0, n+1)

    order = list(perm)[::-1] + [n]
    games = entries_grid.transpose(*order).reshape(-1, n)

    return games
+1

G 4- , 2, 2, 4 3 . 4 24 . G, 4 24 permutations(24,4)= 10626.

, , 10626 G. , , 10626 . -

import itertools

# Create meshes with upto 2,2,4,3 numbers as is the case across G columns
D0,D1,D2,D3 = np.meshgrid(np.arange(2),np.arange(2),np.arange(4),np.arange(3))

# All possible dimension arrangements with 4 dimensions for each of D0,D1,D2,D3
dims = np.asarray(list(itertools.permutations(range(4))))

# All possible arrangements considering the dimension arrangements
dims_row_idx = np.asarray(list(itertools.combinations(range(dims.shape[0]),4)))

# Use dims_row_idx to select rows of dims and subsequently 
# permute dimensions of D0,D1,D2,D3 and stack them as columns
for d in dims_row_idx:
    c0 = D0.transpose(dims[d[0]]).ravel()
    c1 = D1.transpose(dims[d[1]]).ravel()
    c2 = D2.transpose(dims[d[2]]).ravel()
    c3 = D3.transpose(dims[d[3]]).ravel()
    out = np.column_stack((c0,c1,c2,c3))
    # print(out)
0
import numpy as np
import itertools

def games(d=3):
    res_list=[]
    a=list(itertools.permutations([2,2,2**(d-1),d],4))
    for index in range(len(a)):
        res=np.empty(0).astype(int)
        for i in range(a[index][0]):
            for j in range(a[index][1]):
                for p in range(a[index][2]):
                    for q in range(a[index][3]):
                        res=np.append(res,[i,j,p,q],axis=0)
        res=np.reshape(res,(-1,4))
        res_list.append(res)
    return res_list

I think there is a problem of inconsistency in your question. I think you mean the third col {0.2 ** (d-1) -1} instead of {0.2 ** (d-1)}.

0
source

Source: https://habr.com/ru/post/1608107/


All Articles