# Faddeeva Package

### From AbInitio

Revision as of 02:12, 25 October 2012 (edit)Stevenj (Talk | contribs) (→Faddeeva / complex error function) ← Previous diff |
Revision as of 02:20, 25 October 2012 (edit)Stevenj (Talk | contribs) (→Algorithm) Next diff → |
||

Line 20: |
Line 20: | ||

== Algorithm == | == Algorithm == | ||

- | We use the algorithm described in the paper: | + | 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 |

+ | |||

+ | * 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]. | ||

+ | |||

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

* Mofreh R. Zaghloul and Ahmed N. Ali, "[http://dx.doi.org/10.1145/2049673.2049679 Algorithm 916: Computing the Faddeyeva and Voigt Functions]," ''ACM Trans. Math. Soft.'' '''38''' (2), 15 (2011). Preprint available at [http://arxiv.org/abs/1106.0151 arXiv:1106.0151]. | * Mofreh R. Zaghloul and Ahmed N. Ali, "[http://dx.doi.org/10.1145/2049673.2049679 Algorithm 916: Computing the Faddeyeva and Voigt Functions]," ''ACM Trans. Math. Soft.'' '''38''' (2), 15 (2011). Preprint available at [http://arxiv.org/abs/1106.0151 arXiv:1106.0151]. | ||

- | 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 "[http://www.gnu.org/philosophy/categories.html semifree]" [http://www.acm.org/publications/policies/softwarecrnotice ACM copyright terms] and therefore unusable in free/open-source software). | + | (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, it is competitive for smaller |''z''|, and is significantly more accurate than the Poppe & Wijers [http://www.netlib.org/toms/680 code] in some regions, e.g. in the vicinity of |''z''|=1.) |

+ | |||

+ | 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 "[http://www.gnu.org/philosophy/categories.html semifree]" [http://www.acm.org/publications/policies/softwarecrnotice ACM copyright terms] and are therefore unusable in free/open-source software. | ||

- | This algorithm requires an external [[w:Error function|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''<sup>''x''<sup>2</sup></sup>erfc(''x''). Here, we include an erfcx function derived from the DERFC routine in [[w:SLATEC|SLATEC]] (modified by SGJ to compute erfcx instead of erfc), originally written by W. Fullerton at [[w:Los Alamos National Laboratory|Los Alamos National Laboratory]]. | + | Algorithm 916 requires an external [[w:Error function|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''<sup>''x''<sup>2</sup></sup>erfc(''x''). Here, we include an erfcx function derived from the DERFC routine in [[w:SLATEC|SLATEC]] (modified by SGJ to compute erfcx instead of erfc), originally written by W. Fullerton at [[w:Los Alamos National Laboratory|Los Alamos National Laboratory]]. |

== Test program == | == Test program == |

## Revision as of 02:20, 25 October 2012

## Contents |

# Faddeeva / complex error function

Steven G. Johnson has written free/open-source C++ code to compute the **scaled complex error function** *w*(*z*) = *e*^{−z2}erfc(−*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:

- http://ab-initio.mit.edu/Faddeeva_w.cc (updated 24 October 2012)

## Usage

To use the code, add the following declaration to your C++ source (or header file):

#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

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

- 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, it is competitive for smaller |*z*|, and is significantly more accurate than the Poppe & Wijers code in some regions, e.g. in the vicinity of |*z*|=1.)

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

## License

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

*Copyright © 2012 Massachusetts Institute of Technology*-
*[Also included are functions derived from derfc in SLATEC (netlib.org/slatec), which "is in the public domain" and hence may be redistributed under these or any terms.]*

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