CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_ERF_INCLUDED 00003 # define CPPAD_ERF_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Eclipse Public License Version 1.0. 00011 00012 A copy of this license is included in the COPYING file of this distribution. 00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00014 -------------------------------------------------------------------------- */ 00015 00016 /* 00017 ------------------------------------------------------------------------------- 00018 $begin erf$$ 00019 00020 $section The AD Error Function$$ 00021 $spell 00022 Vedder 00023 Cpp 00024 namespace 00025 Vec 00026 erf 00027 const 00028 $$ 00029 00030 $index erf, AD function$$ 00031 $index error, AD function$$ 00032 $index function, error AD$$ 00033 00034 $head Syntax$$ 00035 $icode%y% = erf(%x%)%$$ 00036 00037 00038 $head Description$$ 00039 Returns the value of the error function which is defined by 00040 $latex \[ 00041 {\rm erf} (x) = \frac{2}{ \sqrt{\pi} } \int_0^x \exp( - t * t ) \; {\bf d} t 00042 \] $$ 00043 00044 $head x$$ 00045 The argument $icode x$$, and the result $icode y$$ 00046 have one of the following paris of prototypes: 00047 $codei% 00048 const float%% &%x%, float%% %y% 00049 const double%% &%x%, double%% %y% 00050 const AD<%Base%> &%x%, AD<%Base%> %y% 00051 const VecAD<%Base%>::reference &%x%, AD<%Base%> %y% 00052 %$$ 00053 00054 00055 $head Operation Sequence$$ 00056 The AD of $icode Base$$ 00057 operation sequence used to calculate $icode y$$ is 00058 $cref/independent/glossary/Operation/Independent/$$ 00059 of $icode x$$. 00060 00061 $head Method$$ 00062 This is a fast approximation (few numerical operations) 00063 with relative error bound $latex 4 \times 10^{-4}$$; see 00064 Vedder, J.D., 00065 $icode Simple approximations for the error function and its inverse$$, 00066 American Journal of Physics, 00067 v 55, 00068 n 8, 00069 1987, 00070 p 762-3. 00071 00072 $head Example$$ 00073 $children% 00074 example/erf.cpp 00075 %$$ 00076 The file 00077 $cref erf.cpp$$ 00078 contains an example and test of this function. 00079 It returns true if it succeeds and false otherwise. 00080 00081 $end 00082 ------------------------------------------------------------------------------- 00083 */ 00084 # include <cppad/local/cppad_assert.hpp> 00085 00086 // needed before one can use CPPAD_ASSERT_FIRST_CALL_NOT_PARALLEL 00087 # include <cppad/thread_alloc.hpp> 00088 00089 // BEGIN CppAD namespace 00090 namespace CppAD { 00091 00092 template <class Type> 00093 Type erf_template(const Type &x) 00094 { using CppAD::exp; 00095 const Type a = static_cast<Type>(993./880.); 00096 const Type b = static_cast<Type>(89./880.); 00097 00098 return tanh( (a + b * x * x) * x ); 00099 } 00100 00101 inline float erf(const float &x) 00102 { return erf_template(x); } 00103 00104 inline double erf(const double &x) 00105 { return erf_template(x); } 00106 00107 template <class Base> 00108 inline AD<Base> erf(const AD<Base> &x) 00109 { return erf_template(x); } 00110 00111 template <class Base> 00112 inline AD<Base> erf(const VecAD_reference<Base> &x) 00113 { return erf_template( x.ADBase() ); } 00114 00115 00116 } // END CppAD namespace 00117 00118 # endif