
The Faddeeva PackageSteven G. Johnson has written free/opensource C++ code (with wrappers for C, Matlab, GNU Octave, Python, R, Scilab, and Julia) to compute the various error functions of arbitrary complex arguments. In particular, we provide:
Given the Faddeeva function w(z) and the other complex error functions, one can also easily compute Voigt functions, Fresnel integrals, and similar related functions as well. In benchmarks of our code, we find that it is comparable to or faster than most competing software for these functions in the complex plane (but we also have specialcase optimizations for purely real or imaginary arguments), and we find that the accuracy is typically at at least 13 significant digits in both the real and imaginary parts. Because all of the algorithms are based on algorithms for the Faddeeva function, we call this the Faddeeva Package.
DownloadDownload the source code from:
See also below for wrappers to call the Faddeeva package from other languages. This code has also been packaged into the libcerf and openlibm libraries. UsageTo use the code, include the #include "Faddeeva.hh" and compile and link the extern std::complex<double> Faddeeva::w(std::complex<double> z, double relerr=0); This function Omitting the Similarly, the erf, erfc, erfcx, erfi, and Dawson functions are computed by calling: extern std::complex<double> Faddeeva::erf(std::complex<double> z, double relerr=0); extern std::complex<double> Faddeeva::erfc(std::complex<double> z, double relerr=0); extern std::complex<double> Faddeeva::erfcx(std::complex<double> z, double relerr=0); extern std::complex<double> Faddeeva::erfi(std::complex<double> z, double relerr=0); extern std::complex<double> Faddeeva::Dawson(std::complex<double> z, double relerr=0); Since these functions are purely real for real arguments z=x, we provide the following specialized interfaces for convenience (and a slight performance gain, although the complex functions above automatically execute specialized code for purely real arguments): extern double Faddeeva::erf(double x); extern double Faddeeva::erfc(double x); extern double Faddeeva::erfcx(double x); extern double Faddeeva::erfi(double x); extern double Faddeeva::Dawson(double x); (These functions always compute to maximum accuracy, usually near machine precision.) It is also sometimes useful to compute Im[w(x)] for real x, since in that case (like the Dawson function but without the √π/2 factor). [Note that Re[w(x)] is simply exp(−x^{2}) for real x.] Im[w(x)] can be computed efficiently to nearly machine precision by calling: extern double Faddeeva::w_im(double x); Wrappers: C, Matlab, GNU Octave, Python, R, Scilab, JuliaWrappers are available for this function in other languages.
AlgorithmsOur implementation uses a combination of different algorithms, mostly centering around computing the Faddeeva function w(z). To compute the Faddeeva function for sufficiently large z, we use a continuedfraction expansion for w(z) similar to those described in
Unlike those papers, however, we switch to a completely different algorithm for smaller z or for z close to the real axis:
(I initially used this algorithm for all z, but the continuedfraction expansion turned out to be faster for larger z. On the other hand, Algorithm 916 is competitive or faster for smaller z, and appears to be significantly more accurate than the Poppe & Wijers code in some regions, e.g. in the vicinity of z=1 [although comparison with other compilers suggests that this may be a problem specific to gfortran]. Algorithm 916 also has better relative accuracy in Re[z] for some regions near the realz axis. You can switch back to using Algorithm 916 for all z by changing Note that this is SGJ's independent reimplementation of these algorithms, based on the descriptions in the papers only. In particular, we did not refer to the authors' Fortran or Matlab implementations (respectively), which are under restrictive "semifree" ACM copyright terms and are therefore unusable in free/opensource software. Algorithm 916 requires an external complementary error function erfc(x) function for real arguments x to be supplied as a subroutine. More precisely, it requires the scaled function erfcx(x) = e^{x2}erfc(x). Here, we use an erfcx routine written by SGJ that uses a combination of two algorithms: a continuedfraction expansion for large x and a lookup table of Chebyshev polynomials for small x. (I initially used an erfcx function derived from the DERFC routine in SLATEC, modified by SGJ to compute erfcx instead of erfc, but the new erfcx routine is much faster, and also seems to be faster than the calerf rationalChebyshev code by W. J. Cody.) Similarly, we also implement specialcase code for real z, where the imaginary part of w is Dawson's integral. Similar to erfcx, this is also computed by a continuedfraction expansion for large x, a lookup table of Chebyshev polynomials for smaller x, and finally a Taylor expansion for very small x. (This seems to be faster than the dawsn function in the Cephes library, and is substantially faster than the gsl_sf_dawson function in the GNU Scientific Library.) The other error functions can be computed in terms of w(z). The basic equations are:
Note that we sometimes employ different equations for positive and negative Re(z) in order to avoid numerical problems arising from multiplying exponentially large and small quantities. For erfi and the Dawson function, there are simplifications that occur for real x as noted. In some cases, however, there are additional complications that require our implementation to go beyond these simple formulas. For erf, large cancellation errors occur in these formulas near z=0 where w(z) is nearly 1, as well as near the imaginary axis for Re[erf], and in these regimes we switch to a Taylor expansion. Similarly, for the Dawson function we switch to a Taylor expansion near the origin or near the real axis. (Similar problems occur for erfi, but our erfi implementation simply calls our erf code.) Test programTo test the code, a small test program is included at the end of LicenseThe software is distributed under the "MIT License" (also called the Expat License), a simple permissive free/opensource license (which is compatible with the GPL and most other licenses):
