Pybind Numpy访问2D / ND阵列

[英]Pybind Numpy access 2D / ND arrays


New to pybind - read the documentation but I do not grasp how to apply it to 2D arrays.

pybind新手 - 阅读文档,但我不知道如何将其应用于2D数组。

I have two arrays storing 3d coordinates shape = (10,3)

我有两个存储3d坐标的数组=(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?

现在,使用pybind,如何在numpy数组上使用C ++执行此操作?

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?

在一些文档中,我读取使用[]运算符访问元素,而在其他文档中使用()。如何分配3D矢量?我如何获得指向数组元素的指针以使用步幅进行赋值 - 或者它是否有运算符?

2 个解决方案

#1


3  

PyBind is awesome, shout out to the authors/maintainers! You have an almost working example here.

PyBind真棒,向作者/维护者大喊大叫!你这里有一个近乎有效的例子。

Adapted to your problem it would give something like (edited answer after El Dude's comment):

适应你的问题它会给出类似的东西(在El Dude评论之后编辑的答案):

#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>


namespace py = pybind11;


py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) {
auto buf1 = input1.request(), buf2 = input2.request();

if (buf1.size != buf2.size)
throw std::runtime_error("Input shapes must match");

/*  allocate the buffer */
py::array_t<double> result = py::array_t<double>(buf1.size);

auto buf3 = result.request();

double *ptr1 = (double *) buf1.ptr,
        *ptr2 = (double *) buf2.ptr,
        *ptr3 = (double *) buf3.ptr;
int X = buf1.shape[0];
int Y = buf1.shape[1];

for (size_t idx = 0; idx < X; idx++)
    for (size_t idy = 0; idy < Y; idy++)
        ptr3[idx*Y + idy] = ptr1[idx*Y+ idy] + ptr2[idx*Y+ idy];

// reshape array to match input shape
result.resize({X,Y});

return result;
}


PYBIND11_MODULE(example, m) {
        m.doc() = "Add two vectors using pybind11"; // optional module docstring

        m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
}

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)

我使用python2.7和gcc v5.4在linux上构建(我必须使用与doc中提供的稍微不同的命令,因为找不到Python.h,因此我添加了到python 2.7的链接)

c++ -O3 -Wall -shared -std=c++11 -fPIC -I/usr/include/python2.7 -lpython2.7 `python -m pybind11 --includes` example.cpp -o example`python-config --extension-suffix

And you'd call it from python with

你会从python中调用它

import numpy as np
import example # [bad] name I chose for my compiled module

a = np.zeros((10,3))
b = np.ones((10,3)) * 3 
c = example.add_arrays(a, b)

print c

Hope it helps.

希望能帮助到你。

#2


0  

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).

诀窍是使用缓冲类。它很好地隐藏/复杂到文档和示例中,但它被提及(@Christian的帖子)。

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 beautiful C11 auto type to explain this usage.

缓冲区包含指向数据的指针以及步幅和其他数组参数。本质上是通过请求方法访问的numpy标头。从那以后易于使用,但发现它有点痛苦,因为该示例使用漂亮的C11自动型来解释这种用法。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2018/03/30/e77f546e4ee5659e222f3d27cd0cfe4f.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告