Is there an easily available implementation of erf() for Python?

后端 未结 8 2015
日久生厌
日久生厌 2020-12-24 04:54

I can implement the error function, erf, myself, but I\'d prefer not to. Is there a python package with no external dependencies that contains an implementation of this func

8条回答
  •  不思量自难忘°
    2020-12-24 05:31

    To answer my own question, I have ended up using the following code, adapted from a Java version I found elsewhere on the web:

    # 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
    

提交回复
热议问题