Pylab Error Function
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 python inverse error function Overflow the company Business Learn more about hiring developers or posting ads with us scipy erfc Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a module 'scipy' has no attribute 'special' community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Is there an easily available implementation of erf() for Python? up vote 36 down vote error function wiki 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 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
Pyplot Errorbar
question asked Jan 19 '09 at 12:10 rog 2,21211721 add a comment| 7 Answers 7 active oldest votes up vote 44 down vote Since v.2.7. the standard math module contains erf function. This should be the 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 53.3k33221278 answered Jul 12 '11 at 9:31 bezalel 59146 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
Exceptions are noted. Error handling¶ Errors are handled by returning nans, or other appropriate values. Some of the special function
Pyplot Plot
routines will emit warnings when an error occurs. By default pyplot scatter this is disabled. To enable such messages use errprint(1), and to disable such messages use pyplot legend errprint(0). Example: >>> print scipy.special.bdtr(-1,10,0.3) >>> scipy.special.errprint(1) >>> print scipy.special.bdtr(-1,10,0.3) errprint([inflag]) Sets or returns the error printing flag for special functions. SpecialFunctionWarning Warning that can http://stackoverflow.com/questions/457408/is-there-an-easily-available-implementation-of-erf-for-python be issued with errprint(True) Available functions¶ Airy functions¶ airy(z) Airy functions and their derivatives. airye(z) Exponentially scaled Airy functions and their derivatives. ai_zeros(nt) Compute nt zeros and values of the Airy function Ai and its derivative. bi_zeros(nt) Compute nt zeros and values of the Airy function Bi and its derivative. https://docs.scipy.org/doc/scipy/reference/special.html itairy(x) Integrals of Airy functions Elliptic Functions and Integrals¶ ellipj(u,m) Jacobian elliptic functions ellipk(m) Complete elliptic integral of the first kind. ellipkm1(p) Complete elliptic integral of the first kind around m = 1 ellipkinc(phi,m) Incomplete elliptic integral of the first kind ellipe(m) Complete elliptic integral of the second kind ellipeinc(phi,m) Incomplete elliptic integral of the second kind Bessel Functions¶ jv(v,z) Bessel function of the first kind of real order and complex argument. jn(v,z) Bessel function of the first kind of real order and complex argument. jve(v,z) Exponentially scaled Bessel function of order v. yn(n,x) Bessel function of the second kind of integer order and real argument. yv(v,z) Bessel function of the second kind of real order and complex argument. yve(v,z) Exponentially scaled Bessel function of the second kind of real order. kn(n,x) Modified Bessel function of the second kind of integer order n kv(v,z) Modified Bessel function of
Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough http://rajeshrinet.github.io/blog/2014/numpy-matplotlib/ to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better error function than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! Plotting using matplotlib In[2]: %matplotlib inline import numpy as pylab error function np import matplotlib.pyplot as plt x = np.linspace(0, 4*np.pi, 64) plt.plot(x, np.sin(x), '*-'); We can improve on the above plot in several ways. E.g setting axis labels. setting axis limits. chossing color of our choice. putting legends ... In the plot below we have included some of these features along with another plot in the same figure. This can be used to compare between two plots on the same figure. In[13]: %matplotlib inline import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 4*np.pi, 64) plt.plot(x, np.sin(x), color="#348ABD", linewidth=2, linestyle="-", label='sin(x)'); plt.plot(x, np.cos(x), color="#A60628", linewidth= 3, linestyle="-", label='cos(x)'); plt.xlim([0, 4*np.pi]); plt.xlabel('x'); plt.legend(loc="lower left") Out[13]: