Is there an erf () implementation available for Python?

I can implement the error function, erf, myself, but I would prefer not to. Is there a python package without external dependencies that contains an implementation of this function? I found this one , but it seems to be part of a much larger package (and it is not even clear which one).

+42
python math
Jan 19 '09 at 12:10
source share
7 answers

Since v.2.7. The standard math module contains the erf function. This should be the easiest way.

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

+55
Jul 12 2018-11-11T00:
source share

I recommend SciPy for numeric functions in Python, but if you want something without dependencies, here is a function with an error error of less than 1.5 * 10 -7 for all inputs.

def erf(x): # save the sign of x sign = 1 if x >= 0 else -1 x = abs(x) # constants a1 = 0.254829592 a2 = -0.284496736 a3 = 1.421413741 a4 = -1.453152027 a5 = 1.061405429 p = 0.3275911 # A&S formula 7.1.26 t = 1.0/(1.0 + p*x) y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x) return sign*y # erf(-x) = -erf(x) 

Algorithm from Reference on mathematical functions , formula 7.1.26.

+41
Jan 19 '09 at 14:46
source share

I would recommend you download numpy (to have an efficient matrix in python) and scipy (a substitute for the Matlab tool that uses numpy). The erf function lies in scipy.

 >>>from scipy.special import erf >>>help(erf) 

You can also use the erf function defined in pylab, but this is more for constructing the results of calculations that you calculate with numpy and scipy. If you want a multifunction device installing this software, you can directly use the Python Enthought distribution .

+24
Jan 19 '09 at 12:47
source share

An implementation of pure python can be found in the mpmath module ( http://code.google.com/p/mpmath/ )

From the document line:

 >>> from mpmath import * >>> mp.dps = 15 >>> print erf(0) 0.0 >>> print erf(1) 0.842700792949715 >>> print erf(-1) -0.842700792949715 >>> print erf(inf) 1.0 >>> print erf(-inf) -1.0 

For large real x , \mathrm{erf}(x) approaches 1 quickly:

 >>> print erf(3) 0.999977909503001 >>> print erf(5) 0.999999999998463 

The error function is an odd function:

 >>> nprint(chop(taylor(erf, 0, 5))) [0.0, 1.12838, 0.0, -0.376126, 0.0, 0.112838] 

: func: erf implements arbitrary precision and supports complex numbers ::

 >>> mp.dps = 50 >>> print erf(0.5) 0.52049987781304653768274665389196452873645157575796 >>> mp.dps = 25 >>> print erf(1+j) (1.316151281697947644880271 + 0.1904534692378346862841089j) 

Related functions

See also: func: erfc , which is more accurate for large x , and: func: erfi , which gives the primitive \exp(t^2) .

Fresnel integrals: func: fresnels and: func: fresnelc also associated with the error function.

+7
Jan 20 '09 at 21:44
source share

I have a function that makes 10 ^ 5 erf calls. On my car ...

scipy.special.erf does this time at 6.1s

erf math functions reference takes 8.3s

erf Numerical Recipes 6.2 takes 9.5s

(middle three runs, code taken from previous posters).

+7
Oct 26 '10 at 6:41
source share

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

 # 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 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 
+6
Jan 19 '09 at 15:54
source share

One note for those seeking higher performance: vectorize if possible.

 import numpy as np from scipy.special import erf def vectorized(n): x = np.random.randn(n) return erf(x) def loopstyle(n): x = np.random.randn(n) return [erf(v) for v in x] %timeit vectorized(10e5) %timeit loopstyle(10e5) 

gives results

 # vectorized 10 loops, best of 3: 108 ms per loop # loops 1 loops, best of 3: 2.34 s per loop 
+4
Dec 23 '13 at 14:37
source share



All Articles