[英]RuntimeWarning: overflow encountered in cosh -- Python. What does this mean?
我正在运行以下计算:
N = 2**15
dx = 0.1
x = np.arange(-N/2,N/2)
u0 = np.zeros([N, 1])
L = N * dx
x0 = x[1] + 2 * delta
delta = 15
while x0 < L - delta:
l1 = 1.267;
x0 = x0 + delta
r = 1/(l1*np.cosh(x)**2)
u0 = r + u0
本质上,虽然 x0< L - delta 这个循环将运行 2^15 个点。
这可以很好地转换为 MATLAB,但是 python 给了我这个错误:
运行时警告:在 cosh 中遇到溢出
r = 1/(l1*np.cosh(x)**2)
cosh
变得非常大:
In [70]: np.cosh(2**10)
<ipython-input-70-c4511154ec1e>:1: RuntimeWarning: overflow encountered in cosh
np.cosh(2**10)
Out[70]: inf
为什么你的x
这么宽? 对于大部分范围,此cosh
的倒数将为 0。
In [72]: N=2**15; x = np.arange(-N/2,N/2)
In [73]: len(x)
Out[73]: 32768
In [74]: r = 1/(np.cosh(x)**2)
<ipython-input-74-404fbe3be390>:1: RuntimeWarning: overflow encountered in cosh
r = 1/(np.cosh(x)**2)
<ipython-input-74-404fbe3be390>:1: RuntimeWarning: overflow encountered in square
r = 1/(np.cosh(x)**2)
In [75]: r[:10]
Out[75]: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [77]: np.sum(r<1e-16)
Out[77]: 32729
查看x
的 +-20 范围
In [88]: x = np.arange(-20,20)
In [89]: r = 1/(np.cosh(x)**2)
In [90]: r
Out[90]:
array([1.69934170e-17, 1.25565312e-16, 9.27809132e-16, 6.85563373e-15,
5.06566622e-14, 3.74304919e-13, 2.76576004e-12, 2.04363561e-11,
...
1.00000000e+00, 4.19974342e-01, 7.06508249e-02, 9.86603717e-03,
...
1.51005382e-10, 2.04363561e-11, 2.76576004e-12, 3.74304919e-13,
5.06566622e-14, 6.85563373e-15, 9.27809132e-16, 1.25565312e-16])
In [148]: x=np.array([-2**15,-2**4,0,2**4,2**15])
In [155]: np.cosh(x)
<ipython-input-155-1e743139b88e>:1: RuntimeWarning: overflow encountered in cosh
np.cosh(x)
Out[155]:
array([ inf, 4.44305526e+06, 1.00000000e+00, 4.44305526e+06,
inf])
In [156]: 1/(np.cosh(x)**2)
<ipython-input-156-5cf76600c0c7>:1: RuntimeWarning: overflow encountered in cosh
1/(np.cosh(x)**2)
Out[156]:
array([0.00000000e+00, 5.06566622e-14, 1.00000000e+00, 5.06566622e-14,
0.00000000e+00])
该警告不会阻止您获得有用的值。 这是一个警告,而不是一个错误。
但可以抑制警告。 途中是errstate
:
In [157]: with np.errstate(over='ignore'):
...: y = 1/(np.cosh(x)**2)
...:
In [158]: y
Out[158]:
array([0.00000000e+00, 5.06566622e-14, 1.00000000e+00, 5.06566622e-14,
0.00000000e+00])
np.seterr
也可以使用,但它会改变整个脚本的处理,而不仅仅是这个上下文。 所以with np.errstate
是首选。
一定要花时间阅读文档。
In [159]: np.geterr()
Out[159]: {'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}
还有一个warnings
模块
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.