numpy : efficient sum computations

Robert Kern robert.kern at gmail.com
Tue Oct 16 20:54:44 CEST 2007


TG wrote:
> Hi there.
> 
> I want to do some intensive computations with numpy, and I'm
> struggling a bit to find myyyyy wayyyyyy.

The best place to ask numpy questions is on the numpy mailing list.

  http://www.scipy.org/Mailing_Lists

> 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 ?

That's about it. There's no need for the [:,:], though.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list