numpy : efficient sum computations

TG girodt at gmail.com
Tue Oct 16 04:50:29 EDT 2007


Hi there.

I want to do some intensive computations with numpy, and I'm
struggling a bit to find myyyyy wayyyyyy. Here is the problem :

m and d are two matrices :

> m.shape = (x,y,a,b)
> d.shape = (a,b)

I want to return
> i.shape = (x,y)
with
> i[x,y] = sum(m[x,y] * d)

I already found that
> m[:,:] * d
will give me a matrix of shape (x,y,a,b) containing the products.

Now I want to sum up on axis 2 and 3. If I do :
> (m[:,:] * d).sum(axis=3).sum(axis=2)
it seems like I get my result.

I'm wondering : is this syntax leading to efficient computation, or is
there something better ?

thanks

Thomas




More information about the Python-list mailing list