[Numpy-discussion] Row-wise dot product?

Hans-Andreas Engel engelh@deshaw....
Mon Sep 7 09:09:57 CDT 2009


> From: T J <tjhnson <at> gmail.com>
> Is there a better way to achieve the following, perhaps without the
> python for loop?
> 
> >>> x.shape
> (10000,3)
> >>> y.shape
> (10000,3)
> >>> z = empty(len(x))
> >>> for i in range(10000):
> ...    z[i] = dot(x[i], y[i])
> ...
> _______________________________________________

Nadav Horesh <nadavh <at> visionsense.com> writes:

> 
> (x*y).sum(1)
> 
>   Nadav
> 


If you wish to avoid the extra memory allocation implied by `x*y'
and get a ~4x speed-up, you can use a generalized ufunc 
(numpy >= 1.3, stolen from the testcases):

   z = numpy.core.umath_tests.inner1d(x, y)

Best,
Hansres







More information about the NumPy-Discussion mailing list