以下
from scipy.special import gamma
gamma(x)
大型x
的溢出。这就是为什么scipy提供了gammaln
,它相当于np.log(gamma(x))
,允许我们在日志空间中工作并避免溢出
scipy的^{x
没有下溢:
import numpy as np
from scipy.special import exp1
def exp1ln(x):
return np.log(exp1(x))
(我认为这类似于gammaln
的原因是exp1
属于同一个函数族,请参见此处:Incomplete Gamma function in scipy。)
谢谢
对于任何想要只处理单个数字而不是数组的
exp1ln
版本的人,我修改了orlp中的解决方案以获得以下结果:对于realx,您可以使用log的系列扩展(Ei(x)来表示x→ ∞ (参见here),这对于大型
x
非常准确通过一些快速实验,当x>;=50(及 这就是
scipy
开始失去全部精度的地方)。而且系列的扩展也很不错, 系数是阶乘数,因此我们可以使用精确的计算,而不会产生灾难性的后果 取消:与WolframAlpha(可提供数百位数字)的比较:
相关问题 更多 >
编程相关推荐