How to "reduce" a numpy array using a costum binary function

Robert Kern robert.kern at gmail.com
Thu Nov 13 16:48:34 EST 2008


Slaunger wrote:
> It is always good to ask yourself a question.
> I had forgooten about the reduce function
> 
> I guess this implementation
> 
> from numpy import *
> 
> def compl_add_uint16(a, b):
>     c = a + b
>     c += c >> 16
>     return c & 0xFFFF
> 
> def compl_one_checksum(uint16s):
>     return reduce(compl_add_uint16, uint16s, 0x0000)
> 
> is somewhat better?
> 
> But is it the best way to do it with numpy?

It's not too bad, if you only have 1D arrays to worry about (or you are only 
concerned with reducing down the first axis). With a Python-implemented 
function, there isn't much that will get you faster.

My coworker Ilan Schnell came up with a neat way to use PyPy's RPython->C 
translation scheme and scipy.weave's ad-hoc extension module-building 
capabilities to generate new numpy ufuncs (which have a .reduce() method) 
implemented in pure RPython.

   http://conference.scipy.org/proceedings/SciPy2008/paper_16/full_text.pdf
   http://svn.scipy.org/svn/scipy/branches/fast_vectorize/

If you have more numpy questions, please join us on the numpy mailing list.

   http://www.scipy.org/Mailing_Lists

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