Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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
如何从C++中访问嵌入式Python中的NUMPY数组? 在C++中访问一个2Dimple数组的好方法是什么?我已经查看了numpy/CAPI和其他一些帖子,但这并没有让我走得更远。情况如下:_Python_C++_Arrays_Numpy_C Api - Fatal编程技术网

如何从C++中访问嵌入式Python中的NUMPY数组? 在C++中访问一个2Dimple数组的好方法是什么?我已经查看了numpy/CAPI和其他一些帖子,但这并没有让我走得更远。情况如下:

如何从C++中访问嵌入式Python中的NUMPY数组? 在C++中访问一个2Dimple数组的好方法是什么?我已经查看了numpy/CAPI和其他一些帖子,但这并没有让我走得更远。情况如下:,python,c++,arrays,numpy,c-api,Python,C++,Arrays,Numpy,C Api,我在名为Testfile.py的python文件中定义了以下numpy数组: import numpy as np A = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) 现在,我想访问C++中的数组,以便进一步计算。以下是我到目前为止所做的。 注意:为了简单起见,我省略了错误处理和引用计数代码段 #define NPY_NO_DEPRECATED_APINPY_1_7_API_VERSION #define PY_ARRAY_UNIQUE_SYMBOL

我在名为Testfile.py的python文件中定义了以下numpy数组:

import numpy as np
A = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])

现在,我想访问C++中的数组,以便进一步计算。以下是我到目前为止所做的。 注意:为了简单起见,我省略了错误处理和引用计数代码段

#define NPY_NO_DEPRECATED_APINPY_1_7_API_VERSION
#define PY_ARRAY_UNIQUE_SYMBOL cool_ARRAY_API
#include <Python.h>
#include <arrayobject.h> // numpy!
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <string>

int main(){

// Name of input-file
char pyfilename[] = "Testfile";

// initilaize python interpreter
Py_Initialize();
import_array(); 

// load input-file
PyObject *pyName = PyUnicode_FromString(pyfilename);
PyObject *pyModule = PyImport_Import(pyName);

// import my numpy array object
char pyarrayname[] = "A";
PyObject *obj = PyObject_GetAttrString(pyModule, pyarrayname);

//------------------------------------------------------------
// The Problem starts here..

// Array Dimensions
npy_intp Dims[] = { PyArray_NDIM(obj) }; // array dimension
Dims[0] = PyArray_DIM(obj, 0); // number of rows
Dims[1] = PyArray_DIM(obj, 1); // number of columns

// PyArray_SimpleNew allocates the memory needed for the array.
PyObject *ArgsArray = PyArray_SimpleNew(2, Dims, NPY_DOUBLE);

// The pointer to the array data is accessed using PyArray_DATA()
double *p = (double *)PyArray_DATA(ArgsArray);

for (int i = 0; i<Dims[0]; i++)
{
    for (int j = 0; j<Dims[1]; j++)
        {
             p[i * Dims[1] + j] = *((int *)PyArray_GETPTR2(obj, i, j));
        }
}
// -----------------------------------------------------------


Py_Finalize();

return 0;
}
我使用python 3.6和MSVC 2015

编辑:我添加了我使用的标题,并稍微改变了问题的表述


编辑:在您通过访问阵列后,我添加了Swift和Alan Stokes提供的建议解决方案策略

double *p = (double*)PyArray_DATA(ArgsArray);
int* pA = (int*)PyArray_DATA(obj);
您可以像使用数组一样使用它。什么是尺寸

int height = PyArray_DIM(obj, 0);
int width = PyArray_DIM(obj, 1);
现在您可以使用该指针访问数组中的数据

for ( int i = 0; y<height; y++) 
{ 
   for (int j = 0; x<width; x++) 
   { 
      p[i * width + j] = pA[i * width + j];
   }
}
实际上,如果您只需要复制数组,那么此时就使用memcpy或std::copy


Tbh,我曾考虑改用boost.python,但这是我自己的偏好。

调用数组意味着什么?对我来说,这意味着使用以前定义的数据。例如,首先我定义一个整数inta=1;稍后,我在代码中的其他地方使用/调用这个变量,例如int b;b=a+1;我不确定单词call是否是正确的术语:在我的情况下,我使用numpy在python中定义了一个名为a的数组。现在我想在C++代码中使用/调用这个数组,用于其他微积分。这不是电话通常的意思;调用函数,但访问变量或数组元素。数组在通常意义上是不可调用的。PyArray_GETPTR2可能会帮助您。@Alan Stokes啊,听起来像LUA和Python的细节。。当他们使用调用时,也就是说,用括号来访问所有内容而不是使用pA[i*width+j],有没有办法获得多维数组?例如,通过编写一些具有这样做的运算符的包装器,像pA[i][j]?@decadenza一样访问它?在一般情况下,我不会尝试冒险回答,任何类型转换都可能导致别名破坏。我认为boost提供了这个功能,我只是从来没有使用过这个扩展。非常感谢。我实际上正在研究Python。我正在编写一些C++扩展,对大矩阵进行操作,但是我发现使用一维数组非常痛苦,并且惊讶C++没有现成的东西来做这件事。另一方面,我不想添加不必要的负载。