How should I multiply 1d-array in 3d-array by 2d-matrix with numpy
<p>I want bellow calculation.Values used in this example are variable in real case.</p>

<pre><code> A = [[1,1,1],[2,1,1]]
 B =[[[1,2,3],[2,2,2]],[[1,1,2],[1,1,1]]]
 result = apply_some_function(A,B)
 &gt;&gt; result = [[[6,7],[6,8]],
             [[4,5],[3,4]]]
</code></pre>

<p>Is their any efficient way satisfy above calculation with numpy.
In fact, I don't understand clearly how to applicate numpy for multi dimensional array. So if you know helpful document to understand manipulation multi dimensional array rules, I'm very glad if you let me know.</p>

<p>You could use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.tensordot.html" rel="nofollow"><code>np.tensordot</code></a> or <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.einsum.html" rel="nofollow"><code>np.einsum</code></a>. </p>

<pre><code>In [19]: np.tensordot(B, A, (-1, -1))
Out[19]: 
array([[[6, 7],
        [6, 8]],

       [[4, 5],
        [3, 4]]])

In [20]: np.einsum('ij,klj-&gt;kli', A, B)
Out[20]: 
array([[[6, 7],
        [6, 8]],

       [[4, 5],
        [3, 4]]])
</code></pre>

<hr>

<p>Both of these functions compute sums of products. Thus, they are
generalizations of matrix multiplication. </p>

<p>It is often helpful to pay attention to the shape of the arrays. If we make <code>A</code>, <code>B</code>, and <code>result</code> NumPy arrays:</p>

<pre><code>A = np.array([[1,1,1],[2,1,1]])
B = np.array([[[1,2,3],[2,2,2]],[[1,1,2],[1,1,1]]])
result = np.array([[[6,7],[6,8]], [[4,5],[3,4]]])
</code></pre>

<p>then </p>

<pre><code>In [6]: A.shape
Out[6]: (2, 3)

In [7]: B.shape
Out[7]: (2, 2, 3)

In [9]: result.shape
Out[9]: (2, 2, 2)
</code></pre>

<p>Notice that the axis of length 3 in <code>A</code> and <code>B</code> disappears in <code>result</code>. That suggests the sum (of products) is being taken over the last axis of <code>A</code> and <code>B</code>. </p>

<p>The <code>(-1, -1)</code> in <code>np.tensordot(B, A, (-1, -1))</code> tells <code>np.tensordot</code> to sum over the last axes of <code>A</code> and <code>B</code>.</p>

<p>Similarly, the <code>'ij,klj-&gt;kli'</code> in <code>np.einsum('ij,klj-&gt;kli', A, B)</code> says that if <code>A</code> has indices <code>i</code> and <code>j</code> and if <code>B</code> has indices <code>k</code>, <code>l</code> and <code>j</code>, then the result should have indices <code>k</code>,<code>l</code>,<code>i</code>. Notice the <code>j</code> index disappears. The <code>j</code> index is the last index in <code>A</code> and <code>B</code>. Thus, <code>'ij,klj-&gt;kli'</code> tells <code>np.einsum</code> to sum over the last index of <code>A</code> and <code>B</code>.</p>

<p>The only thing left to do is figure out the correct order of the <code>k</code>, <code>l</code> and <code>i</code> indices. Since each axis in <code>result</code> has the same length, the shape of the <code>result</code> gives no clue. I found the correct order by trial and error.</p>

python arrays numpy