我正在将Cython内存视图转换为numpy数组(以便能够在纯Python代码中使用它):
from libc.stdlib cimport realloc
cimport numpy as np
DTYPE = np.float64
ctypedef np.float64_t DTYPE_t
cpdef np.ndarray[DTYPE_t] compute(DTYPE_t[:,::1] data):
cdef unsigned int Nchannels = data.shape[0]
cdef unsigned int Ndata = data.shape[1]
cdef DTYPE_t* output = NULL
cdef DTYPE_t[::1] mv
output = <DTYPE_t*>realloc(output, Ndata*sizeof(output))
if not output:
raise MemoryError()
mv = <DTYPE_t[:Ndata]>output
mv[10:Ndata-10] = 0.0
# various calculations...
return np.asarray(mv, dtype=DTYPE, order='C')
Run Code Online (Sandbox Code Playgroud)
它编译,但编译器给出以下警告:
/Users/vlad/anaconda/lib/python2.7/site-packages/numpy/core/include
/nump/npy_1_7_deprecated_api.h:15:2: warning:
"Using deprecated NumPy API, disable it by #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
Run Code Online (Sandbox Code Playgroud)
我在setup.py中添加了建议的指令:
from distutils.core import …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已经被回答了很多次,我也阅读了文档,但我仍然无法清楚地理解这是如何工作的.在中,我无法理解如何在其参数中填充值.这些例子不是很清楚地解释它(或者可能是我不能).谁能帮助我了解这个函数的参数是如何填充的?他们的价值观应该是什么?我必须将一个从C++传递到Python的向量而不重新分配内存.任何帮助深表感谢.很多天我都坚持这个.
我正在实现的代码:
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs,*pXVec,*c, *xarr1;
int i;
float fArray[5] = {0,1,2,3,4};
//float *p = &fArray[0] ;
npy_intp m = 5;
//void* PyArray_GetPtr(PyArrayObject* aobj, npy_intp* ind)¶
// Initialize the Python Interpreter
Py_Initialize();
PySys_SetArgv(argc, argv);
// Build the name object
pName = PyString_FromString(argv[1]);
// Load the module object
pModule = PyImport_Import(pName);
printf("check0\n");
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
printf("check1\n");
// pFunc is also a borrowed reference
pFunc = …Run Code Online (Sandbox Code Playgroud)