[SciPy-User] Can I create a 3 argument UFunc easily?

Robert Kern robert.kern@gmail....
Tue Jun 8 18:27:33 CDT 2010


On Tue, Jun 8, 2010 at 18:46, John Salvatier <jsalvati@u.washington.edu> wrote:
> Hello,
>
> I would like to make a 3 argument UFunc that finds the weighted average of
> two of the arguments using the 3rd argument as the weight. This way, the
> .accumulate method of the ufunc can be used as an exponentially weighted
> moving average function.
>
> Unfortunately I am not very familiar with the Numpy C API, so I was hoping
> to use the Cython hack for making UFuncs
> (http://wiki.cython.org/MarkLodato/CreatingUfuncs). However, looking at the
> UFunc C API doc
> (http://docs.scipy.org/doc/numpy/reference/c-api.ufunc.html), it looks like
> numpy only has 2 argument "generic functions". Is there a simple way to
> create a "generic function" that takes 3 arguments that will still work with
> accumulate? Is there another way to create the sort of UFunc I want?

While you can make n-argument ufuncs (scipy.special has many of them),
.accumulate() only works for 2-argument ufuncs.

All in all, it's a lot easier and more performant to simply code up an
EWMA in C rather than "tricking" the general ufunc machinery into
achieving a specific effect.

-- 
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 SciPy-User mailing list