Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
which is just an array of four 2's.
I want to perform a bitwise_and reduction of this array:
y = np.bitwise_and.reduce(x)
I expect the result to be:
because each element of the array is identical, so successive AND's should yield the same result, but instead I get:
Why the discrepancy?
–
In the reduce
docstring, it is explained that the function is equivalent to
r = op.identity # op = ufunc
for i in range(len(A)):
r = op(r, A[i])
return r
The problem is that np.bitwise_and.identity
is 1:
In [100]: np.bitwise_and.identity
Out[100]: 1
For the reduce
method to work as you expect, the identity would have to be an integer with all bits set to 1.
The above code was run using numpy 1.11.2. The problem has been 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
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.