Python compute a specific inner product on vectors
<p>Assume having two vectors with m x 6, n x 6</p>

<pre><code>import numpy as np
a = np.random.random(m,6)
b = np.random.random(n,6)
</code></pre>

<p>using np.inner works as expected and yields</p>

<pre><code>np.inner(a,b).shape
(m,n)
</code></pre>

<p>with every element being the scalar product of each combination. I now want to compute a special inner product (namely Plucker). Right now im using</p>

<pre><code>def pluckerSide(a,b):
    a0,a1,a2,a3,a4,a5 = a
    b0,b1,b2,b3,b4,b5 = b
    return a0*b4+a1*b5+a2*b3+a4*b0+a5*b1+a3*b2
</code></pre>

<p>with a,b sliced by a for loop. Which is way too slow. Any plans on vectorizing fail. Mostly broadcast errors due to wrong shapes. Cant get np.vectorize to work either. 
Maybe someone can help here?</p>

<p>There seems to be an indexing based on some random indices for pairwise multiplication and summing on those two input arrays with function <code>pluckerSide</code>. So, I would list out those indices, index into the arrays with those and finally use <code>matrix-multiplication</code> with <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html" rel="nofollow"><code>np.dot</code></a> to perform the sum-reduction.</p>

<p>Thus, one approach would be like this -</p>

<pre><code>a_idx = np.array([0,1,2,4,5,3])
b_idx = np.array([4,5,3,0,1,2])
out = a[a_idx].dot(b[b_idx])
</code></pre>

<p>If you are doing this in a loop across all rows of <code>a</code> and <code>b</code> and thus generating an output array of shape <code>(m,n)</code>, we can vectorize that, like so -</p>

<pre><code>out_all = a[:,a_idx].dot(b[:,b_idx].T)
</code></pre>

<p>To make things a bit easier, we can re-arrange <code>a_idx</code> such that it becomes <code>range(6)</code> and re-arrange <code>b_idx</code> with that pattern. So, we would have :</p>

<pre><code>a_idx = np.array([0,1,2,3,4,5])
b_idx = np.array([4,5,3,2,0,1])
</code></pre>

<p>Thus, we can skip indexing into <code>a</code> and the solution would be simply -</p>

<pre><code>a.dot(b[:,b_idx].T)
</code></pre>

python numpy vector vectorization