[Numpy-discussion] dot/tensordot limitations

Charles R Harris charlesr.harris at gmail.com
Wed Apr 30 03:16:07 EDT 2008


On Tue, Apr 29, 2008 at 4:41 PM, Anne Archibald <peridot.faceted at gmail.com>
wrote:

> Timothy Hochberg has proposed a generalization of the matrix mechanism
> to support manipulating arrays of linear algebra objects. For example,
> one might have an array of matrices one wants to apply to an array of
> vectors, to yield an array of vectors:
>
> In [88]: A = np.repeat(np.eye(3)[np.newaxis,...],2,axis=0)
>
> In [89]: A
> Out[89]:
> array([[[ 1.,  0.,  0.],
>        [ 0.,  1.,  0.],
>        [ 0.,  0.,  1.]],
>
>       [[ 1.,  0.,  0.],
>        [ 0.,  1.,  0.],
>        [ 0.,  0.,  1.]]])
>
> In [90]: V = np.array([[1,0,0],[0,1,0]])
>
> Currently, it is very clumsy to handle this kind of situation even
> with arrays, keeping track of dimensions by hand. For example if one
> wants to multiply A by V "elementwise", one cannot simply use dot:
>

Let A have dimensions LxMxN and b dimensions LxN, then sum(A*b[:,newaxis,:],
axis=-1) will do the trick.

Example:

In [1]: A = ones((2,2,2))

In [2]: b = array([[1,2],[3,4]])

In [3]: A*b[:,newaxis,:]
Out[3]:
array([[[ 1.,  2.],
        [ 1.,  2.]],

       [[ 3.,  4.],
        [ 3.,  4.]]])

In [4]: sum(A*b[:,newaxis,:], axis=-1)
Out[4]:
array([[ 3.,  3.],
       [ 7.,  7.]])

Chuck




Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080430/42101b17/attachment.html>


More information about the NumPy-Discussion mailing list