CppAD: A C++ Algorithmic Differentiation Package 20110419
erf.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_ERF_INCLUDED
00003 # define CPPAD_ERF_INCLUDED
00004 
00005 /* --------------------------------------------------------------------------
00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-08 Bradley M. Bell
00007 
00008 CppAD is distributed under multiple licenses. This distribution is under
00009 the terms of the 
00010                     Common 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 $xref/glossary/Operation/Independent/independent/1/$$
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 $italic 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 $xref/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 
00085 // BEGIN CppAD namespace
00086 namespace CppAD {   
00087 
00088 template <class Type>
00089 Type erf_template(const Type &x)
00090 {       using CppAD::exp;
00091         static Type a = static_cast<Type>(993./880.);
00092         static Type b = static_cast<Type>(89./880.); 
00093 
00094         return tanh( (a + b * x * x) * x );
00095 }
00096 
00097 inline float erf(const float &x)
00098 {       return erf_template(x); }
00099 
00100 inline double erf(const double &x)
00101 {       return erf_template(x); }
00102 
00103 template <class Base>
00104 inline AD<Base> erf(const AD<Base> &x)
00105 {       return erf_template(x); }
00106 
00107 template <class Base>
00108 inline AD<Base> erf(const VecAD_reference<Base> &x)
00109 {       return erf_template( x.ADBase() ); }
00110 
00111 
00112 } // END CppAD namespace
00113 
00114 # endif