title: numpy - Sum of dot products along axis
body: <p>I have an <code>A</code> x <code>B</code> array and another <code>D</code> x <code>A</code> x <code>A</code> array and am trying to come up with efficient ways to compute the sum of the dot products of two arrays along the <code>D</code> axis (such that the result would be an <code>A</code> x <code>B</code> array). The most obvious way would be to use a for loop:</p>
  
  <pre><code>result = np.zeros(first_array.shape)
  for d in range(0,second_array.shape[0]):
      result = result + np.dot(second_array[d], first_array)
  print result
  </code></pre>
  
  <p>I'm wondering if there are more efficient ways of computing this in numpy. I've read a bit  into <a href="https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.einsum.html" rel="nofollow noreferrer"><code>np.einsum</code></a> but unfortunately don't completely understand whether it would be able to help in this case.</p>