[Numpy-discussion] Best way to broadcast a function from C

James Crist crist042 at umn.edu
Wed Aug 20 21:34:05 EDT 2014


I have a C function func that takes in scalar arguments, and an array of
fixed dimension that is modified in place to provide the output. The
prototype is something like:

`void func(double a, double b, double c, double *arr);`

I've wrapped this in Cython and called it from python with no problem. What
I'd like to do now though is get it set up to broadcast over the input
arguments and return a 3 dimensional array of the results. By this I mean

a = array([1, 2, 3])
b = array([2.0, 3.0, 4.0])
c = array([3, 4, 5])

func(a, b, c) -> a 3d array containing the results of func for (a, b, c) =
(1, 2.0, 3), (2, 3.0, 4), (3, 4.0, 5)

I'm not sure if this would qualify as a ufunc, as the result of one
function call isn't a scalar but an array, but the effect I'm looking for
is similar. Ideally it would handle datatype conversions (in the above `a`
and `c` aren't double, but `func` takes in double). It would also be
awesome to allow an argument to be a scalar and not an array, and have it
be broadcast as if it were.

I'm just wondering what the best way for me to hook my code up to the
internals of numpy and get this kind of behavior in an efficient way. I've
read the "writing your own ufunc" part of the docs, but am unsure if what
I'm looking for qualifies. Note that I can change the inner workings of
`func` if this is required to achieve this behavior.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140820/aec5b6f5/attachment.html>

More information about the NumPy-Discussion mailing list