【发布时间】:2020-02-21 16:03:00
【问题描述】:
我正在处理scipy tutorial,我遇到了我不太理解的函数 numpy.real_if_close 的行为:
b = np.array([1+1j, 2+1j, 3+1j, 4+5e-15j])
b
>>>[1.+1.e+00j 2.+1.e+00j 3.+1.e+00j 4.+5.e-15j]
np.real(b)
>>>[1. 2. 3. 4.]
b.real
>>>[1. 2. 3. 4.]
np.imag(b)
>>>[0. 0. 0. 5.]
b.imag
>>>[0. 0. 0. 5.]
np.finfo(float).eps
>>>2.220446049250313e-16
b # <-- to make sure I didn't change b
>>>[1.+1.e+00j 2.+1.e+00j 3.+1.e+00j 4.+5.e-15j]
np.real_if_close(b, tol=1000) # <-- does not perform the approx 4+5e-14j ~= 4
>>>[1.+1.e+00j 2.+1.e+00j 3.+1.e+00j 4.+5.e-15j]
b.real_if_close(tol=1000) # <-- raises an AttributeError
>>>AttributeError: 'numpy.ndarray' object has no attribute 'real_if_close'
np.real_if_close([2.1 + 4e-14j], tol=1000) # <-- example from the tutorial
>>>[2.1]
从real_if_close、real 和imag 的文档中,三个函数的参数以相同的方式描述,因此我对它们对同一对象的不同行为感到有些迷茫。
- 我在这里假设有什么问题吗?
- 这是一个值得报告的问题吗?
【问题讨论】:
-
b.imag的输出看起来不正确。应该是array([1.e+00, 1.e+00, 1.e+00, 5.e-15])。