numpy逐位操作

2024-05-23 23:13:26 发布

您现在位置:Python中文网/ 问答频道 /正文

numpy版本1.9.0

1 & (2**63)
0

np.bitwise_and(1, 2**63)
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

np.bitwise_and(1, 2**63 + 100)
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

np.bitwise_and(1, 2**64)
0

这是虫子还是我遗漏了什么?


Tags: andthetoforinputnpnotbe
1条回答
网友
1楼 · 发布于 2024-05-23 23:13:26

首先转换为uint64

np.bitwise_and(np.uint64(1), np.uint64(2**63))

下面是检查将python integer转换为numpy integer的规则的代码:

print np.array([2**30]).dtype
print np.array([2**31]).dtype
print np.array([2**63]).dtype
print np.array([2**64]).dtype

输出:

int32
int64
uint64
object

我认为np.bitwise_and(1, 2**63)引发错误是因为2**63超出了int32和int64的范围。

np.bitwise_and(1, 2**64)工作,因为它将使用Python的长对象。

我们需要阅读源代码来理解细节。

相关问题 更多 >