Python 数组的cython/numpy类型
我试图构造python类型int的矩阵,一个64位有符号整数Python 数组的cython/numpy类型,python,types,numpy,python-2.7,cython,Python,Types,Numpy,Python 2.7,Cython,我试图构造python类型int的矩阵,一个64位有符号整数 cdef matrix33(): return np.zeros((3,3),dtype=int) cdef do_stuf(np.ndarray[int, ndim=2] matrix): ... return some_value def start(): print do_stuf(matrix33()) 它可以正确编译,但当我运行它时,会不断出现以下错误: ValueError: Buff
cdef matrix33():
return np.zeros((3,3),dtype=int)
cdef do_stuf(np.ndarray[int, ndim=2] matrix):
...
return some_value
def start():
print do_stuf(matrix33())
它可以正确编译,但当我运行它时,会不断出现以下错误:
ValueError: Buffer dtype mismatch, expected 'int' but got 'long'
我不能使用python long,但我不知道如何正确地转换为64 int
更新
好的。我确信我正确地使用了Cython。我写的代码是为了在capture go/atari go游戏中进行minmax搜索
到目前为止,调用最多的函数有:
cdef isThere_greedy_move(np.ndarray[np.int64_t, ndim=2]board, int player):
cdef int i, j
for i in xrange(len(board)):
for j in xrange(len(board)):
if board[i,j] == 0:
board[i,j] = player
if player in score(board):
board[i,j] = 0
return True
board[i,j] = 0
return False
# main function of the scoring system.
# returns list of players that eat a stone
cdef score(np.ndarray[np.int64_t, ndim=2] board):
scores = []
cdef int i,j
cdef np.ndarray[np.int64_t, ndim = 2] checked
checked = np.zeros((board.shape[0], board.shape[1]), dtype = int)
for i in xrange(len(board)):
for j in xrange(len(board)):
if checked[i,j] == 0 and board[i,j] !=0:
life, newly_checked = check_life(i,j,board,[])
if not life:
if -board[i,j] not in scores:
scores.append(-board[i,j])
if len(scores) == 2:
return scores
checked = update_checked(checked, newly_checked)
return scores
# helper functions of score/1
cdef check_life(int i, int j, np.ndarray[np.int64_t, ndim=2] board, checked):
checked.append((i,j))
if liberty(i,j,board):
return True, checked
for pos in [[1,0],[0,1],[-1,0],[0,-1]]:
pos = np.array([i,j]) + np.array(pos)
if check_index(pos[0],pos[1],len(board)) and board[pos[0],pos[1]] == board[i,j] and (pos[0],pos[1]) not in checked:
life, newly_checked = check_life(pos[0],pos[1],board,checked)
if life:
checked = checked + newly_checked
return life, checked
return False, [] # [] is a dummy.
cdef liberty(int i,int j, np.ndarray[np.int64_t, ndim=2] board):
for pos in [np.array([1,0]),np.array([0,1]),np.array([-1,0]),np.array([0,-1])]:
pos = np.array([i,j]) - pos
if check_index(pos[0],pos[1],len(board)) and board[pos[0],pos[1]] == 0:
return True
return False
我真的以为这会是cython发光的机会。
要解决3x3捕获问题,请执行以下操作:
Python2.7执行的是一致的2.28秒,cython执行的是一致的2.03秒
两者都是在python时间模块和低于60°C的i7处理器上进行测试的
现在我要问的是,如果我要切换到Haskell或C++,这个项目…< /P> < P> Cython的代码> int >代码>类型与C <代码> int <代码>相同,即通常(但不一定)32位。您应该将
matrix33
中的dtype
声明为np.int64
,并将do\u stuf
中的np.int64\t
声明为其C对应项:
cimport numpy as np
import numpy as np
cdef do_stuff(np.ndarray[np.int64_t, ndim=2] matrix):
pass
cdef matrix33():
return np.zeros((3,3), dtype=int)
def start():
print do_stuff(matrix33())
谢谢,代码现在运行正确。但是,它的运行速度和我声明所有内容时一样快。我知道长对象的速度不快,因为它不会溢出,并且被视为不同的对象(使用更多的周期)。我想使用一个可以溢出的int,它比python类型long快。而且,在解释python上的速度略低于10%,这让我怀疑我没有完全正确的类型…@user1020753:类型是正确的。加速取决于您在函数中所做的操作。@user1020753您的函数在大多数操作中都使用Python数据类型-列表、元组等。为了使Cython发挥更大的作用,需要重写它以使用本机C数据类型。此外,如果您键入函数,可能会加快速度。看见