我正在 Sage Cell Server 中编译此 Cython 代码我收到以下错误。
undeclared name not builtin: array
它在 Sage Notebook 中显示相同的错误。我认为它无法识别 numpy array
但它
很奇怪,因为我已经导入了 numpy。
cython('''
cimport numpy as np
ctypedef np.int DTYPE
def computeDetCy(np.ndarray[DTYPE, ndim=2] matrix):
return determ(matrix,len(matrix))
cdef inline int determ(np.ndarray[DTYPE, ndim=2] matrix, int n):
cdef int det = 0
cdef int p=0
cdef int h
cdef int k
cdef int i=0
cdef int j=0
cdef np.ndarray[DTYPE, ndim=2] temp=np.zeros(4,4)
if n == 1:
return matrix[0][0]
elif n == 2:
return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]
else:
for p in range(0, n):
h = 0
k = 0
for i in range(1, n):
for j in range(0, n):
if j==p:
continue
temp[h][k] = matrix[i][j]
k+=1
if k ==(n-1):
h+=1
k=0
det= det + matrix[0][p] * (-1)**p * determ(temp, n-1)
return det
computeDetCy(array([[13,42,43,22],[12,67,45,98],[23,91,18,54],[34,56,82,76]]))
''')
最佳答案
是的,但是您将其导入为 np
,而不是导入 *
(无论如何这都是一个坏主意)并且没有进行常规的 Python 导入。 (有时您必须同时执行 cimport
和 import
,请参阅 this SO question for an example。)
但是,即使之后
import numpy as np
并使用np.array
,我仍然遇到一些错误
ValueError: Buffer dtype mismatch, expected 'DTYPE' but got 'long'
所以这解决了您的问题,但并不是全部,而且我尝试的方法并不能解决这个新问题。
关于numpy - 赛通错误 : Undeclared name not built in:array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16175973/