[SciPy-User] Dot product of two arrays of vectors
Robert Cimrman
cimrman3@ntc.zcu...
Thu Oct 4 08:33:39 CDT 2012
On 10/04/2012 03:26 PM, Alexander Kalinin 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.
Yes, but the important part is the "on last two dimensions" part of the docstring:
In [5]: a = np.ones((5, 2))
In [6]: b = 2 * a
In [7]: a
Out[7]:
array([[ 1., 1.],
[ 1., 1.],
[ 1., 1.],
[ 1., 1.],
[ 1., 1.]])
In [8]: b
Out[8]:
array([[ 2., 2.],
[ 2., 2.],
[ 2., 2.],
[ 2., 2.],
[ 2., 2.]])
In [17]: matrix_multiply(a[:, None, :], b[:, :, None]).squeeze()
Out[17]: array([ 4., 4., 4., 4., 4.])
r.
> 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
>
More information about the SciPy-User
mailing list