I'm trying to use numpy.einsum
to simplify a loop I have in my code.
Currently, my code looks something like this:
k = 100
m = 50
n = 10
A = np.arange(k*m*n).reshape(k, m, n)
B = np.arange(m*m).reshape(m, m)
T = np.zeros((n, n))
for ind in xrange(k):
T += np.dot(A[ind,:,:].T, np.dot(B, A[ind,:,:]))
I'm trying to use numpy.einsum
as an alternative to this loop:
Tp = np.einsum('nij,njk->ik', np.einsum('nij,jk->nik', A.transpose(0,2,1), B), A)
print np.allclose(T, Tp)
Is it possible to use a single numpy.einsum
instead of two?