On Thu, Feb 12, 2009 at 8:18 PM, Greg Ewing <[email protected]> wrote:
> I've never looked at the implementation of ufuncs, so this may
> be completely nonsensical, but would it be possible to pass
> shape/strides info into the function, so that it could do its
> own looping over the data?

Good question.  Not only is that possible, but that's what NumPy
expects.  You define a function that takes one dimensional arrays and
outputs one dimensional arrays (iterating over the single dimension
yourself), and the ufunc machinery takes care of handling the
multi-dimensional aspects, broadcasting, types, error handling, etc.

To make things even easier, NumPy includes a set of functions, such as
PyUFunc_d_d(), which perform the one dimensional loop, calling a
supplied function pointer for each element.  For example, if you
define a function, "double foo(double)", you can register your ufunc
with PyUFunc_d_d() as the "function" and foo() as the "data".  This is
easier, but the downside is the extra function call overhead.

The other thing to note is that you can register multiple functions,
each handling different data types, for a single ufunc.  This allows
NumPy to perform the function directly without having to create a
temporary array of the correct type.  In the NumPy source, there is an
"add" function for every possible data type, while my lgamma example
from the wiki page only defines float, double, and long double
functions.

~Mark
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to