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 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 [[w:SLATEC|SLATEC]], modified by SGJ to compute erfcx instead of erfc, by the new erfcx routine is much faster, and also seems to be faster than the [http://www.netlib.org/specfun/erf calerf] rational-Chebyshev code by W. J. Cody.)

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 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 [[w:SLATEC|SLATEC]], modified by SGJ to compute erfcx instead of erfc, by the new erfcx routine is much faster, and also seems to be faster than the [http://www.netlib.org/specfun/erf 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. Like erfcx, this is also computed by a continued-fraction expansion for large |''x''|, a lookup table of Chebyshev polynomials for small |''x''|, and finally a Taylor expansion for very small |''x''|.

+

Similarly, we also implement special-case code for real-''z'', where the imaginary part of ''w'' is Dawson's integral. Like erfcx, this is also computed by a continued-fraction expansion for large |''x''|, a lookup table of Chebyshev polynomials for small |''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]].)

Given the Faddeeva function, one can easily compute Voigt functions, the 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.

Usage

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

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.

Note that in the case of erf and erfc, we provide 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);

which computes Im[w(x)] efficiently.

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:

(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, 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. Like erfcx, this is also computed by a continued-fraction expansion for large |x|, a lookup table of Chebyshev polynomials for small |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.)

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:

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.