. Divakar Trick , , , , 4d- sercnew2.
(. https://www.peterbe.com/plog/uniqifiers-benchmark) . sercnew2 , .
from itertools import product
import numpy as np
sercnew2 = np.ones((gn, gn, gn, gn))
n_dims=4
idx = list(product(np.arange(gn), repeat=n_dims))
for i,j,k,l in idx:
unique_items = set((i,j,k,l))
for ele in unique_items:
sercnew2[i,j,k,l] *= ewp[ele]
EDIT: as suggested by @unutbu, we could also use the cartesian_product function from fooobar.com/questions/64380 / ... to speed up initializationidx
Edit2: If you are having difficulty understanding what it is doing productfrom itertools, it provides all permutations. For example, suppose the gn=2repetition size is 4, you get
[0, 0, 0, 0]
[0, 0, 0, 1]
[0, 0, 1, 0]
[0, 0, 1, 1]
[0, 1, 0, 0]
[0, 1, 0, 1]
[0, 1, 1, 0]
[0, 1, 1, 1]
[1, 0, 0, 0]
[1, 0, 0, 1]
[1, 0, 1, 0]
[1, 0, 1, 1]
[1, 1, 0, 0]
[1, 1, 0, 1]
[1, 1, 1, 0]
[1, 1, 1, 1]