[Numpy-discussion] array of matrices

Charles R Harris charlesr.harris@gmail....
Fri Mar 27 20:48:10 CDT 2009


On Fri, Mar 27, 2009 at 4:43 PM, Robert Kern <robert.kern@gmail.com> wrote:

> On Fri, Mar 27, 2009 at 17:38, Bryan Cole <bryan@cole.uklinux.net> wrote:
> > I have a number of arrays of shape (N,4,4). I need to perform a
> > vectorised matrix-multiplication between pairs of them I.e.
> > matrix-multiplication rules for the last two dimensions, usual
> > element-wise rule for the 1st dimension (of length N).
> >
> > (How) is this possible with numpy?
>
> dot(a,b) was specifically designed for this use case.
>


I think maybe he wants to treat them as stacked matrices.

 In [13]: a = arange(8).reshape(2,2,2)

In [14]: (a[:,:,:,newaxis]*a[:,newaxis,:,:]).sum(-2)
Out[14]:
array([[[ 2,  3],
        [ 6, 11]],

       [[46, 55],
        [66, 79]]])

In [15]: for i in range(2) : dot(a[i],a[i])
   ....:
Out[15]:
array([[ 2,  3],
       [ 6, 11]])
Out[15]:
array([[46, 55],
       [66, 79]])


Although it might be easier to keep them in a list.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090327/cdf3f39a/attachment.html 


More information about the Numpy-discussion mailing list