[Numpy-discussion] Vectorization of the product of several matrices ?
Charles R Harris
charlesr.harris@gmail....
Tue Sep 30 15:49:29 CDT 2008
On Sun, Sep 28, 2008 at 9:47 AM, oc-spam66 <oc-spam66@laposte.net> wrote:
>
> Hello,
>
> I have two lists of numpy matrices : LM = [M_i, i=1..N] and LN = [N_i, i
> =1..N]
> and I would like to compute the list of the products : LP = [M_i * N_i,
> i=1..N].
>
> I can do :
>
> P=[]
> for i in range(N) :
> P.append(LM[i]*LN[i])
>
> But this is not vectorized. Is there a faster solution ?
> Can I vectorize this operation ? How ?
> Will it be more efficient than the code above ?
>
> Thank you for your help,
>
There are at least three methods I can think of, but choosing the best one
requires more information. How long are the lists? Do the arrays have
variable dimensions? The simplest and most adaptable method is probably
something like
In [1]: M = [matrix(eye(3))]*5
In [2]: N = [matrix(eye(3))]*5
In [3]: P = [m*n for m,n in zip(M,N)]
In [4]: P
Out[4]:
[matrix([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]]),
matrix([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]]),
matrix([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]]),
matrix([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]]),
matrix([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])]
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080930/b835a0fd/attachment.html
More information about the Numpy-discussion
mailing list