[Numpy-discussion] Optimized sum of squares

Gary Ruben gruben@bigpond.net...
Thu Oct 22 04:51:54 CDT 2009

josef.pktd@gmail.com wrote:
> Is it really possible to get the same as np.sum(a*a, axis)  with
> tensordot  if a.ndim=2 ?
> Any way I try the "something_else", I get extra terms as in np.dot(a.T, a)

Just to answer this question, np.dot(a,a) is equivalent to 
np.tensordot(a,a, axis=(0,0))
but the latter is about 10x slower for me. That is, you have to specify 
the axes for both arrays for tensordot:

In [16]: a=rand(1000)

In [17]: timeit dot(a,a)
100000 loops, best of 3: 3.51 µs per loop

In [18]: timeit tensordot(a,a,(0,0))
10000 loops, best of 3: 37.6 µs per loop

In [19]: tensordot(a,a,(0,0))==dot(a,a)
Out[19]: True

More information about the NumPy-Discussion mailing list