I need to multiply two 3D arrays in an usual way.
If needed to accomplish my task, I can ''afford'' to permute (change their shape) as I need as they are pretty small in size (less than (1_000, 200, 200) of np.complex128
).
At the moment, I have the following inefficient triple nested for-loop:
import numpy as np
result = np.zeros( (640, 39, 20) )
a = np.random.rand(640, 640, 20)
b = np.random.rand(39, 640, 20)
for j in range(640):
for m in range(39):
for l in range(20):
result[j, m, l] = (a[j, :, l] * b[m, :, l]).sum()
How can I make the above as efficient as possible using numpy
's magic?
I know I could use numba
and hope that I beat numpy
by using compiled code and parallel=True, but I want to see if numpy suffices for my task.
EDIT: Does it work for a more complex inner for loop as below?
for l in range(20):
for m in range(-l, l+1, 1):
for j in range(640):
result[j, m, l] = (a[j, :, l] * b[m, :, l]).sum()
After @hpaulj comment, I now understand that the above is not possible.
Thank you!