cython/numpy类型的数组 [英] cython / numpy type of an array
问题描述
我正在尝试构造python类型为int的矩阵,这是一个64位带符号整数.
I am trying to construct a matrix of python type int, a 64bit signed integer.
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())
它编译正确,但是当我运行它时,我不断收到此错误:
It compiles right, but when I run it I keep getting this error:
ValueError: Buffer dtype mismatch, expected 'int' but got 'long'
我无法使用python long的python,但是我不知道如何正确转换为64 int.
I can not work with python long's, but I don't know how to properly convert to a 64 int.
更新
Okey.我确信我正确使用了Cython.我编写的代码用于在捕获go/atari go游戏中进行minmax搜索.
Okey. I am quite sure that I used Cython correctly. The code that I wrote was for an minmax search in the game of capture go / atari go.
到目前为止,最常用的功能是这些:
By far the most called functions are these:
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
我真的以为这将是一个为赛昂大放异彩的机会. 要解决3x3捕获,请执行以下操作:
I would really have thought that this would be a chance to shine for cython. To solve 3x3 capture go:
Python 2.7保持一致的2.28秒,而cython则保持一致的2.03秒 两者均使用python time模块并在低于60°C的i7处理器上进行了测试.
Python 2.7 does a consistent 2.28 seconds, with cython it is a consistent 2.03 Both were tested with the python time module and on an i7 processor of less than 60C°
现在对我来说,问题是我是否要为此项目切换到Haskell或C ++ ...
Now the question for me is if I am going to switch to Haskell or C++ for this project...
推荐答案
Cython的int
类型与C int
相同,即通常(但不一定)为32位.您应该在matrix33
中将dtype
声明为np.int64
,在do_stuf
中将其声明为C对应的np.int64_t
:
Cython's int
type is the same as a C int
, i.e. usually (but not necessarily) 32-bit. You should declare the dtype
in matrix33
as np.int64
and in do_stuf
as its C counterpart, 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())
这篇关于cython/numpy类型的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!