numpy.bitwise_and.reduce表现异常? [英] numpy.bitwise_and.reduce behaving unexpectedly?

查看:91
本文介绍了numpy.bitwise_and.reduce表现异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy的 ufunc.reduce .bitwise_and.reduce似乎无法正常运行...我滥用它了吗?

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]
>>> 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()的最后一个值,而不是.我在这里想念什么?

The result of reduce() should be the last value of accumulate() and it's not. What am I missing here?

目前,我可以通过使用DeMorgan的身份(交换OR和AND,以及反转输入和输出)来解决此问题:

For the moment, I can work around by using DeMorgan's identity (swapping OR and AND, and inverting input and output):

>>> ~np.bitwise_or.reduce(np.invert(x))
17

推荐答案

根据您提供的文档,ufunc.reduce使用op.identity作为初始值.

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

numpy.bitwise_and.identity1,不是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 )

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

这篇关于numpy.bitwise_and.reduce表现异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆