title: Elementwise function over entries in two numpy arrays of differen shape
body: <p>Let A be a numpy array of shape (a,b,c) and B a numpy array of shape (a',b,c). Let f(A_,B_) be a function that maps a numpy array A_ of shape (b,c) and a numpy array B_ of shape (b,c) to a real number. I would like to construct a numpy array C of shape (a,a') with entries given by applying f to the slices over the first indices.
  The naive solution is </p>
  
  <pre><code>    A=np.reshape(range(2*3*4), (2,3,4))
      B=np.reshape(range(3*3*4), (3,3,4))
      C=np.empty((2,3))
  
      def f(A_,B_):
        return np.prod(A_)+np.prod(B_)
  
      for i in range(A.shape[0]):
          for j in range(B.shape[0]):
             C[i,j]=f(A[i],B[j])
  </code></pre>
  
  <p>which returns C as</p>
  
  <pre><code> [[  0.00000000e+00,   6.47647525e+14,   3.99703747e+17],
    [  6.47647525e+14,   1.29529505e+15,   4.00351395e+17]]
  </code></pre>
  
  <p>I'm going to apply this to much larger arrays A,B with an f that is computationally expensive (above f is just a toy example). I usually try to avoid accessing numpy arrays elementwise but in above situation I'm not sure how to accomplish this.</p>