Skip to content Skip to sidebar Skip to footer

Numpy.bitwise_and.reduce Behaving Unexpectedly?

The ufunc.reduce for numpy.bitwise_and.reduce does not appear to behave properly... am I misusing it? >>> import numpy as np >>> x = [0x211f,0x1013,0x1111] >&g

Solution 1:

According to the documentation you provided, ufunc.reduce uses op.identity as an initial value.

numpy.bitwise_and.identity is 1, not 0xffffffff.... nor -1.

>>>np.bitwise_and.identity
1

So numpy.bitwise_and.reduce([0x211f,0x1013,0x1111]) is equivalent to:

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

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

There seems to be no way to specify the initial value according to the documentation. (unlike Python builtin function reduce)

Post a Comment for "Numpy.bitwise_and.reduce Behaving Unexpectedly?"