* [[w:Python (programming language)|Python]]: Our code is used to provide <code>scipy.special.erf</code>, <code>scipy.special.wofz</code>, and the other error functions in [[w:SciPy|SciPy]] starting in version 0.12.0 (see [https://github.com/scipy/scipy/commit/ed14bf0 here]).

* [[w:Python (programming language)|Python]]: Our code is used to provide <code>scipy.special.erf</code>, <code>scipy.special.wofz</code>, and the other error functions in [[w:SciPy|SciPy]] starting in version 0.12.0 (see [https://github.com/scipy/scipy/commit/ed14bf0 here]).

+

+

* [[w:Scilab]] has [http://bugzilla.scilab.org/show_bug.cgi?id=6092 a patch] to call the Faddeeva Package which should hopefully be incorporated into the next Scilab release.

Given the Faddeeva function w(z), one can also easily compute Voigt functions 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 special-case 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.

Omitting the relerr argument, or passing relerr=0 (or any relerr less than machine precision ε≈10−16), corresponds to requesting machine precision, and in practice a relative error < 10−13 is usually achieved. Specifying a larger value of relerr may improve performance for some z (at the expense of accuracy).

Similarly, the erf, erfc, erfcx, erfi, and Dawson functions are computed by calling:

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):

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(−x2) 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, Scilab, Julia

Wrappers are available for this function in other languages.

C: Download the files http://ab-initio.mit.edu/Faddeeva.c and http://ab-initio.mit.edu/Faddeeva.h (in addition to Faddeeva.cc from above) to obtain a pure C version (you do not need a C++ compiler), using C99 complex numbers. The complex functions are Faddeeva_erf(double complex z, double relerr) etc. instead of Faddeeva::erf, and the real-argument versions are Faddeeva_erf_re(double x) etc. (Note that in gcc you may need to compile with the -std=c99 flag to enable C99 support.)

Matlab (also available here): We provide source code for compiled Matlab plugins (MEX files) to interface all of the error functions above from Matlab.

The provided functions are called Faddeeva_w, Faddeeva_erf, Faddeeva_erfc, Faddeeva_erfi, Faddeeva_erfcx, and Faddeeva_Dawson, equivalent to the C++ functions above. All have usage of the form w = Faddeeva_w(z) [or w = Faddeeva_w(z, relerr) to pass the optional relative error], to compute the function value from an array or matrix z of complex (or real) inputs.

For convenience, a script to compile all of the plugins using the mex command in Matlab is included. Assuming you have a C++ compiler installed (and have run mex -setup to tell Matlab to use it), you can simply run the Faddeeva_build.m script in Matlab to compile all of the Faddeeva functions.

Install the resulting *.mex* files, along with the *.m help files, into your Matlab path

GNU Octave: Similar to Matlab, above, we provide source code for compiled GNU Octave plugins (.oct files) for all of the error functions above. (Note: our code for complex-argument erf, erfc, erfcx, erfi, and dawson functions has been merged into Octave and should be included in a future release.)

The provided functions are called Faddeeva_w, Faddeeva_erf, Faddeeva_erfc, Faddeeva_erfi, Faddeeva_erfcx, and Faddeeva_Dawson, with usage identical to the Matlab plugins above.

A Makefile is included. Assuming you have a C++ compiler and the mkoctfile command installed (mkoctfile comes with Octave, possibly in an octave-devel or similarly named package in GNU/Linux distributions), you can simply run make to compile the plugins, and sudo make install to install them system-wide (assuming you have system administrator privileges); otherwise put the compiled .oct files somewhere in your octave path.

Python: Our code is used to provide scipy.special.erf, scipy.special.wofz, and the other error functions in SciPy starting in version 0.12.0 (see here).

w:Scilab has a patch to call the Faddeeva Package which should hopefully be incorporated into the next Scilab release.

(I initially used this algorithm for all z, but the continued-fraction 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 real-z axis. You can switch back to using Algorithm 916 for all z by changing USE_CONTINUED_FRACTION to 0 in the code.)

Note that this is SGJ's independent re-implementation 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/open-source 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) = ex2erfc(x). Here, we use an erfcx routine written by SGJ that uses a combination of two algorithms: a continued-fraction 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 rational-Chebyshev code by W. J. Cody.)

Similarly, we also implement special-case code for real z, where the imaginary part of w is Dawson's integral. Similar to erfcx, this is also computed by a continued-fraction 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 program

To test the code, a small test program is included at the end of Faddeeva.cc which tests w(z) against several known results (from Wolfram Alpha) and prints the relative errors obtained. To compile the test program, #define TEST_FADDEEVA in the file (or compile with -DTEST_FADDEEVA on Unix) and compile Faddeeva.cc. The resulting program prints SUCCESS at the end of its output if the errors were acceptable.

License

The software is distributed under the "MIT License", a simple permissive free/open-source license:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.