find the dot product of sub-arrays in numpy find the dot product of sub-arrays in numpy numpy numpy

find the dot product of sub-arrays in numpy


How about:

from numpy.core.umath_tests import inner1dZ = inner1d(X,Y)

For example:

X = np.random.normal(size=(10,5))Y = np.random.normal(size=(10,5))Z1 = inner1d(X,Y)Z2 = [np.dot(X[k],Y[k]) for k in range(10)]print np.allclose(Z1,Z2)

returns True

Edit Correction since I didn't see the 3D part of the question

from numpy.core.umath_tests import matrix_multiplyX = np.random.normal(size=(10,5,3))Y = np.random.normal(size=(10,3,5))Z1 = matrix_multiply(X,Y)Z2 = np.array([np.dot(X[k],Y[k]) for k in range(10)])np.allclose(Z1,Z2)  # <== returns True

This works because (as the docstring states), matrix_multiplyprovides

matrix_multiply(x1, x2[, out]) matrix

multiplication on last two dimensions