scipy.special.erf#
- scipy.special.erf(z, out=None) = <ufunc 'erf'>#
Error function of real or complex argument.
\[\operatorname{erf}(z) = \frac{2}{\sqrt{\pi}} \int_0^z e^{-t^2} dt\]- Parameters:
- zndarray
Input array.
- outndarray, optional
Optional output array for the function values.
- Returns:
- resscalar or ndarray
The values of the error function at the given points z.
Notes
The cumulative distribution function (CDF) of the standard normal distribution can be expressed in terms of the error function as
\[\Phi(z) = \frac{1}{2} \left[1 + \operatorname{erf} \left(\frac{z}{\sqrt{2}}\right)\right]\]Array API Standard Support
erfhas support for Python Array API Standard compatible backends in addition to NumPy. The following combinations of backend and device (or other capability) are supported.Library
CPU
GPU
NumPy
✅
n/a
CuPy
n/a
✅
PyTorch
✅
✅
JAX
✅
✅
Dask
✅
n/a
For the NumPy backend, this function supports all NumPy ufunc keyword arguments. Other backends may support
out, but none of the other ufunc kwargs.outis typically supported for CuPy and PyTorch, but not currently in cases where SciPy relies on a generic Array API implementation or, for PyTorch on CPU, falls back to the NumPy backend.outis never supported for JAX because JAX arrays are immutable.See Support for the array API standard for more information.
References
[2]Milton Abramowitz and Irene A. Stegun, eds. Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables. New York: Dover, 1972.
[3]Steven G. Johnson, Faddeeva W function implementation. http://ab-initio.mit.edu/Faddeeva
Examples
In this example we show how
erfcan be used to solve the heat equation. Consider the problem\[\frac{\partial T}{\partial t} = \frac{\partial^2 T}{\partial x^2}, \qquad x \in (-\infty, \infty), \quad t > 0,\]with boundary conditions \(T(x,t) \to 0\) as \(x \to -\infty\) and \(T(x,t) \to 1\) as \(x \to \infty\) and initial condition \(T(x,0) = \mathcal{H}(x)\), where \(\mathcal{H}\) is the Heaviside step function. Seeking a solution of the form \(T(x,t) = f(\eta)\) with \(\eta = x/\sqrt{t}\) transforms the problem into the following ordinary differential equation
\[f'' + \frac{\eta}{2} f' = 0,\]with the boundary conditions \(f(\eta) \to 0\) as \(\eta \to -\infty\) and \(f(\eta) \to 1\) as \(\eta \to \infty\). This has the solution \(f(\eta) = (1 + \operatorname{erf}(\eta/2))/2\). We conclude the example by plotting the solution both as a function of \(\eta\) and as a function of \(x\) for different times.
>>> import numpy as np >>> import matplotlib.pyplot as plt >>> from scipy.special import erf >>> fig, (ax1, ax2) = plt.subplots(2, 1, layout="constrained", figsize=(5, 5)) >>> eta = np.linspace(-5, 5) >>> ax1.plot(eta, (1 + erf(eta/2))/2) >>> ax1.set_xlabel(r'$\eta$') >>> ax1.set_ylabel(r'$f(\eta)$') >>> x = np.linspace(-5, 5, num=500) >>> for t in [0.001, 0.01, 0.1, 0.5, 1]: ... ax2.plot(x, (1 + erf(x/(2*np.sqrt(t))))/2, label=f't={t}') >>> ax2.set_xlabel(r'$x$') >>> ax2.set_ylabel(r'$T(x,t)$') >>> ax2.legend() >>> plt.show()