Python -Taking dot product of long list of arrays
<p>So I'm trying to to take the dot product of two arrays using numpy's dot product function.</p>

<pre><code>import numpy as np

MWFrPos_Hydro1 = subPos1[submaskFirst1]
x = MWFrPos_Hydro1
MWFrVel_Hydro1 = subVel1[submaskFirst1]
y = MWFrVel_Hydro1
MWFrPosMag_Hydro1 = [np.linalg.norm(i) for i in MWFrPos_Hydro1]

np.dot(x, y)
</code></pre>

<p>returns </p>

<pre><code>---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-135-9ef41eb4235d&gt; in &lt;module&gt;()
      6 
      7 
----&gt; 8 np.dot(x, y)

ValueError: shapes (1220,3) and (1220,3) not aligned: 3 (dim 1) != 1220 (dim 0)
</code></pre>

<p>And I using this function improperly?</p>

<p>The arrays look like this </p>

<pre><code>print x

[[  51.61872482  106.19775391   69.64765167]
 [  33.86419296   11.75729942   11.84990311]
 [  12.75009823   58.95491028   38.06708527]
 ..., 
 [  99.00266266   96.0210495    18.79844856]
 [  27.18083954   74.35041809   78.07577515]
 [  19.29788399   82.16114044    1.20453501]]

print y

[[  40.0402298  -162.62153625 -163.00158691]
 [-359.41983032 -115.39328766   14.8419466 ]
 [  95.92044067 -359.26425171  234.57330322]
 ..., 
 [ 130.17840576   -7.00977898   42.09699249]
 [  37.37852478  -52.66002655 -318.15155029]
 [ 126.1726532   121.3104248  -416.20855713]]
</code></pre>

<p>Would for looping <code>np.vdot</code> be more optimal in this circumstance?</p>

<p>Some possible products for <code>(n,3)</code> arrays (here I'll just one)</p>

<pre><code>In [434]: x=np.arange(12.).reshape(4,3)
In [435]: x
Out[435]: 
array([[  0.,   1.,   2.],
       [  3.,   4.,   5.],
       [  6.,   7.,   8.],
       [  9.,  10.,  11.]])
</code></pre>

<p>element by element product, summed across the columns; <code>n</code> values.  This is a magnitude like number.</p>

<pre><code>In [436]: (x*x).sum(axis=1)
Out[436]: array([   5.,   50.,  149.,  302.])
</code></pre>

<p>Same thing with <code>einsum</code>, which gives more control over which axes are multiplied, and which are summed.</p>

<pre><code>In [437]: np.einsum('ij,ij-&gt;i',x,x)
Out[437]: array([   5.,   50.,  149.,  302.])
</code></pre>

<p><code>dot</code> requires last of the 1st and 2nd last of 2nd to have the same size, so I have to use <code>x.T</code> (transpose).  The diagonal matches the above.</p>

<p>In [438]: np.dot(x,x.T)
    Out[438]: 
    array([[   5.,   14.,   23.,   32.],
           [  14.,   50.,   86.,  122.],
           [  23.,   86.,  149.,  212.],
           [  32.,  122.,  212.,  302.]])</p>

<p><code>np.einsum('ij,kj',x,x)</code> does the same thing.</p>

<p>There is a new <code>matmul</code> product, but with 2d arrays like this it is just <code>dot</code>.  I have to turn them into 3d arrays to get the 4 values; and even with that I have to squeeze out excess dimensions:</p>

<pre><code>In [450]: x[:,None,:]@x[:,:,None]
Out[450]: 
array([[[   5.]],

       [[  50.]],

       [[ 149.]],

       [[ 302.]]])
In [451]: np.squeeze(_)
Out[451]: array([   5.,   50.,  149.,  302.])
</code></pre>

python numpy