def _prod_vectorized(M1, M2):
 sh1 = M1.shape
 sh2 = M2.shape
 assert (len(sh1) >= 2)
 assert (len(sh2) >= 2)
 assert (sh1[(-1)] == sh2[(-2)])
 ndim1 = len(sh1)
 t1_index = (list(xrange((ndim1 - 2))) + [(ndim1 - 1), (ndim1 - 2)])
 return np.sum((np.transpose(M1, t1_index)[..., np.newaxis] * M2[..., np.newaxis, :]), (-3))

def _prod_vectorized ( M1 , M2 ) : sh1 = M1 . shape sh2 = M2 . shape assert ( len ( sh1 ) >= 2 ) assert ( len ( sh2 ) >= 2 ) assert ( sh1 [ ( - 1 ) ] == sh2 [ ( - 2 ) ] ) ndim1 = len ( sh1 ) t1_index = ( list ( xrange ( ( ndim1 - 2 ) ) ) + [ ( ndim1 - 1 ) , ( ndim1 - 2 ) ] ) return np . sum ( ( np . transpose ( M1 , t1_index ) [ ... , np . newaxis ] * M2 [ ... , np . newaxis , : ] ) , ( - 3 ) )
matrix product between arrays of matrices .