cython/numpy类型的数组

问题描述:

我想构建一个python类型的int矩阵,一个64位有符号整数。cython/numpy类型的数组

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' 

我不能跟蟒蛇长的工作,但我不知道如何正确地转换为64 INT。

UPDATE

奥基。我很确定我正确使用了Cython。我写的代码是在capture go/atari去的游戏中进行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的捕获去:

的Python 2.7确实一致2.28秒,与用Cython它是一致的2.03 两者都与蟒时间模块和小于60℃°

一个i7处理器立即测试对我来说,问题是,如果我要切换到哈斯克尔或C++为这个项目......

用Cython的int类型是一样为C int,即通常(但不一定)32位。你应该在声明作为matrix33np.int64dtypedo_stuf为相应的C,np.int64_t

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()) 
+0

谢谢你,现在的代码运行正确。但是,它的运行速度与我宣布一切都很长时一样快。我知道长时间的物体并不快,因为它不会溢出并且被对待不同(使用更多的周期)。我想使用一个可以溢出并且比python类型更快的int。 – Ihmahr 2012-02-28 12:04:07

+0

此外,解释蟒蛇的速度超过10%,这让我很怀疑我没有正确的类型... – Ihmahr 2012-02-28 12:10:41

+0

@ user1020753:类型是正确的。加速取决于你在你的功能中做什么。 – 2012-02-28 13:02:18