2009-12-22 80 views
4

我试图总结它有很多的功能,这样扩展numpy的与用Cython

test.h

void test(int N, int* data_in, int* data_out); 

,这样我可以使用那些从numpy的头文件。

现在我有以下用Cython代码:

test.pyx

import numpy as np 
cimport numpy as np 

ctypedef np.int_t itype_t 

cdef extern from 'VolumeForm.h': 
    void _test 'test' (int, int*, int*) 

def wrap_test(np.ndarray[itype_t, ndim=2] data): 
    cdef np.ndarray[dtype_t, ndim=1] out 
    out = np.zeros((data.shape[0],1), dtype=np.double) 
    _test(
     data.shape[0], 
     <itype_t*> data.data, 
     <itype_t*> out.data 
    ) 
    return out 

然而,当我尝试编译它,我得到的错误:

Error converting Pyrex file to C: 
(...) 
Cannot assign type 'test.itype_t *' to 'int *' 

如何我能解决这个问题吗?

回答

4

此问题目前正在Cython邮件列表上讨论;显然它从一个小bug茎在用Cython库之一:

http://codespeak.net/mailman/listinfo/cython-dev

目前,一个潜在的解决方法是使用NumPy的阵列D型细胞np.long,然后写“ctypedef np.long_t itype_t'代替。然后,你只需要让C代码满意,而不是整数。

2

另一个解决方法,不需要您将事情从int s更改为long s:更改cdef extern from '...'块中的函数签名。 Cython使用cdef extern块中的声明仅在生成.c文件时检查类型,但生成的C代码只是执行#include "VolumeForm.h",因此您可以避开它。

import numpy as np 
cimport numpy as np 

ctypedef np.int_t itype_t 

cdef extern from 'VolumeForm.h': 
    # NOTE: We changed the int* declarations to itype_t* 
    void _test 'test' (int, itype_t*, itype_t*) 

def wrap_test(np.ndarray[itype_t, ndim=2] data): 
    cdef np.ndarray[dtype_t, ndim=1] out 
    out = np.zeros((data.shape[0],1), dtype=np.double) 
    _test(
     data.shape[0], 
     <itype_t*> data.data, 
     <itype_t*> out.data 
    ) 
    return out 

用Cython不会抱怨以上。