问题描述
我需要在 -150
$ b <$ < p $ p> import numpy as np
from scipy.special import ndtr
def my_func(x):
return np.exp(x ** 2)* 2 * ndtr(x * np.sqrt(2))
问题是,部分功能
np.exp(x ** 2)
pre>
倾向于无穷大 - 我得到了
inf
,其值x
小于大约-26
。
以及这部分函数
2 * ndtr(x * np.sqrt(2))
相当于
from scipy.special import erf
1 + erf(x)
倾向于0。 b $ b
所以,非常非常大的数字乘以一个非常非常小的数字应该给我一个合理大小的数字 - 但是,而不是那个
python
给我nan
。
我能做些什么来规避这个问题? / p>
解决方案已经有这样的功能:。我认为
erfcx(-x)
应该给你你想要的被积函数(注意1 + erf(x)= erfc(-x) code>)。
I need to compute the integral of the following function within ranges that start as low as
-150
:import numpy as np from scipy.special import ndtr def my_func(x): return np.exp(x ** 2) * 2 * ndtr(x * np.sqrt(2))
The problem is that this part of the function
np.exp(x ** 2)
tends toward infinity -- I get
inf
for values ofx
less than approximately-26
.And this part of the function
2 * ndtr(x * np.sqrt(2))
which is equivalent to
from scipy.special import erf 1 + erf(x)
tends toward 0.
So, a very, very large number times a very, very small number should give me a reasonably sized number -- but, instead of that,
python
is giving menan
.What can I do to circumvent this problem?
解决方案There already is such a function:
erfcx
. I thinkerfcx(-x)
should give you the integrand you want (note that1+erf(x)=erfc(-x)
).这篇关于诱骗numpy / python代表非常大和非常小的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!