# Faddeeva Package

### From AbInitio

Revision as of 14:17, 3 November 2012 (edit)Stevenj (Talk | contribs) (→Usage) ← Previous diff |
Revision as of 03:23, 4 November 2012 (edit)Stevenj (Talk | contribs) Next diff → |
||

Line 1: |
Line 1: | ||

- | = Faddeeva / complex error function = | + | = Complex error functions = |

- | [http://math.mit.edu/~stevenj Steven G. Johnson] has written [[w:Free and open-source software|free/open-source]] C++ code (with wrappers for other languages) to compute the '''scaled complex [[w:Error function|error function]]''' ''w''(''z'') = ''e''<sup>−''z''<sup>2</sup></sup>erfc(−''iz''), also called the ''[[w:Faddeeva function|Faddeeva function]]'' (and also the ''plasma dispersion function''), for arbitrary [[w:complex number|complex]] arguments ''z'' to a given accuracy. | + | [http://math.mit.edu/~stevenj Steven G. Johnson] has written [[w:Free and open-source software|free/open-source]] C++ code (with wrappers for other languages) to compute the various [[w:Error function|error functions]] of arbitrary [[w:Complex number|complex]] arguments. In particular, we provide: |

- | Download the source code from: | + | |

- | * [http://ab-initio.mit.edu/Faddeeva_w.cc http://ab-initio.mit.edu/Faddeeva_w.cc] (updated 30 October 2012) | + | * The [[w:Faddeeva function|Faddeeva function]] <math>w(z) = e^{-z^2} \mathrm{erfc}(z) \!</math>, where erfc is the complementary error function. |

+ | * The [[w:Error function|error function]] <math>\mathrm{erf}(z) \!</math> | ||

+ | * The complementary error function <math>\mathrm{erfc}(z) = 1 - \mathrm{erf}(z) \!</math> | ||

+ | * The scaled complementary error function <math>\mathrm{erfcx}(z) = e^{z^2} \mathrm{erfc}(z) \!</math> | ||

+ | * The imaginary error function <math>\mathrm{erfi}(z) = -i \mathrm{erf}(iz) \!</math> | ||

+ | * The [[w:Dawson function|Dawson function]] <math>\mathrm{Dawson}(z) = \frac{\sqrt{\pi}}{2} e^{-z^2} \mathrm{erfi}(z)</math> | ||

- | Given the Faddeeva function, one can easily compute [[w:Voigt profile|Voigt functions]], the [[w:Dawson function|Dawson function]], and similar related functions. Our implementation includes special-case optimizations for purely real or imaginary ''z'', making its performance competitive with specialized implementations of (e.g.) the Dawson function, erfcx, and erfi. | + | Given the Faddeeva function ''w''(''z''), one can also easily compute [[w:Voigt profile|Voigt functions]] and similar related functions as well. In benchmarks of our code, we find that it is competitive or faster than most competing software for these functions, especially 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 near [[w:Machine epsilon|machine precision]]. |

+ | |||

+ | Because all of the algorithms are based on algorithms for the Faddeeva function, we call this the '''Faddeeva package'''. Download the source code from: | ||

+ | |||

+ | * [http://ab-initio.mit.edu/Faddeeva.cc http://ab-initio.mit.edu/Faddeeva.cc] and [http://ab-initio.mit.edu/Faddeeva.hh http://ab-initio.mit.edu/Faddeeva.hh] (updated 3 November 2012) | ||

== Usage == | == Usage == | ||

- | To use the code, add the following declaration to your C++ source (or header file): | + | To use the code, include the <code>Faddeeva.hh</code> [[w:Header file|header file]]: |

+ | |||

+ | #include "Faddeeva.hh" | ||

+ | |||

+ | and compile and [[w:Linker (computing)|link]] the <code>Faddeeva.cc</code> source code. You can then call various functions. For example: | ||

- | #include <complex> | + | extern std::complex<double> Faddeeva::w(std::complex<double> z, double relerr=0); |

- | extern std::complex<double> Faddeeva_w(std::complex<double> z, double relerr=0); | + | |

- | The function <code>Faddeeva_w(z, relerr)</code> computes ''w''(''z'') to a desired [[w:Approximation error|relative error]] <code>relerr</code>. | + | The function <code>Faddeeva::w(z, relerr)</code> computes ''w''(''z'') to a desired [[w:Approximation error|relative error]] <code>relerr</code>. |

Omitting the <code>relerr</code> argument, or passing <code>relerr=0</code> (or any <code>relerr</code> less than machine precision ε≈10<sup>−16</sup>), corresponds to requesting [[w:Machine epsilon|machine precision]], and in practice a relative error < 10<sup>−13</sup> is usually achieved. Specifying a larger value of <code>relerr</code> may improve performance (at the expense of accuracy). | Omitting the <code>relerr</code> argument, or passing <code>relerr=0</code> (or any <code>relerr</code> less than machine precision ε≈10<sup>−16</sup>), corresponds to requesting [[w:Machine epsilon|machine precision]], and in practice a relative error < 10<sup>−13</sup> is usually achieved. Specifying a larger value of <code>relerr</code> may improve performance (at the expense of accuracy). | ||

- | You should also compile <code>Faddeeva_w.cc</code> and link it with your program, of course. | + | Similarly, the erf, erfc, erfcx, erfi, and Dawson functions are computed by calling: |

- | In terms of ''w''(''z''), some other important functions are: | + | extern std::complex<double> Faddeeva::erf(std::complex<double> z, double relerr=0); |

- | :<math>\mathrm{erfcx}(z) = e^{z^2} \mathrm{erfc}(z) = w(iz)</math> (scaled complementary error function) | + | extern std::complex<double> Faddeeva::erfc(std::complex<double> z, double relerr=0); |

- | :<math>\mathrm{erfc}(z) = e^{-z^2} w(iz) = \begin{cases} e^{-z^2} w(iz) & \mathrm{Re}\,z \geq 0 \\ 2 - e^{-z^2} w(-iz)) & \mathrm{Re}\,z < 0 \end{cases}</math> (complementary error function) | + | extern std::complex<double> Faddeeva::erfcx(std::complex<double> z, double relerr=0); |

- | :<math>\mathrm{erf}(z) = 1 - \mathrm{erfc}(z) = \begin{cases} 1 - e^{-z^2} w(iz) & \mathrm{Re}\,z \geq 0 \\ e^{-z^2} w(-iz) - 1 & \mathrm{Re}\,z < 0 \end{cases}</math> (error function) | + | extern std::complex<double> Faddeeva::erfi(std::complex<double> z, double relerr=0); |

- | :<math>\mathrm{erfi}(z) = -i\mathrm{erf}(iz) = -i[e^{z^2} w(z) - 1]</math>; for '''real''' ''x'', <math>\mathrm{erfi}(x) = e^{x^2} \mathrm{Im}[w(x)] = \frac{\mathrm{Im}[w(x)]}{\mathrm{Re}[w(x)]}</math> (imaginary error function) | + | extern std::complex<double> Faddeeva::Dawson(std::complex<double> z, double relerr=0); |

- | :<math>F(z) = \frac{\sqrt{\pi}}{2} e^{-z^2} \mathrm{erfi}(z) = \frac{i\sqrt{\pi}}{2} \left[ e^{-z^2} - w(z) \right]</math>; for '''real''' ''x'', <math>F(x) = \frac{\sqrt{\pi}}{2}\mathrm{Im}[w(x)]</math> ([[w:Dawson function|Dawson function]]) | + | |

- | In the case of erf and erfc, we suggest 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 ''F'', there are simplifications that occur for real ''x'' as noted. Furthermore, if you want to compute e.g. erfi or the Dawson function ''F'' for real ''z''=''x'', you can obtain the imaginary part of ''w''(''x'') directly without computing the real part, by calling: | + | |

- | extern double ImFaddeeva_w(double x); | + | Since these functions are purely [[w:Real number|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): |

- | which computes Im[''w''(''x'')] efficiently (to nearly machine precision). Note that Re[''w''(''x'')] is simply exp(−''x''<sup>2</sup>) for real ''x''. | + | 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 <math>\mathrm{Im}[w(z)] = e^{-z^2} \mathrm{erfi}(z)</math>. Note that Re[''w''(''x'')] is simply exp(−''x''<sup>2</sup>) for real ''x''. Im[''w''(''x'')] can be computed efficiently by calling: | ||

+ | |||

+ | extern double Faddeeva::w_im(double x); | ||

+ | |||

+ | (Again, this computes to nearly machine precision.) | ||

== Wrappers: Matlab, GNU Octave, and Python == | == Wrappers: Matlab, GNU Octave, and Python == | ||

Line 49: |
Line 70: | ||

== Algorithm == | == 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 | + | This implementation uses a combination of different algorithms, centering around computing the Faddeeva function ''w''(''z''). For sufficiently large |''z''|, we use a continued-fraction expansion for ''w''(''z'') similar to those described in |

* Walter Gautschi, "[http://dx.doi.org/10.1137/0707012 Efficient computation of the complex error function]," ''SIAM J. Numer. Anal.'' '''7''' (1), pp. 187–198 (1970). G. P. M. Poppe and C. M. J. Wijers, "[http://dx.doi.org/10.1145/77626.77629 More efficient computation of the complex error function]," ''ACM Trans. Math. Soft.'' '''16''' (1), pp. 38–46 (1990); this is [http://www.netlib.org/toms/680 TOMS Algorithm 680]. | * Walter Gautschi, "[http://dx.doi.org/10.1137/0707012 Efficient computation of the complex error function]," ''SIAM J. Numer. Anal.'' '''7''' (1), pp. 187–198 (1970). G. P. M. Poppe and C. M. J. Wijers, "[http://dx.doi.org/10.1145/77626.77629 More efficient computation of the complex error function]," ''ACM Trans. Math. Soft.'' '''16''' (1), pp. 38–46 (1990); this is [http://www.netlib.org/toms/680 TOMS Algorithm 680]. | ||

Line 64: |
Line 85: | ||

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 [http://www.netlib.org/cephes/doubldoc.html#dawsn dawsn function] in the Cephes library, and is substantially faster than the [http://www.gnu.org/software/gsl/manual/html_node/Dawson-Function.html gsl_sf_dawson] function in the [[w:GNU Scientific Library|GNU Scientific Library]].) | 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 [http://www.netlib.org/cephes/doubldoc.html#dawsn dawsn function] in the Cephes library, and is substantially faster than the [http://www.gnu.org/software/gsl/manual/html_node/Dawson-Function.html gsl_sf_dawson] function in the [[w:GNU Scientific Library|GNU Scientific Library]].) | ||

+ | |||

+ | The other error functions can be computed in terms of ''w''(''z''). The basic equations are: | ||

+ | :<math>\mathrm{erfcx}(z) = e^{z^2} \mathrm{erfc}(z) = w(iz)</math> (scaled complementary error function) | ||

+ | :<math>\mathrm{erfc}(z) = e^{-z^2} w(iz) = \begin{cases} e^{-z^2} w(iz) & \mathrm{Re}\,z \geq 0 \\ 2 - e^{-z^2} w(-iz)) & \mathrm{Re}\,z < 0 \end{cases}</math> (complementary error function) | ||

+ | :<math>\mathrm{erf}(z) = 1 - \mathrm{erfc}(z) = \begin{cases} 1 - e^{-z^2} w(iz) & \mathrm{Re}\,z \geq 0 \\ e^{-z^2} w(-iz) - 1 & \mathrm{Re}\,z < 0 \end{cases}</math> (error function) | ||

+ | :<math>\mathrm{erfi}(z) = -i\mathrm{erf}(iz) = -i[e^{z^2} w(z) - 1]</math>; for '''real''' ''x'', <math>\mathrm{erfi}(x) = e^{x^2} \mathrm{Im}[w(x)] = \frac{\mathrm{Im}[w(x)]}{\mathrm{Re}[w(x)]}</math> (imaginary error function) | ||

+ | :<math>\mathrm{Dawson}(z) = \frac{\sqrt{\pi}}{2} e^{-z^2} \mathrm{erfi}(z) = \frac{i\sqrt{\pi}}{2} \left[ e^{-z^2} - w(z) \right]</math>; for '''real''' ''x'', <math>F(x) = \frac{\sqrt{\pi}}{2}\mathrm{Im}[w(x)]</math> ([[w:Dawson function|Dawson function]]) | ||

+ | In the case of erf and erfc, we 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 == | == Test program == |

## Revision as of 03:23, 4 November 2012

## Contents |

# Complex error functions

Steven G. Johnson has written free/open-source C++ code (with wrappers for other languages) to compute the various error functions of arbitrary complex arguments. In particular, we provide:

- The Faddeeva function , where erfc is the complementary error function.
- The error function
- The complementary error function
- The scaled complementary error function
- The imaginary error function
- The Dawson function

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 competitive or faster than most competing software for these functions, especially 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 near machine precision.

Because all of the algorithms are based on algorithms for the Faddeeva function, we call this the **Faddeeva package**. Download the source code from:

- http://ab-initio.mit.edu/Faddeeva.cc and http://ab-initio.mit.edu/Faddeeva.hh (updated 3 November 2012)

## Usage

To use the code, include the `Faddeeva.hh`

header file:

#include "Faddeeva.hh"

and compile and link the `Faddeeva.cc`

source code. You can then call various functions. For example:

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

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 . Note that Re[*w*(*x*)] is simply exp(−*x*^{2}) for real *x*. Im[*w*(*x*)] can be computed efficiently by calling:

extern double Faddeeva::w_im(double x);

(Again, this computes to nearly machine precision.)

## 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, centering around computing the Faddeeva function *w*(*z*). For sufficiently large |*z*|, we use a continued-fraction expansion for *w*(*z*) similar to those described in

- Walter Gautschi, "Efficient computation of the complex error function,"
*SIAM J. Numer. Anal.***7**(1), pp. 187–198 (1970). G. P. M. Poppe and C. M. J. Wijers, "More efficient computation of the complex error function,"*ACM Trans. Math. Soft.***16**(1), pp. 38–46 (1990); this is TOMS Algorithm 680.

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

- Mofreh R. Zaghloul and Ahmed N. Ali, "Algorithm 916: Computing the Faddeyeva and Voigt Functions,"
*ACM Trans. Math. Soft.***38**(2), 15 (2011). Preprint available at arXiv:1106.0151.

(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*) = *e*^{x2}erfc(*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:

- (scaled complementary error function)
- (complementary error function)
- (error function)
- ; for
**real***x*, (imaginary error function) - ; for
**real***x*, (Dawson function)

In the case of erf and erfc, we 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_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.

## License

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

*Copyright © 2012 Massachusetts Institute of Technology**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.*