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.
You can certainly do with np.einsum
:
Z[i,a,b,j] += X[m,e,b,j] * Y[m,e,a,i] * 2
translates to
Z = np.einsum('mebj,meai->iabj', X,Y) * 2
Controller Advice bean not instantiated at proper order in Spring Boot 2.4
What should I enter to the connection string to connect my NodeJS application to a MongoDB server?
Unable to use Computed Property Values with Dots - Unable to Set as String - JS
I need to make the arrow of the right side colorful #2b6a83
hello i have a list of posts and i use javascript to download a post as pdf but i have a problem because the download button works just on the first post and not in the others and when i inspect the button in the browser i can see the description below but the button...
it`s my first question in stackOverflow, and i need help with how to insert data (Strings) into a RealmList
I'm new to SQL and I'm working with the following tables: