如何修复编译错误:无法转换为Cython + numpy的指针类型?

时间:2015-04-01 02:10:48

标签: python numpy cython

我正在尝试使用Cython和numpy包装C函数以在Python中使用。

编译时出现以下错误:

_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)

非常感谢任何帮助!我一直在研究这个问题已经很久了,我觉得我很接近。谢谢!

1 个答案:

答案 0 :(得分:1)

原来我只需要在调用外部函数时使用<double*> &lam。我试图将变量强制转换为指针...当我不应该这样做时。