[SciPy-User] Dot product of two arrays of vectors

eat e.antero.tammi@gmail....
Thu Oct 4 09:27:04 CDT 2012

```Hi,

On Thu, Oct 4, 2012 at 4:36 PM, Robert Cimrman <cimrman3@ntc.zcu.cz> wrote:

> Or the ultimate weapon: np.einsum(). But I suspect matrix_multiply() to be
> faster.
>
FWIW, indeed it's at least faster than sum() based, like:
In []: from numpy.core.umath_tests import matrix_multiply as mm
In []: f0= lambda a, b: mm(a[:, None, :], b[:, :, None]).squeeze()
In []: f1= lambda a, b: np.sum(a* b, axis= 1).reshape(-1, 1).squeeze()

In []: n= 1000
In []: a, b= rand(n, 3), rand(n, 3)
In []: allclose(f0(a, b), f1(a, b))
Out[]: True

In []: %timeit f0(a, b)
10000 loops, best of 3: 47.2 us per loop
In []: %timeit f1(a, b)
10000 loops, best of 3: 58 us per loop

In []: n= 5000
In []: a, b= rand(n, 3), rand(n, 3)
In []: %timeit f0(a, b)
10000 loops, best of 3: 178 us per loop
In []: %timeit f1(a, b)
1000 loops, best of 3: 225 us per loop

My 2 cents,
-eat

>
> r.
>
> On 10/04/2012 03:29 PM, George Nurser wrote:
> > Tensordot may be what you're after. It gives a lot of flexibility.
> > cheers, George.
> >
> > On 4 October 2012 14:26, Alexander Kalinin <alec.kalinin@gmail.com>
> wrote:
> >> Could you, please, explain me more about matrix_multiply? I tried the
> >> following:
> >>
> >>>>> import numpy.core.umath_tests as ut
> >>>>> ut.matrix_multiply.signature
> >> '(m,n),(n,p)->(m,p)'
> >>>>>
> >>
> >> So, I see the the matrix_multiply is the usual matrix product.
> >>
> >> Sincerely,
> >> Alexander
> >>
> >>
> >> On Thu, Oct 4, 2012 at 3:43 PM, Robert Cimrman <cimrman3@ntc.zcu.cz>
> wrote:
> >>>
> >>> On 10/04/2012 01:25 PM, Alexander Kalinin wrote:
> >>>> Hello, SciPy,
> >>>>
> >>>> Could you, please, explain me, what is the most standard way in NumPy
> to
> >>>> calculate a dot product of two arrays of vectors, like in MatLab? For
> >>>> example, consider two numpy arrays of vectors:
> >>>>
> >>>> a = np.array([[1, 2, 3], [4, 5, 6]])
> >>>> b = np.array([[3, 2, 1], [6, 5, 4]])
> >>>>
> >>>> For the cross product we have convenient function numpy.cross:
> >>>>>>> np.cross(a, b)
> >>>> array([[ -4,   8,  -4],
> >>>>          [-10,  20, -10]])
> >>>>
> >>>> But the numpy.dot product for the arrays of vectors do the matrix
> >>>> multiplication:
> >>>>>>> np.dot(a, b)
> >>>> Traceback (most recent call last):
> >>>>     File "<stdin>", line 1, in <module>
> >>>> ValueError: objects are not aligned
> >>>>
> >>>> Yes, I can emulate the dot product code like:
> >>>>
> >>>> np.sum(a * b, axis = 1).reshape(-1, 1)
> >>>> but may be there is exist more standard way to do the dot product?
> >>>
> >>> You could try using:
> >>>
> >>> from numpy.core.umath_tests import matrix_multiply
> >>>
> >>> if your numpy is recent enough.
> >>>
> >>> Cheers,
> >>> r.
> >>>
> >>> _______________________________________________
> >>> SciPy-User mailing list
> >>> SciPy-User@scipy.org
> >>> http://mail.scipy.org/mailman/listinfo/scipy-user
> >>
> >>
> >>
> >> _______________________________________________
> >> SciPy-User mailing list
> >> SciPy-User@scipy.org
> >> http://mail.scipy.org/mailman/listinfo/scipy-user
> >>
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User@scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> >
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20121004/ce8e6037/attachment-0001.html
```