【发布时间】:2015-08-03 23:21:15
【问题描述】:
我要将c++ 数组作为numpy array 发送到python 函数并返回另一个numpy array。在咨询了numpy 文档和其他一些线程并调整了代码后,代码终于可以正常工作了,但我想知道这段代码是否写得最好,考虑到:
- 在
c++和numpy (python)之间不必要地复制数组。 - 正确取消引用变量。
- 简单直接的方法。
C++ 代码:
// python_embed.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "Python.h"
#include "numpy/arrayobject.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Py_SetProgramName(argv[0]);
Py_Initialize();
import_array()
// Build the 2D array
PyObject *pArgs, *pReturn, *pModule, *pFunc;
PyArrayObject *np_ret, *np_arg;
const int SIZE{ 10 };
npy_intp dims[2]{SIZE, SIZE};
const int ND{ 2 };
long double(*c_arr)[SIZE]{ new long double[SIZE][SIZE] };
long double* c_out;
for (int i{}; i < SIZE; i++)
for (int j{}; j < SIZE; j++)
c_arr[i][j] = i * SIZE + j;
np_arg = reinterpret_cast<PyArrayObject*>(PyArray_SimpleNewFromData(ND, dims, NPY_LONGDOUBLE,
reinterpret_cast<void*>(c_arr)));
// Calling array_tutorial from mymodule
PyObject *pName = PyUnicode_FromString("mymodule");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (!pModule){
cout << "mymodule can not be imported" << endl;
Py_DECREF(np_arg);
delete[] c_arr;
return 1;
}
pFunc = PyObject_GetAttrString(pModule, "array_tutorial");
if (!pFunc || !PyCallable_Check(pFunc)){
Py_DECREF(pModule);
Py_XDECREF(pFunc);
Py_DECREF(np_arg);
delete[] c_arr;
cout << "array_tutorial is null or not callable" << endl;
return 1;
}
pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs, 0, reinterpret_cast<PyObject*>(np_arg));
pReturn = PyObject_CallObject(pFunc, pArgs);
np_ret = reinterpret_cast<PyArrayObject*>(pReturn);
if (PyArray_NDIM(np_ret) != ND - 1){ // row[0] is returned
cout << "Function returned with wrong dimension" << endl;
Py_DECREF(pFunc);
Py_DECREF(pModule);
Py_DECREF(np_arg);
Py_DECREF(np_ret);
delete[] c_arr;
return 1;
}
int len{ PyArray_SHAPE(np_ret)[0] };
c_out = reinterpret_cast<long double*>(PyArray_DATA(np_ret));
cout << "Printing output array" << endl;
for (int i{}; i < len; i++)
cout << c_out[i] << ' ';
cout << endl;
// Finalizing
Py_DECREF(pFunc);
Py_DECREF(pModule);
Py_DECREF(np_arg);
Py_DECREF(np_ret);
delete[] c_arr;
Py_Finalize();
return 0;
}
在 CodeReview 中,有一个绝妙的答案:Link...
【问题讨论】:
-
你看过 Boost.Numpy 吗?看这个非常简单的例子:github.com/ndarray/Boost.NumPy/blob/master/libs/numpy/example/…
-
或使用 Cython? stackoverflow.com/a/18176741/2230844
-
@denfromufa,我不想在这里使用 boost,cython 也不是一个选项,因为我正在使用 python/numpy 扩展 C++。
-
你看过 SWIG (docs.scipy.org/doc/numpy/reference/swig.interface-file.html) 吗?它允许在 C++ 数组上的 numpy 中拥有内存视图,反之亦然。它会自动处理包装。虽然我从未在 C++ 到 Python 的方向上使用过它。
-
@Dietrich,谢谢,SWIG 也不错,但在这里我想纯粹使用 Python/C++ API。我感谢提供的代码上的任何 cmets。
标签: python c++ arrays numpy c-api