2

numpy を numba で使用しようとしていますが、int に変換された float インデックスを使用して float の numpy 配列にアクセスまたは値を設定しようとすると、奇妙な結果が得られます。この基本機能で確認してください。

@numba.jit("void(f8[:,::1],f8[:,::1])")
def test(table, index):
 x,y = int(index[0,0]), int(index[1,0)
 table[y,x] = 1.0
 print index[0,0], index[1,0], x,y
 print table
 print table[y,x]

table = np.zeros((5,5), dtype = np.float32)
index = np.random.ranf(((2,2)))*5
test(table, index)

結果:

index[0,0] = 1.34129550525 index[1,0] = 0.0656177324359 x = 1 y = 0    
table[0,1] = 1.0 
table [[ 0.     0.     1.875  0.     0.   ]
       [ 0.     0.     0.     0.     0.   ]
       [ 0.     0.     0.     0.     0.   ]
       [ 0.     0.     0.     0.     0.   ]
       [ 0.     0.     0.     0.     0.   ]]

テーブルに 1.0 ではなく 1.875 が表示されるのはなぜですか? これは基本的な例ですが、大きな配列で作業しているため、多くのエラーが発生します。index をnp.int32に変換し、 @numba.jit("void(f8[:,::1],f8[:,::1])")@numba.jit("void(f8 [:,::1],i4[:,::1])")それは正常に機能していますが、なぜこれが機能しないのかを理解していただきたいと思います。Python から C++ への型の解析中に問題がありますか?

助けてくれてありがとう

4

1 に答える 1