I have a collection of symmetric tensors full_tensors
, and I have calculated all distinct elements in a 2D array tensor_reduced
. I want to calculate the full full_tensors
from the distinct elements tensor_reduced
. I have a list all_permutations_list
that contains tuples over all indices that should have the same value (e.g. all_permutations_list[1]=((0, 0, 1, 0, 0, 0), (0, 1, 0, 0, 0, 0), (1, 0, 0, 0, 0, 0), (0, 0, 0, 1, 0, 0), (0, 0, 0, 0, 1, 0), (0, 0, 0, 0, 0, 1)
) and we have that tensor_reduced.shape[1]
is the same as len(all_permutations_list)
.
The following code does what I want:
def makeFulltensor(tensor_reduced,all_permutations_list,dim=4):
full_tensors=np.zeros((tensor_reduced.shape[0],dim,dim,dim,dim,dim,dim),dtype=tensor_reduced.dtype)
for index,all_permutations in enumerate(all_permutations_list):
for perm in (all_permutations):
full_tensors[:,perm[0],perm[1],perm[2],perm[3],perm[4],perm[5]]=tensor_reduced[:,index]
return full_tensors
I would like to speed it up using numpy’s indexing features to avoid the inner (and perhaps the outer) for loop. Does anyone know of an efficient way to do this?
Thanks!
PS: all_permutations_list was calculated as follows:
from itertools import permutations
from itertools import combinations_with_replacement as cwr
all_permutations_list=[]
reduced_indices=list(cwr(range(4), 6))
for index,(i,j,k,l,m,n) in enumerate(reduced_indices):
all_permutations=set(permutations([i,j,k,l,m,n]))
all_permutations_list.append(all_permutations)
all_permutations_list=[tuple(x) for x in all_permutations_list]