Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pybind11将numpy数组传递给C++;混乱的元素 我有一个C++函数,它把三个1D的NUPY数组作为输入: py::array_t<double> myFunc( py::array_t<double> xcoords, py::array_t<double> ycoords, py::array_t<double> zcoords, const int nparts) { // process numpy array inputs from the python side auto buf2 = xcoords.request(); double *x = (double *)buf2.ptr; auto buf3 = ycoords.request(); double *y = (double *)buf3.ptr; auto buf4 = zcoords.request(); double *z = (double *)buf4.ptr; // allocate mem for the output numpy array py::array_t<double> result = py::array_t<double>(nbins); auto buf5 = result.request(); // c++ counterpart of the result array double *hist = (double *)buf5.ptr; // var declarations int i; // loop over all points for (i = 0; i < nparts; i++) { std::cout << i << " " << x[i] << " " << y[i] << " " << z[i] << std::endl; // do stuff to populate elements of result } return result; }_Python_C++_Pybind11 - Fatal编程技术网

pybind11将numpy数组传递给C++;混乱的元素 我有一个C++函数,它把三个1D的NUPY数组作为输入: py::array_t<double> myFunc( py::array_t<double> xcoords, py::array_t<double> ycoords, py::array_t<double> zcoords, const int nparts) { // process numpy array inputs from the python side auto buf2 = xcoords.request(); double *x = (double *)buf2.ptr; auto buf3 = ycoords.request(); double *y = (double *)buf3.ptr; auto buf4 = zcoords.request(); double *z = (double *)buf4.ptr; // allocate mem for the output numpy array py::array_t<double> result = py::array_t<double>(nbins); auto buf5 = result.request(); // c++ counterpart of the result array double *hist = (double *)buf5.ptr; // var declarations int i; // loop over all points for (i = 0; i < nparts; i++) { std::cout << i << " " << x[i] << " " << y[i] << " " << z[i] << std::endl; // do stuff to populate elements of result } return result; }

pybind11将numpy数组传递给C++;混乱的元素 我有一个C++函数,它把三个1D的NUPY数组作为输入: py::array_t<double> myFunc( py::array_t<double> xcoords, py::array_t<double> ycoords, py::array_t<double> zcoords, const int nparts) { // process numpy array inputs from the python side auto buf2 = xcoords.request(); double *x = (double *)buf2.ptr; auto buf3 = ycoords.request(); double *y = (double *)buf3.ptr; auto buf4 = zcoords.request(); double *z = (double *)buf4.ptr; // allocate mem for the output numpy array py::array_t<double> result = py::array_t<double>(nbins); auto buf5 = result.request(); // c++ counterpart of the result array double *hist = (double *)buf5.ptr; // var declarations int i; // loop over all points for (i = 0; i < nparts; i++) { std::cout << i << " " << x[i] << " " << y[i] << " " << z[i] << std::endl; // do stuff to populate elements of result } return result; },python,c++,pybind11,Python,C++,Pybind11,并传递到函数中 res = module.myFunc(points[:,0], points[:,1], points[:,2], 10) 一组值示例如下: 4.6880498641905195 0.335265721430103 4.095097368241972 1.287345441223418 0.18213643934310575 1.9076717100770817 3.9083692529696985 3.58692445785723 1.2090842532316792 2.

并传递到函数中

res = module.myFunc(points[:,0], points[:,1], points[:,2], 10)
一组值示例如下:

4.6880498641905195 0.335265721430103 4.095097368241972
1.287345441223418 0.18213643934310575 1.9076717100770817
3.9083692529696985 3.58692445785723 1.2090842532316792
2.0727395137272273 0.8991931179824342 3.428767449961339
2.8468968826288634 4.935019700055825 1.8476045204842446
2.2248370075368733 4.684880446417251 3.7368688164101376
3.4876671749517643 1.7266611398614629 0.22011783388784623
1.5372784463757512 4.6664761047248575 4.148012219052029
3.10720337544295 4.670106619033467 2.8297722540139763
当我检查
中的值时,python代码会打印出这个值。但是,C++代码产生以下输出:

0 4.68805 0.335266 4.0951
1 0.335266 4.0951 1.28735
2 4.0951 1.28735 0.182136
3 1.28735 0.182136 1.90767
4 0.182136 1.90767 3.90837
5 1.90767 3.90837 3.58692
6 3.90837 3.58692 1.20908
7 3.58692 1.20908 2.07274
8 1.20908 2.07274 0.899193
9 2.07274 0.899193 3.42877
这些价值观不知何故变得杂乱无章<代码>点[1,0]最终会复制
点[0,1]
,等等。这看起来像一个愚蠢的内存/索引问题,但我无法解决。我错过了什么

编辑:文档中有一句话说:“NumPy数组中的数据不能保证以密集方式打包;此外,条目可以用任意的列和行跨距分隔。有时,要求函数只接受使用C(行主键)或Fortran的密集数组是有用的(列主)排序。这可以通过第二个模板参数来完成,该参数的值为
py::array::c_style
py::array::f_style


因此,我将
数组\u t
,改为
。这似乎有效。我不明白为什么numpy条目会在默认情况下被混淆。我希望您能深入了解这种奇怪的行为。

从您的打印资料中可以看出,可能是您将numpy数组按“Fortrat排序”(主列)在C++中的代码中,你可以像C数组那样访问它(行主要)如果你将代码更改为:“STD::CUT@ yKANER,我认为默认的NUMPY数组排序是C。在Python代码中,它是C排序的。为什么在将它转换到C++代码时它会被转换成FORTRAN排序?您不知道它是物理的(内存中的)。因为python是一种高级语言,所以在python中进行排序你不知道Python在<代码> x>代码>和<代码> y>代码>上的应用是为了得到你想要的真正地址。哦,我明白了。所以最好的做法是在C++中方法强制定义C类型排序。正如我从你的打印中看到的,你可能在“FrrAT排序”中得到了NUMPY数组(列主要)。在C++中的代码中,你可以像C数组那样访问它(行主要)如果你将代码更改为:“STD::CUT@ yKANER,我认为默认的NUMPY数组排序是C。在Python代码中,它是C排序的。为什么在将它转换到C++代码时它会被转换成FORTRAN排序?您不知道它是物理的(内存中的)。Python中的Python是一种高级语言,因此如果你写了代码> [x] [y] 你不知道Python应用于<代码> x< /C>和 y>代码>,以获得你想要的真正地址。哦,我明白了。所以最好的做法是在C++中方法强制定义C类型排序。
0 4.68805 0.335266 4.0951
1 0.335266 4.0951 1.28735
2 4.0951 1.28735 0.182136
3 1.28735 0.182136 1.90767
4 0.182136 1.90767 3.90837
5 1.90767 3.90837 3.58692
6 3.90837 3.58692 1.20908
7 3.58692 1.20908 2.07274
8 1.20908 2.07274 0.899193
9 2.07274 0.899193 3.42877