私は、私の研究の分析で使用する次の 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" は、ジャンプと振動の交互動作シーケンスの長さ (時間単位) を保持する 2 つの配列を返します。