(Difference between revisions)
 Revision as of 19:58, 16 October 2012 (edit)Stevenj (Talk | contribs) (→Faddeeva / complex error function)← Previous diff Revision as of 18:27, 19 October 2012 (edit)Stevenj (Talk | contribs) (→Usage)Next diff → Line 10: Line 10: #include <complex> #include <complex> - extern std::complex<double> Faddeeva_w(std::complex<double> z, double relerr); + 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 [[w:Approximation error|relative error]] relerr. The function Faddeeva_w(z, relerr) computes ''w''(''z'') to a desired [[w:Approximation error|relative error]] relerr. - Passing relerr=0 (or any relerr less than machine precision ε≈10−16) corresponds to requesting [[w:Machine epsilon|machine precision]], and in practice a relative error < 10−13 is usually achieved. Specifying a larger value of relerr generally improves performance (at the expense of accuracy). + Omitting the relerr argument, or passing relerr=0 (or any relerr less than machine precision ε≈10−16), corresponds to requesting [[w:Machine epsilon|machine precision]], and in practice a relative error < 10−13 is usually achieved. Specifying a larger value of relerr generally improves performance (at the expense of accuracy). You should also compile Faddeeva_w.cc and link it with your program, of course. You should also compile Faddeeva_w.cc and link it with your program, of course.

# Faddeeva / complex error function

Steven G. Johnson has written free/open-source C++ code 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. Download the source code from:

## 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` generally improves performance (at the expense of accuracy).

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

## Algorithm

We use the algorithm described in the paper:

Note that this is SGJ's independent re-implementation of this algorithm, based on the description in the paper only. In particular, we did not refer to (or even download) the author's Matlab implementation (which is under restrictive "semifree" ACM copyright terms and therefore unusable in free/open-source software).

This algorithm 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 include an erfcx function derived from the DERFC routine in SLATEC (modified by SGJ to compute erfcx instead of erfc), originally written by W. Fullerton at Los Alamos National Laboratory.

## 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.