I have two input arrays and one output array like this:
M=np.array([[1,2,3],[3,4,5],[6,7,8]])
u=np.array([[1,2,3],[4,5,7],[2,4,9]])
res=np.zeros((3,))
I want to do the following calculation:
for i in range(3):
res[i]=np.matmul(np.matmul(u[0:,i].T,M),u[0:,i])
#res=array([ 231., 594., 1957.])
Can I do it without doing for loop since for loop will take alot of time in larger size matrix
so the goal is to acheive quicker method
The most intuitive way to do it is probably via np.einsum
:
res = np.einsum('ki,kl,li->i', u, M, u)