我是Cython的新手,下面的代码用于初始化 A 类:
a.pyx
from __future__ import division
cimport cython
from libcpp cimport bool
cimport numpy as np
import numpy as np
DTYPE = np.float64
ctypedef np.float64_t DTYPE_t
TTYPE = np.int64
ctypedef np.int64_t TTYPE_t
cdef class A():
@cython.boundscheck(False)
@cython.wraparound(False)
def __init__(self, np.ndarray[DTYPE_t, ndim=2, mode='c'] _cost_matrix):
cdef np.ndarray[DTYPE_t, ndim=2] _cost_matrix = np.atleast_2d(_cost_matrix)
...
错误是编译Cython文件时出错:
------------------------------------------------------------
@cython.boundscheck(False)
@cython.wraparound(False)
def __init__(self, np.ndarray[DTYPE_t, ndim=2, mode='c'] _cost_matrix):
linear_assignment_cython.pyx:72:23: Previous declaration is here
Traceback (most recent call last):
------------------------------------------------------------
setup.py
# coding: UTF-8
"""
@author: samuel ko
"""
from distutils.core import setup
from Cython.Build import cythonize
import numpy
setup(
name="haha",
ext_modules=cythonize("a.pyx"),
include_dirs=[numpy.get_include()],
)
我想知道numpy的include_dirs
的位置需要明确指出绝对数字吗?
我的numpy核心路径的目录为:/usr/local/lib/python3.6/dist-packages/numpy/core/include
,我将其更改为替换numpy.get_include()
但仍然无法正常工作。
真的希望您的好心帮助,非常感谢^。^〜
答案 0 :(得分:1)
您已两次定义_cost_matrix
。一次作为函数输入,一次作为局部变量。第二个赋值(在np.atleast_2d
之后)是没有意义的,因为您已经确保函数输入是2D数组。
您可能会做得更好:
def __init__(self, _cost_matrix_in): # untyped variable in, different name
# then ensure it's 2D and enforce the type.
cdef np.ndarray[DTYPE_t, ndim=2] _cost_matrix = np.atleast_2d(_cost_matrix)