[Numpy-discussion] Dot product threading?

Angus McMorland a.mcmorland at auckland.ac.nz
Wed May 17 17:39:19 EDT 2006


Robert Kern wrote:
> Angus McMorland wrote:
>
>>Is there a way to specify which dimensions I want dot to work over?
>
> Use swapaxes() on the arrays to put the desired axes in the right places.

Thanks for your reply, Robert. I've explored a bit further, and have
made sense of what's going on, to some extent, but have further questions.

My interpretation of the dot docstring, is that the shapes I need are:
    a.shape == (2,3,1) and b.shape == (2,1,3)
so that the sum is over the 1s, giving result.shape == (2,3,3)
but:
    In [85]:ma = array([[[4],[5],[6]],[[7],[8],[9]]]) #shape = (2, 3, 1)
    In [86]:mb = array([[[4,5,6]],[[7,8,9]]])         #shape = (2, 1, 3)
so
    In [87]:res = dot(ma,mb).shape
    In [88]:res.shape
    Out[88]:(2, 3, 2, 3)
such that
    res[i,:,j,:] == dot(ma[i,:,:], mb[j,:,:])
which means that I can take the results I want out of res by slicing
(somehow) res[0,:,0,:] and res[1,:,1,:] out.

Is there an easier way, which would make dot only calculate the dot
products for the cases where i==j (which is what I call threading over
the first dimension)?

Since the docstring makes no mention of what happens over other
dimensions, should that be added, or is this the conventional numpy
behaviour that I need to get used to?

Cheers,

Angus
-- 
Angus McMorland
email a.mcmorland at auckland.ac.nz
mobile +64-21-155-4906

PhD Student, Neurophysiology / Multiphoton & Confocal Imaging
Physiology, University of Auckland
phone +64-9-3737-599 x89707

Armourer, Auckland University Fencing
Secretary, Fencing North Inc.




More information about the NumPy-Discussion mailing list