A better way to express a multitude of dot products?

numpy, python

Solution

As an alternative to Warren's solution, which I think is the best, there is the undocumented `inner1d`:

>>> from numpy.core.umath_tests import inner1d
>>> a = inner1d(h, c)
>>> np.allclose(a, ans)
True

From its docstring:

inner1d(x1, x2[, out])

inner on the last dimension and broadcast on the rest: (i),(i)->()

For this particular case, on my system, `inner1d`is slightly faster than `np.einsum`:

In [2]: %timeit np.einsum('ijk,jk->ij', h, c)
100 loops, best of 3: 3.85 ms per loop

In [3]: %timeit inner1d(h, c)
100 loops, best of 3: 2.78 ms per loop

Problem

is there a better and faster way to express the following dot-products in numpy? I have the following shapes: ``` >>> h.shape (600L, 400L, 3L) >>> c.shape (400L, 3L) ``` I want to calculate the following, if possible without a loop: ``` ans = np.empty((600, 400)) for i in range(400): ans[:, i] = h[:, i, :].dot(c[i, :]) ``` I think it should be possible with a simeple reshape, but i don't see how atm.

Original source