(Difference between revisions)
 Revision as of 21:10, 30 October 2012 (edit)Stevenj (Talk | contribs) (→Usage)← Previous diff Revision as of 23:40, 30 October 2012 (edit)Stevenj (Talk | contribs) (→Faddeeva / complex error function)Next diff → Line 5: Line 5: Download the source code from: Download the source code from: - * [http://ab-initio.mit.edu/Faddeeva_w.cc http://ab-initio.mit.edu/Faddeeva_w.cc] (updated 29 October 2012) + * [http://ab-initio.mit.edu/Faddeeva_w.cc http://ab-initio.mit.edu/Faddeeva_w.cc] (updated 30 October 2012) == Usage == == Usage ==

# Faddeeva / complex error function

Steven G. Johnson has written free/open-source C++ code (with wrappers for other languages) to compute the scaled complex error function w(z) = ez2erfc(−iz), also called the Faddeeva function (and also the plasma dispersion function), for arbitrary complex arguments z to a given accuracy. Given the Faddeeva function, one can easily compute Voigt functions, the Dawson function, and similar related functions.

## Usage

#include <complex>
extern std::complex<double> Faddeeva_w(std::complex<double> z, double relerr=0);


The function Faddeeva_w(z, relerr) computes w(z) to a desired relative error relerr.

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 (at the expense of accuracy).

You should also compile Faddeeva_w.cc and link it with your program, of course.

In terms of w(z), some other important functions are:

$\mathrm{erfcx}(x) = e^{x^2} \mathrm{erfc}(x) = w(ix)$ (scaled complementary error function)
$\mathrm{erfc}(x) = e^{-x^2} w(ix) = \begin{cases} e^{-x^2} w(ix) & \mathrm{Re}\,x \geq 0 \\ 2 - e^{-x^2} w(-ix)) & \mathrm{Re}\,x < 0 \end{cases}$ (complementary error function)
$\mathrm{erf}(x) = 1 - \mathrm{erfc}(x) = \begin{cases} 1 - e^{-x^2} w(ix) & \mathrm{Re}\,x \geq 0 \\ e^{-x^2} w(-ix) - 1 & \mathrm{Re}\,x < 0 \end{cases}$ (error function)
$\mathrm{erfi}(x) = -i\mathrm{erf}(ix) = -i[e^{x^2} w(x) - 1]$; for real x, $\mathrm{erfi}(x) = \frac{\mathrm{Im}[w(x)]}{\mathrm{Re}[w(x)]}$ (imaginary error function)
$F(x) = \frac{i\sqrt{\pi}}{2} \left[ e^{-x^2} - w(x) \right]$; for real x, $F(x) = \frac{\sqrt{\pi}}{2}\mathrm{Im}[w(x)]$ (Dawson function)

Note that in the case of erf and erfc, we provide different equations for positive and negative x, in order to avoid numerical problems arising from multiplying exponentially large and small quantities. For erfi and F, there are simplifications that occur for real x as noted.

## Wrappers: Matlab, GNU Octave, and Python

Wrappers are available for this function in other languages.

• Matlab (also available here): A function Faddeeva_w(z, relerr), where the arguments have the same meaning as above (the relerr argument is optional) can be downloaded from Faddeeva_w_mex.cc (along with the help file Faddeeva_w.m. Compile it into an octave plugin with:
mex -output Faddeeva_w -O Faddeeva_w_mex.cc Faddeeva_w.cc

• GNU Octave: A function Faddeeva_w(z, relerr), where the arguments have the same meaning as above (the relerr argument is optional) can be downloaded from Faddeeva_w_oct.cc. Compile it into a MEX file with:
mkoctfile -DMPICH_SKIP_MPICXX=1 -DOMPI_SKIP_MPICXX=1 -s -o Faddeeva_w.oct Faddeeva_w_oct.cc Faddeeva_w.cc

• Python: Our code is used to provide scipy.special.wofz in SciPy starting in version 0.12.0 (see here).

## Algorithm

This implementation uses a combination of different algorithms. For sufficiently large |z|, we use a continued-fraction expansion for w(z) similar to those described in

Unlike those papers, however, we switch to a completely different algorithm for smaller |z|:

(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, by the new erfcx routine is much faster.)

## Test program

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