如何通过从C ++数组蟒蛇功能和检索蟒蛇返回数组到C ++ [英] how to pass an array from c++ to python function and retrieve python returned array to c++
问题描述
我书面方式C ++程序来调用一个Python函数和检索返回array.but我总是得到一个错误如下:
I'm writting a c++ program to call a python function and retrieve the return array.but i always get an error as below:
仅长度-1阵列可以转换到Python标量
only length-1 arrays can be converted to Python scalars
和我的C ++ code:
and my c++ code:
int main(int argc, char *argv[])
{
int i;
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
if (argc < 3)
{
printf("Usage: exe_name python_source function_name\n");
return 1;
}
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyString_FromString(argv[1]);
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, argv[2]);
if (PyCallable_Check(pFunc))
{
// Prepare the argument list for the call
if( argc > 3 )
{
pArgs = PyTuple_New(argc - 3);
for (i = 0; i < argc - 3; i++)
{
pValue = PyInt_FromLong(atoi(argv[i + 3]));
if (!pValue)
{
PyErr_Print();
return 1;
}
PyTuple_SetItem(pArgs, i, pValue);
}
pValue = PyObject_CallObject(pFunc, pArgs);
if (pArgs != NULL)
{
Py_DECREF(pArgs);
}
} else
{
pValue = PyObject_CallObject(pFunc, NULL);
}
if (pValue != NULL)
{
cout <<pValue;
printf("Return of call : %ld\n", PyInt_AsLong(pValue));
PyErr_Print();
Py_DECREF(pValue);
}
else
{
PyErr_Print();
}
} else
{
PyErr_Print();
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
system("PAUSE");
return 0;
}
Python函数:
Python function:
import numpy as np
_ZERO_THRESHOLD = 1e-9 # Everything below this is zero
def data():
print "process starting..."
N = 5
obs = np.matrix([np.random.normal(size=5) for _ in xrange(N)])
V = pca_svd(obs)
print "V:"
print V[0:5]
pca = IPCA(obs.shape[1], 3)
for i in xrange(obs.shape[0]):
x = obs[i,:].transpose()
print " "
print "new input:"
print x
pca.update(x)
U = pca.components
A = pca.variances
B = U.T*x
print B
return B
我知道有什么不对本声明
I know there is something wrong with this statement
PyInt_AsLong(P值)
谁能告诉我如何解决,为了检索蟒蛇C ++
PyInt_AsLong(pValue) Can anyone tell me how to fix that in order to retrieve the matrix from python to c++
感谢您这么多。
推荐答案
您正在使用 PyInt_AsLong(P值)
来的Python对象转换P值
为C 长
标。如果 P值
是一个numpy的数组,这意味着你正在试图将数组转换为数字,这是唯一可能的长度为1的数组:
You are using PyInt_AsLong(pValue)
to convert the Python object pValue
to a C long
scalar. If pValue
is a Numpy array, this means that you are trying to convert the array to a number, which is only possible for length-1 arrays:
仅长度-1阵列可以转换到Python标量
only length-1 arrays can be converted to Python scalars
而不是使用 PyInt_AsLong
,请使用的 PyArray _ * 功能:// docs.scipy.org/doc/numpy/reference/c-api.html~~V相对=nofollow> numpy的的C API 访问数据;特别是,见阵列API 。
Instead of using PyInt_AsLong
, use the PyArray_*
functions provided by Numpy's C API to access the data; in particular, see section Array API.
这篇关于如何通过从C ++数组蟒蛇功能和检索蟒蛇返回数组到C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!