0
我想在Python中使用Cython和numpy來包裝C函數。如何修復編譯器錯誤:不能轉換爲Cython + numpy的指針類型?
我編譯時出現以下錯誤:
_fastlp.c: In function ‘__pyx_pf_6fastlp_fastlp_func’:
_fastlp.c:1351:3: error: cannot convert to a pointer type
fastlp(((double *)PyArray_DATA(((PyArrayObject *)__pyx_v_obj))),
((double *)PyArray_DATA(((PyArrayObject *)__pyx_v_mat))),
((double *)PyArray_DATA(((PyArrayObject *)__pyx_v_rhs))), (&__pyx_v_m0),
(&__pyx_v_n0), ((double *)PyArray_DATA(__pyx_v_opt)),
(&__pyx_v_status), ((double *)__pyx_v_lam));
^
箭頭^
點(&__pyx_v_status)
。
我.pyx
文件是:
import numpy as np
cimport numpy as np
np.import_array()
# cdefine the signature of the c function
cdef extern from "fastlp.h":
void fastlp(double *obj, double *mat, double *rhs, int *m0 , int *n0,
double *opt, int *status, double *lam)
def fastlp_func(np.ndarray[double, ndim=1, mode="c"] obj not None,
np.ndarray[double, ndim=2, mode="c"] mat not None,
np.ndarray[double, ndim=1, mode="c"] rhs not None,
double lam):
#Define output
cdef np.ndarray opt = np.zeros((len(obj),), dtype = np.float64)
cdef int status = 0
#Call external C function
cdef int m0 = mat.shape[0]
cdef int n0 = mat.shape[1]
fastlp(<double*> np.PyArray_DATA(obj),
<double*> np.PyArray_DATA(mat),
<double*> np.PyArray_DATA(rhs),
&m0,
&n0,
<double*> np.PyArray_DATA(opt),
&status,
<double*> lam)
return (opt,status)
任何幫助,不勝感激!我一直在研究這麼久,我想我很接近。謝謝!