我可以实现错误功能,erf,我自己,但我不愿意。是否有一个没有外部依赖项的python包,其中包含此函数的实现?我已经找到了这个,但这似乎是一些更大的包的一部分(它甚至不清楚哪一个!)。
发布于 2018-09-20 09:31:52
为了回答我自己的问题,我最终使用了以下代码,该代码改编自我在网络上其他地方找到的Java版本:
# from: http://www.cs.princeton.edu/introcs/21function/ErrorFunction.java.html
# Implements the Gauss error function.
# erf(z) = 2 / sqrt(pi) * integral(exp(-t*t), t = 0..z)
#
# fractional error in math formula less than 1.2 * 10 ^ -7.
# although subject to catastrophic cancellation when z in very close to 0
# from Chebyshev fitting formula for erf(z) from Numerical Recipes, 6.2
def erf(z):
t = 1.0 / (1.0 + 0.5 * abs(z))
# use Horner's method
ans = 1 - t * math.exp( -z*z - 1.26551223 +
t * ( 1.00002368 +
t * ( 0.37409196 +
t * ( 0.09678418 +
t * (-0.18628806 +
t * ( 0.27886807 +
t * (-1.13520398 +
t * ( 1.48851587 +
t * (-0.82215223 +
t * ( 0.17087277))))))))))
if z >= 0.0:
return ans
else:
return -ans
发布于 2018-09-20 11:00:47
从v.2.7开始。标准数学模块包含erf函数。这应该是最简单的方法。
https://stackoverflow.com/questions/-100000785
复制相似问题