Numpy Error Function
Contents |
2/sqrt(pi)*integral(exp(-t**2), t=0..z). Parameters:x python erfc : ndarray Input array. Returns:res : ndarray The values of
Module 'scipy' Has No Attribute 'special'
the error function at the given points x. See also erfc, erfinv, erfcinv Notes The cumulative of the unit normal distribution error function wiki is given by Phi(z) = 1/2[1 + erf(z/sqrt(2))]. References [R200]http://en.wikipedia.org/wiki/Error_function [R201]Milton Abramowitz and Irene A. Stegun, eds. Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables. New York: Dover, 1972. http://www.math.sfu.ca/~cbm/aands/page_297.htm [R202]Steven G. Johnson, Faddeeva W function implementation. http://ab-initio.mit.edu/Faddeeva Previous topic scipy.special.multigammaln Next topic scipy.special.erfc © Copyright 2008-2009, The Scipy community. Last updated on May 11, 2014. Created using Sphinx 1.2.2.
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
Python Cumulative Distribution Function
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
Scipy Special
Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping complementary error function each other. Join them; it only takes a minute: Sign up command for inverse ERF function in python [closed] up vote 7 down vote favorite What is the command to calculate Inverse Error function (erf) of a function https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.special.erf.html in a python and which module is needed to import? python python-2.7 python-3.x numpy share|improve this question asked Jul 7 '15 at 10:37 Naitik Mathur 442 closed as unclear what you're asking by jonrsharpe, ekad, cel, HaveNoDisplayName, Soner Gönül Jul 7 '15 at 14:50 Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page http://stackoverflow.com/questions/31266249/command-for-inverse-erf-function-in-python for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question. add a comment| 2 Answers 2 active oldest votes up vote 10 down vote For the inverse error function, scipy.special has erfinv: http://docs.scipy.org/doc/scipy/reference/generated/scipy.special.erfinv.html#scipy.special.erfinv In [4]: from scipy.special import erfinv In [5]: erfinv(1) Out[5]: inf In [6]: erfinv(0.4) Out[6]: 0.37080715859355784 share|improve this answer answered Jul 7 '15 at 10:41 xnx 11.1k31541 add a comment| up vote 0 down vote I suggest to use scipy, a library that uses numpy. the module you need to import to use is erfinv: from scipy.special import erfinv Scipy is a key player for numerical software in Python. But it might be a little challenging getting started with it. share|improve this answer edited Jul 7 '15 at 10:46 answered Jul 7 '15 at 10:40 DJanssens 1,8143826 add a comment| Not the answer you're looking for? Browse other questions tagged python python-2.7 python-3.x numpy or ask your own question. asked 1 year ago viewed 1657 times active 1 year ago Get the weekly newsletter! In it, you'll get: The week's top questions and answers Important community announcements Questions that need answers see an example newsletter Related 1146How can I represent an 'Enum' in Python?2316Calling an external command in Python5532What does the “yield” keyword do?3use of // in python1How can I change the defau
19 January 2009 by John The question came up on StackOverflow this morning how to compute the error function erf(x) in Python. The standard answer for how to compute anything numerical in Python is "Look in SciPy." However, this person didn't want to take on the dependence on SciPy. I've seen http://www.johndcook.com/blog/2009/01/19/stand-alone-error-function-erf/ variations on this question come up in several different contexts lately, including questions https://mail.python.org/pipermail/tutor/2015-March/104500.html about computing the normal distribution function, so I thought I'd write up a solution.Here's a Python implementation of erf(x) based on formula 7.1.26 from A&S. The maximum error is below 1.5 × 10-7.import math def erf(x): # constants a1 = 0.254829592 a2 = -0.284496736 a3 = 1.421413741 a4 = -1.453152027 a5 = 1.061405429 p = error function 0.3275911 # Save the sign of x sign = 1 if x < 0: sign = -1 x = abs(x) # A & S 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*yThis problem is typical in two ways: A&S has a solution, and you've got to know a little background before you can use it.The formula numpy error function given in A&S is only good for x ≥ 0. That's no problem if you know that the error function is an odd function, i.e. erf(-x) = -erf(x). But if you're an engineer who has never heard of the error function but needs to use it, it may take a while to figure out how to handle negative inputs.One other thing that someone just picking up A&S might not know is the best way to evaluate polynomials. The formula appears as 1 - (a1t1 + a2t2 + a3t3 + a4t4 + a5t5)exp(-x2), which is absolutely correct. But directly evaluating an nth order polynomial takes O(n2) operations, while the factorization used in the code above uses O(n) operations. This technique is known as Horner's method. Categories : Computing Math PythonTags : Python Special functionsBookmark the permalink Post navigationPrevious PostDraw a bigger pictureNext PostStand-alone normal (Gaussian) distribution function 14 thoughts on “Stand-alone error function erf(x)” Sergey Fomel 19 January 2009 at 16:21 erf is actually a standard function in C "math.h"For some reason, it is not included in Python's math module but, if you have SWIG, it is easy to write a SWIG wrapper for it. An example on a Linux machine: bash$ cat erf.i %module
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] Hi Danny, Thanks for the help! As you mentioned, using scipy.special.erfc was a much better idea. Below is a copy of my program and the stack trace, showing a new error. It seems that the first auto correlation works, however the second fails. ############################### # Autocorrelation program ############################### import numpy as np from numpy import ma, logical_or import pylab from pylab import * import matplotlib.pyplot as plt from matplotlib.ticker import AutoMinorLocator import math from math import exp import scipy from scipy.integrate import quad,fixed_quad import sys from scipy.optimize import curve_fit from scipy.fftpack import fft, ifft, fftfreq ############################## #fitting function (gaussian) ############################## def fit(x,a,b,c): return a*np.exp(-(x-b)**2/c) #(1.0/(np.sqrt(2*np.pi)*sigma))*np.exp(-(x-mu)**2 / (2*sigma**2)) ############################## # Load data from .txt file ############################## data1 = np.loadtxt('TL_pulseC.txt',skiprows = 2 ,usecols = (0,1)) data2 = np.loadtxt('cos_pulseC.txt',skiprows = 2 ,usecols = (0,1)) #print "\n Stage Position (um) Amplitude (V)" #print data ################################ # Create data arrays ################################ pos = np.array(data1[:,0]) # micrometers pos_m = pos*1.0*10**(-6) # meters print pos_m amp = np.array(data1[:,1]) # V amp_auto = np.array(data2[:,1]) ################################################################################################# # Finding the index of the maximum amplitude where the pulse delay is zero. Then calculating the # mean to shift the curve accordingly. ################################################################################################# peak_id = np.where(amp == np.max(amp))[0][0] mean = pos_m[peak_id] print mean dif = pos_m - mean t_d =(2.*dif)/(2.99*10**8)*10**(12.) # ps print t_d t = np.arange(0.,0.5,1/float(2*len(t_d))) # picoseconds (ps) ################################ # Define constants ################################ c = 2.99*10**8 # m/s alpha = np.pi # rad gamma = 200.0*10**(-15.) lamb_o = 1160.00*10**(-9.) # m omega_o = ((2.*np.pi*c)/lamb_o)*1.*10**(-12.) # ps^-1 delta = np.pi/2. # rad FWHM = 32.53*10**(-6) # m t_p = ((FWHM)/c)*10**(12.) # ps print t_p E_norm = 1. # normalized ################################ # Define functions ################################ ################################ # Electric field ################################ def E_o(x): return E_norm*(cosh(1.76*x/t_p))**(-1.) # Real part of electric field def E_rin(x): return (1./2.)*E_o(x)*cos(omega_o*x) # Imaginary part