質問

ザ- ufunc.reduce numpyのために。bitwise_and.reduceは正しく動作しないようです。..私はそれを悪用していますか?

>>> import numpy as np
>>> x = [0x211f,0x1013,0x1111]
>>> np.bitwise_or.accumulate(x)
array([ 8479, 12575, 12575])
>>> np.bitwise_and.accumulate(x)
array([8479,   19,   17])
>>> '%04x' % np.bitwise_or.reduce(x)
'311f'
>>> '%04x' % np.bitwise_and.reduce(x)
'0001'

の結果 reduce() の最後の値にする必要があります accumulate() そして、それはありません。私はここで何が欠けていますか?

現時点では、DeMorganのidを使用して回避できます(ORとANDを交換し、入力と出力を反転します):

>>> ~np.bitwise_or.reduce(np.invert(x))
17
役に立ちましたか?

解決

あなたが提供したドキュメントによると, ufunc.reduce 用途 op.identity 初期値として。

numpy.bitwise_and.identity1, 、ない 0xffffffff.... nor -1.

>>> np.bitwise_and.identity
1

だから numpy.bitwise_and.reduce([0x211f,0x1013,0x1111]) と等価である。:

>>> np.bitwise_and(np.bitwise_and(np.bitwise_and(1, 0x211f), 0x1013), 0x1111)
1
>>> 1 & 0x211f & 0x1013 & 0x1111
1

>>> -1 & 0x211f & 0x1013 & 0x1111
17

ドキュメントに従って初期値を指定する方法はないようです。(Pythonの組み込み関数とは異なり reduce)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top