CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
erf.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_ERF_HPP
2 # define CPPAD_CORE_ERF_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 
15 /*
16 -------------------------------------------------------------------------------
17 $begin erf$$
18 $spell
19  erf
20  const
21  Vec
22  std
23  cmath
24  CppAD
25  Vedder
26 $$
27 $section The Error Function$$
28 
29 $head Syntax$$
30 $icode%y% = erf(%x%)%$$
31 
32 $head Description$$
33 Returns the value of the error function which is defined by
34 $latex \[
35 {\rm erf} (x) = \frac{2}{ \sqrt{\pi} } \int_0^x \exp( - t * t ) \; {\bf d} t
36 \] $$
37 
38 $head x, y$$
39 See the $cref/possible types/unary_standard_math/Possible Types/$$
40 for a unary standard math function.
41 
42 $head CPPAD_USE_CPLUSPLUS_2011$$
43 
44 $subhead true$$
45 If this preprocessor symbol is true ($code 1$$),
46 and $icode x$$ is an AD type,
47 this is an $cref/atomic operation/glossary/Operation/Atomic/$$.
48 
49 $subhead false$$
50 If this preprocessor symbol is false ($code 0$$),
51 CppAD uses a fast approximation (few numerical operations)
52 with relative error bound $latex 4 \times 10^{-4}$$; see
53 Vedder, J.D.,
54 $icode Simple approximations for the error function and its inverse$$,
55 American Journal of Physics,
56 v 55,
57 n 8,
58 1987,
59 p 762-3.
60 
61 $head Example$$
62 $children%
63  example/general/erf.cpp
64 %$$
65 The file
66 $cref erf.cpp$$
67 contains an example and test of this function.
68 It returns true if it succeeds and false otherwise.
69 
70 $end
71 -------------------------------------------------------------------------------
72 */
73 # include <cppad/configure.hpp>
74 # if ! CPPAD_USE_CPLUSPLUS_2011
75 
76 // BEGIN CppAD namespace
77 namespace CppAD {
78 
79 template <class Type>
80 Type erf_template(const Type &x)
81 { using CppAD::exp;
82  const Type a = static_cast<Type>(993./880.);
83  const Type b = static_cast<Type>(89./880.);
84 
85  return tanh( (a + b * x * x) * x );
86 }
87 
88 inline float erf(const float &x)
89 { return erf_template(x); }
90 
91 inline double erf(const double &x)
92 { return erf_template(x); }
93 
94 template <class Base>
95 inline AD<Base> erf(const AD<Base> &x)
96 { return erf_template(x); }
97 
98 template <class Base>
99 inline AD<Base> erf(const VecAD_reference<Base> &x)
100 { return erf_template( x.ADBase() ); }
101 
102 
103 } // END CppAD namespace
104 
105 # endif // CPPAD_USE_CPLUSPLUS_2011
106 # endif // CPPAD_ERF_INCLUDED
std::complex< double > erf(const std::complex< double > &x)
AD< Base > exp(const AD< Base > &x)
AD< Base > tanh(const AD< Base > &x)