Error Function Python
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads square root in python with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Ln Python
Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:
Python Cotangent
Sign up Is there an easily available implementation of erf() for Python? up vote 36 down vote favorite 7 I can implement the error function, erf, myself, but I'd prefer not to. Is there a python package with no external dependencies
Python Scipy Error Function
that contains an implementation of this function? I have found http://pylab.sourceforge.net/packages/included_functions.html>this but this seems to be part of some much larger package (and it's not even clear which one!). I'm sorry if this is a naive question - I'm totally new to Python. python math share|improve this question asked Jan 19 '09 at 12:10 rog 2,19211721 add a comment| 7 Answers 7 active oldest votes up vote 43 down vote Since v.2.7. the standard math module contains erf function. This should be the python error handling easiest way. http://docs.python.org/2/library/math.html#math.erf share|improve this answer edited Nov 19 '13 at 14:28 Colonel Panic 52.6k33221275 answered Jul 12 '11 at 9:31 bezalel 58146 1 +1: simplest answer. –Neil G Dec 21 '11 at 4:42 Wow! Never noticed that! –smci May 20 '13 at 23:30 Is there a Python module that provides erf⁻¹(x) ? –Lori Feb 1 '15 at 22:49 add a comment| up vote 39 down vote I recommend SciPy for numerical functions in Python, but if you want something with no dependencies, here is a function with an error error is 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) The algorithm comes from Handbook of Mathematical Functions, formula 7.1.26. share|improve this answer edited Dec 21 '11 at 4:41 Neil G 11k1782168 answered Jan 19 '09 at 14:46 John D. Cook 22k75888 This code gives a division-by-zero error for erf(0.0). –rog Jan 19 '09 at 15:56 You're right. I edited my answer to find the sign of x a different way to fix this problem. Now it's OK. –John D
module is always available. It provides access to the mathematical functions defined by the C standard. These functions cannot be used with complex numbers; use python error function numpy the functions of the same name from the cmath module python math error function if you require support for complex numbers. The distinction between functions which support complex numbers and python error function not defined those which don't is made since most users do not want to learn quite as much mathematics as required to understand complex numbers. Receiving an exception instead http://stackoverflow.com/questions/457408/is-there-an-easily-available-implementation-of-erf-for-python of a complex result allows earlier detection of the unexpected complex number used as a parameter, so that the programmer can determine how and why it was generated in the first place. The following functions are provided by this module. Except when explicitly noted otherwise, all return values are floats. 9.2.1. Number-theoretic and representation functions¶ https://docs.python.org/2/library/math.html math.ceil(x)¶ Return the ceiling of x as a float, the smallest integer value greater than or equal to x. math.copysign(x, y)¶ Return x with the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0) returns -1.0. New in version 2.6. math.fabs(x)¶ Return the absolute value of x. math.factorial(x)¶ Return x factorial. Raises ValueError if x is not integral or is negative. New in version 2.6. math.floor(x)¶ Return the floor of x as a float, the largest integer value less than or equal to x. math.fmod(x, y)¶ Return fmod(x, y), as defined by the platform C library. Note that the Python expression x % y may not return the same result. The intent of the C standard is that fmod(x, y) be exactly (mathematically; to infinite precision) equal to x - n*y for some integer n such that the result has the same sign as x and magni
module is always available. It provides access to the mathematical functions defined by the C standard. These functions cannot be https://docs.python.org/3/library/math.html used with complex numbers; use the functions of the same name from the cmath module if you require support for complex numbers. The distinction between functions which support complex numbers and those which don't is made since most users do not want to learn quite as much mathematics as error function required to understand complex numbers. Receiving an exception instead of a complex result allows earlier detection of the unexpected complex number used as a parameter, so that the programmer can determine how and why it was generated in the first place. The following functions are provided by this module. Except when python error function explicitly noted otherwise, all return values are floats. 9.2.1. Number-theoretic and representation functions¶ math.ceil(x)¶ Return the ceiling of x, the smallest integer greater than or equal to x. If x is not a float, delegates to x.__ceil__(), which should return an Integral value. math.copysign(x, y)¶ Return a float with the magnitude (absolute value) of x but the sign of y. On platforms that support signed zeros, copysign(1.0, -0.0) returns -1.0. math.fabs(x)¶ Return the absolute value of x. math.factorial(x)¶ Return x factorial. Raises ValueError if x is not integral or is negative. math.floor(x)¶ Return the floor of x, the largest integer less than or equal to x. If x is not a float, delegates to x.__floor__(), which should return an Integral value. math.fmod(x, y)¶ Return fmod(x, y), as defined by the platform C library. Note that the Python expression x