I need to optimize an algorithm, which needs to be fast as possible, for now, its a basic sum over the dot products of 2 vectors problem, but I think my solution is a bit redundant, and Einstein notation can get much faster results. First, I had a single value case:
for t in range(2):
for i2 in range(40):
for j2 in range(40):
U_out[i2,j2,t] += np.sum(np.multiply(U_in[:,:,t],constants[:,:,j2,i2,t]))
I needed to use 8 times in a row, so i came up with another barbaric solution:
for t in range(2):
for i2 in range(40):
for j2 in range(40):
U_out[i2,j2,t,0] += np.sum(np.multiply(U_in[:,:,t,0],constants[:,:,j2,i2,t]))
U_out[i2,j2,t,1] += np.sum(np.multiply(U_in[:,:,t,1],constants[:,:,j2,i2,t]))
U_out[i2,j2,t,2] += np.sum(np.multiply(U_in[:,:,t,2],constants[:,:,j2,i2,t]))
U_out[i2,j2,t,3] += np.sum(np.multiply(U_in[:,:,t,3],constants[:,:,j2,i2,t]))
U_out[i2,j2,t,4] += np.sum(np.multiply(U_in[:,:,t,4],constants[:,:,j2,i2,t]))
U_out[i2,j2,t,5] += np.sum(np.multiply(U_in[:,:,t,5],constants[:,:,j2,i2,t]))
U_out[i2,j2,t,6] += np.sum(np.multiply(U_in[:,:,t,6],constants[:,:,j2,i2,t]))
U_out[i2,j2,t,7] += np.sum(np.multiply(U_in[:,:,t,7],constants[:,:,j2,i2,t]))
Now, the first code, repeated 8 times is around a second, whereas the second code is taking 0.4 seconds. However, i will be using them in an optimization AI algorithm so it will be looping over for weeks, in this form. Variable shapes are:
U_out = (40,40,2) 1st code , (40,40,2,8) 2nd code
U_in = (40,40,2) 1st code , (40,40,2,8) 2nd code
constants = (40,40,40,40,2) for both codes
Any help, even no Einstein notation but reducing the 2nd code summation to 4 lines like the above would help me a lot.