2016-12-06 14 views
2

は、次のnumpyの配列arrayを考えてみましょう。削減bitwise_and機能

私はこの配列のbitwise_and削減を実行したい:

y = np.bitwise_and.reduce(x) 

は、私は結果があることを期待:

2 

配列の各要素が同じであるので、その連続したとの得なければなりません同じ結果ですが、代わりに私は得る:

0 

なぜ矛盾ですか? reduceのdocstringで

+0

[関連質問](http://stackoverflow.com/questions/21050875)あなたの現在のPythonのインストールで立ち往生している場合には巧妙な回避策で – gnarledRoot

答えて

3

が、関数が

r = op.identity # op = ufunc 
for i in range(len(A)): 
    r = op(r, A[i]) 
return r 

と同等であることを説明されている問題がnp.bitwise_and.identityが1であるということである:あなたが期待するようreduce方法が動作するように

In [100]: np.bitwise_and.identity 
Out[100]: 1 

については、同一性はすべてのビットが1に設定された整数でなければなりません。

上記のコードはnumpy 1.11.2を使用して実行されました。問題はfixed in the development version of numpyされています:

In [3]: np.__version__ 
Out[3]: '1.13.0.dev0+87c1dab' 

In [4]: np.bitwise_and.identity 
Out[4]: -1 

In [5]: x = np.array([2]*4, dtype=np.uint8) 

In [6]: np.bitwise_and.reduce(x) 
Out[6]: 2