New to pybind - read the documentation but I do not grasp how to apply it to 2D arrays.
I have two arrays storing 3d coordinates shape = (10,3)
a = np.zeros(shape=(10,3))
b = np.ones(shape=(10,3)) * 3
c = a + b
Now, using pybind, how do I perform this operation in C++ working on the numpy arrays?
In some documentations I read to access the elements with the []
operator, in others with ()
.
How do assign the 3D vector?
How would I get the pointer to the array element to use strides for assignment - or does it have an operator?
PyBind is awesome, shout out to the authors/maintainers! You have an almost working example here.
Adapted to your problem it would give something like (edited answer after El Dude's comment):
That I built on linux with python2.7 and gcc v5.4 using (I had to use a slightly different command than provided in the doc, because Python.h wasn't found, hence I added the link to python 2.7)
And you'd call it from python with
Hope it helps.
The trick is to use the buffer class. It's well hidden / convoluted into the documentation and examples, but it's mentioned (@Christian 's post).
Buffers contain a pointer to the data as well as strides and others array parameters. Essentially the numpy header accessed through
request
method. Easy to use from there on, but finding it is bit of a pain since the example uses beautifulC11
auto
type to explain this usage.