首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否有一个容易实现的erf()为Python实现?

是否有一个容易实现的erf()为Python实现?
EN

Stack Overflow用户
提问于 2018-09-20 01:08:18
回答 2查看 0关注 0票数 0

我可以实现错误功能,erf,我自己,但我不愿意。是否有一个没有外部依赖项的python包,其中包含此函数的实现?我已经找到了这个,但这似乎是一些更大的包的一部分(它甚至不清楚哪一个!)。

EN

回答 2

Stack Overflow用户

发布于 2018-09-20 09:31:52

为了回答我自己的问题,我最终使用了以下代码,该代码改编自我在网络上其他地方找到的Java版本:

代码语言:javascript
复制
# 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
票数 0
EN

Stack Overflow用户

发布于 2018-09-20 11:00:47

从v.2.7开始。标准数学模块包含erf函数。这应该是最简单的方法。

http://docs.python.org/2/library/math.html#math.erf

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100000785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档