I have a library of C functions for scalars, e.g.
double f(double x, double y, double z) {
result = x + 2*y - 3*z;
return result;
}
I’d like to use these functions over numpy arrays, but would like to vectorise the operation somehow and avoid several C calls to f for each array element. Most examples that I have found are for functions that already support arrays (f(double *x) or f(double x[])).
What is the right approach here? Rewrite f to support arrays? (There are a lot of functions in the library.) Write a C wrapper, maybe using function pointers? Or do ctypes, cython, etc. offer a simpler alternative?
This has probably been asked hundreds of times, so even just a pointer to the appropriate documentation would be helpful.
Personally I’d say rewriting the functions to use arrays is the best and cleanest way to go. However, an alternative would be the already mentioned wrapper, but with using the original function as an inline function, e. g.
Now the compiler should replace the function call with the code of the function.