On Mon, Sep 7, 2009 at 10:09 AM, Hans-Andreas Engel<[email protected]> wrote: >> 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)
Thanks for the example. There are not many examples of generalized ufuncs on the mailing list and it's easy to forget that they have been added. Initially, I didn't find them in my numpy 1.3.0 install (AttributeErrors). It seems they have to be explicitly imported: import numpy.core.umath_tests Josef > > Best, > Hansres > > > > > > _______________________________________________ > NumPy-Discussion mailing list > [email protected] > http://mail.scipy.org/mailman/listinfo/numpy-discussion > _______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
