1

我编写了以下 cython 代码,用于分析我的研究。

def jumprate(np.ndarray[FTYPE_t, ndim=1] X ,np.ndarray[LTYPE_t, ndim=1] V):

cdef np.ndarray J = np.zeros([X.shape[0]], dtype=LTYPE)
cdef np.ndarray O = np.zeros([X.shape[0]], dtype=LTYPE)
cdef np.ndarray Vel = np.zeros([X.shape[0]], dtype=FTYPE)    
cdef  long  j=0 # index of J
cdef  long  o=0# index of O
cdef  long  k = 0 # index of X
cdef  long l = 0 # counts length of sequence of same velocity sign
cdef  long  L = V.shape[0]/3
cdef  int jumpstart = 1
cdef  int jumpend = 1    
cdef  long S0 = 0    

while k <  L:  # run over position array
    if V[k]==V[k-1]: # might be a start of jump
        jumpstart = k  # Also where last oscilation ended    
        S0 = V[k]
        while V[k]==S0: # As long as velocity sign doesn't change we might be in a jump
            l += 1 # Count sequences length
            k += 1 # Update position in array
    if int(X[jumpstart]) != int(X[k]): # If start end ending point of sequence are 
                                     # in different grid squares, it's a jump
        J[j] = l # Append jump length to list 
        j += 1
        Vel[j] = (jumpstart-jumpend)/100
        O[o] = abs((jumpend-jumpstart))  # Append oscilation length to list 
        o += 1
        l = 0 
        jumpend = k # mark where last jump ended (also where new oscilation starts)
    k+=1    
return J,O,Vel

注意在第 9 行 L 的定义中除以 3。我在运行时收到以下错误后插入它

while V[k]==S0: # ...
IndexError: Out of bounds on buffer access (axis 0) 

哪种解决了问题。但是,传递给函数的数组的 X,V 中有 99990 个元素,这个解决方案意味着只使用前 33330 个元素。起初我虽然我应该简单地将类型从 int 更改为 long 但它没有帮助。

任何人都可以提出解决问题的方法吗?

那些对代码的目的感兴趣的人,它意味着遵循原子(数组 X)的轨迹,它有时会在势阱中振荡,有时会从一个阱跳到另一个阱。函数“jumprate”返回两个数组,它们保存交替跳跃和振荡运动序列的长度(以时间为单位)。

4

1 回答 1

2

k用 初始化0,并且您正在尝试访问V[k -1]numpy 数组

while k <  L:  # run over position array
    if V[k]==V[k-1]: # might be a start of jump
于 2011-12-06T16:45:41.600 回答