Jupyter Notebooks 不正确地计算 numpy 的共轭幂。

huangapple go评论58阅读模式
英文:

Jupyter Notebooks incorrectly calculating numpy conjugation to a power

问题

在我的Jupyter Notebook中出现了一些我无法解释的错误。我有以下代码。

```python
import numpy as np
c = 100
c_conj = np.conjugate(c)
print(c == c_conj)
print(c**5 == c_conj**5)

结果输出为

True
False

在JupyterLite(在线Jupyter Notebook软件)上,我也得到了相同的结果。另外,如果我在任何其他平台上运行相同的代码(例如Google Collab),我会得到以下输出

True
True

这是用户错误吗?有没有办法解释这个现象?


<details>
<summary>英文:</summary>

I happened upon some error in my Jupyter Notebook that I can&#39;t explain. I have the following code.

import numpy as np
c = 100
c_conj = np.conjugate(c)
print(c == c_conj)
print(c5 == c_conj5)


Resulting in the output

True
False


I get the same result for JupyterLite (the online Jupyter Notebook software). Alternatively, if I run the same code on any other platform (e.g. Google Collab), I get the output

True
True


Is this user error? Is there a way to explain this?

</details>


# 答案1
**得分**: 3

`numpy.conjugate(100)` 返回一个 `numpy.int_` 实例,而不是普通的 Python `int`。 `numpy.int_` 对应于 C 的 `long`。在比较结果为 `False` 的平台上,C 的 `long` 是32位,而 `c_conj**5` 的计算溢出。

<details>
<summary>英文:</summary>

`numpy.conjugate(100)` returns a `numpy.int_` instance, not a plain Python `int`. `numpy.int_` corresponds to C `long`. On the platforms where the comparison evaluated to `False`, C `long` is 32 bits, and the `c_conj**5` computation overflowed.

</details>



huangapple
  • 本文由 发表于 2023年2月24日 03:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定