So I have these 6 nested loops, and their purpose is only to multiply and add arrays X and Y over different indices to get array Z.
import numpy as np
dim_a = 5
dim_b = 9
Z = np.zeros((dim_a,dim_b,dim_b,dim_a))
X = np.arange(2025).reshape(dim_a,dim_b,dim_b,dim_a)
Y = np.arange(2025).reshape(dim_a,dim_b,dim_b,dim_a)
for i in range(0, dim_a):
for j in range(0,dim_a):
for a in range(0, dim_b):
for b in range(0, dim_b):
for m in range(0,dim_a):
for e in range(0,dim_b):
Z[i,a,b,j] += X[m,e,b,j] * Y[m,e,a,i] * 2
Is there a way to write it using just a few lines of code using NumPy? The computational effort of these nested loops is enormous. I have a feeling that NumPy can optimize it significantly.