Cython无法识别重载的构造函数

时间:2015-07-30 16:25:03

标签: python c++ constructor cython

我正在尝试使用cython编译cyrand项目,但在测试重载的构造函数时遇到了奇怪的编译错误。有关相关文件,请参阅此gist

从gist中,我可以编译并运行example.pyx就好了,它使用了默认的构造函数:

import numpy as np
cimport numpy as np

cimport cython

include "random.pyx"

@cython.boundscheck(False)
def example(n):

    cdef int N = n

    cdef rng r
    cdef rng_sampler[double] * rng_p = new rng_sampler[double](r)
    cdef rng_sampler[double] rng = deref(rng_p)

    cdef np.ndarray[np.double_t, ndim=1] result = np.empty(N, dtype=np.double)

    for i in range(N):
        result[i] = rng.normal(0.0, 2.0)
    print result
    return result

^这样可以正常运行。示例运行会生成以下输出:

$ python test_example.py [ 0.47237842 3.153744849 3.6854932057 ]

然而,当我尝试编译并运行使用带有unsigned long作为参数的构造函数的测试时:

import numpy as np
cimport numpy as np

cimport cython

include "random.pyx"

@cython.boundscheck(False)
def example_seed(n, seed):

    cdef int N = n
    cdef unsigned long Seed = seed

    cdef rng r
    cdef rng_sampler[double] * rng_p = new rng_sampler[double](Seed)
    cdef rng_sampler[double] rng = deref(rng_p)

    cdef np.ndarray[np.double_t, ndim=1] result = np.empty(N, dtype=np.double)

    for i in range(N):
        result[i] = rng.normal(0.0, 2.0)
    print result
    return result

我收到以下cython编译器错误:

    Error compiling Cython file:
    ----------------------------------------------------------- 
    ...    
       cdef int N = n    
       cdef unsigned long Seed = seed

       cdef rng_sampler[double] * rng_p = new rng_sampler[double](Seed)
    ---------------------------------------------------------- 
    example/example_seed.pyx:15:67 Cannot assign type 'unsigned long' to 'mt19937'

我解释了这条消息,以及example.pyx编译并生成一个工作example.so文件的事实,即cython无法找到(或管理)以无符号长整数作为输入的rng_sampler构造函数。我之前没有使用过cython,而且我的cpp充其量只是中等。任何人都可以解释如何解决这个简单的问题吗?

python:2.7.10(Anaconda 2.0.1) cython:0.22.1

1 个答案:

答案 0 :(得分:0)

我解决了错误,它与如何安装boost有关。我通过apt-get安装了boost。下载/解压缩并在setup.py中更改指针以进行提升后,它可以正常工作。