2

for numpy.bitwise_and.reduceufunc.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
4

1 に答える 1

2

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

numpy.bitwise_and.identityでは1ありませ0xffffffff....-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)

于 2014-01-10T17:53:49.533 に答える