suggestions about efficiently binding typedef function taking double * to take np.array #3190
Unanswered
mike-gimelfarb
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The problem is fairly simple: I have a typedef function declared as follows
typedef std::function<double(const double*)> multivariate;
which is used as a parameter in other functions. I would like this function, from the python side, to take np.array as input and return float. Currently, the only way I know how is to create a wrapper that will work from python, e.g.
typedef std::function<double(const pybind11::array_t<double>&)> multivariate_wrapper;
and then convert this to a multivariate object in the binding code, e.g.
multivariate fc = [&](const double *x) -> double { const auto &arr = py::array(n, x); return f(arr); };
Here, n is taken as an additional argument provided by the python call, and f is an instance of multivariate_wrapper.
This compiles fine, but the python code is much slower (at least 5x) than the corresponding c++ code. I am wondering if there is a better way to achieve this within the pybind11 framework without having to create multivariate explicitly and capture? Would replacing std::function with a struct or similar (maybe with a call function) and pass pointer be a better alternative? Thanks, if you have any suggestions.
Beta Was this translation helpful? Give feedback.
All reactions