CppAD: A C++ Algorithmic Differentiation Package  20130102
std_math_ad.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_STD_MATH_AD_INCLUDED
00003 # define CPPAD_STD_MATH_AD_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 $begin std_math_ad$$
00018 $spell
00019      Vec
00020      std
00021      atan
00022      const
00023      acos
00024      asin
00025      atan
00026      cos
00027      exp
00028      fabs
00029      sqrt
00030      CppAD
00031      namespace
00032      tanh
00033 $$
00034 
00035 $index standard, AD math unary$$
00036 $index math, AD unary$$
00037 $index unary, AD math$$
00038 
00039 $index acos, AD$$
00040 $index asin, AD$$
00041 $index atan, AD$$
00042 $index cos, AD$$
00043 $index cosh, AD$$
00044 $index exp, AD$$
00045 $index fabs, AD$$
00046 $index log, AD$$
00047 $index log10, AD$$
00048 $index sin, AD$$
00049 $index sinh, AD$$
00050 $index sqrt, AD$$
00051 $index tan, AD$$
00052 $index tanh, AD$$
00053 
00054 $section AD Standard Math Unary Functions$$
00055 
00056 $head Syntax$$
00057 $icode%y% = %fun%(%x%)%$$
00058 
00059 
00060 $head Purpose$$
00061 Evaluates the one argument standard math function 
00062 $icode fun$$ where its argument is an 
00063 $cref/AD of/glossary/AD of Base/$$ $icode Base$$ object.
00064 
00065 $head x$$
00066 The argument $icode x$$ has one of the following prototypes
00067 $codei%
00068      const AD<%Base%>               &%x%
00069      const VecAD<%Base%>::reference &%x%
00070 %$$
00071 
00072 $head y$$
00073 The result $icode y$$ has prototype
00074 $codei%
00075      AD<%Base%> %y%
00076 %$$
00077 
00078 
00079 $head Operation Sequence$$
00080 Most of these functions are AD of $icode Base$$
00081 $cref/atomic operations/glossary/Operation/Atomic/$$.
00082 In all cases,
00083 The AD of $icode Base$$
00084 operation sequence used to calculate $icode y$$ is 
00085 $cref/independent/glossary/Operation/Independent/$$
00086 of $icode x$$.
00087 
00088 $head fun$$ 
00089 A definition of $icode fun$$ is included 
00090 for each of the following functions:
00091 $code acos$$,
00092 $code asin$$,
00093 $code atan$$,
00094 $code cos$$,
00095 $code cosh$$,
00096 $code exp$$,
00097 $code fabs$$,
00098 $code log$$,
00099 $code log10$$,
00100 $code sin$$,
00101 $code sinh$$,
00102 $code sqrt$$,
00103 $code tan$$,
00104 $code tanh$$.
00105 
00106 
00107 $head Examples$$
00108 The following files
00109 contain examples and tests of these functions.   
00110 Each test returns true if it succeeds and false otherwise.
00111 $children%
00112      example/acos.cpp%
00113      example/asin.cpp%
00114      example/atan.cpp%
00115      example/cos.cpp%
00116      example/cosh.cpp%
00117      example/exp.cpp%
00118      example/log.cpp%
00119      example/log10.cpp%
00120      example/sin.cpp%
00121      example/sinh.cpp%
00122      example/sqrt.cpp%
00123      example/tan.cpp%
00124      example/tanh.cpp
00125 %$$
00126 $table
00127 $rref abs.cpp$$
00128 $rref Acos.cpp$$
00129 $rref Asin.cpp$$
00130 $rref atan.cpp$$
00131 $rref cos.cpp$$
00132 $rref cosh.cpp$$
00133 $rref exp.cpp$$
00134 $rref log.cpp$$
00135 $rref log10.cpp$$
00136 $rref sin.cpp$$
00137 $rref sinh.cpp$$
00138 $rref sqrt.cpp$$
00139 $rref tan.cpp$$
00140 $rref tanh.cpp$$
00141 $tend
00142 
00143 
00144 $head Derivatives$$
00145 Each of these functions satisfy a standard math function differential equation.
00146 Calculating derivatives using this differential equation 
00147 is discussed for 
00148 both $cref/forward/ForwardTheory/Standard Math Functions/$$
00149 and $cref/reverse/ReverseTheory/Standard Math Functions/$$ mode.
00150 The exact form of the differential equation
00151 for each of these functions is listed below:
00152 
00153 $subhead acos$$
00154 $latex \[
00155 \begin{array}{lcr}
00156      \D{[ {\rm acos} (x) ]}{x} & = & - (1 - x * x)^{-1/2}
00157 \end{array}
00158 \] $$
00159 
00160 $subhead asin$$
00161 $latex \[
00162 \begin{array}{lcr}
00163      \D{[ {\rm asin} (x) ]}{x} & = & (1 - x * x)^{-1/2}
00164 \end{array}
00165 \] $$
00166 
00167 $subhead atan$$
00168 $latex \[
00169 \begin{array}{lcr}
00170         \D{[ {\rm atan} (x) ]}{x} & = & \frac{1}{1 + x^2}
00171 \end{array}
00172 \] $$
00173 
00174 $subhead cos$$
00175 $latex \[
00176 \begin{array}{lcr}
00177         \D{[ \cos (x) ]}{x} & = & - \sin (x)  \\
00178         \D{[ \sin (x) ]}{x} & = & \cos (x)
00179 \end{array}
00180 \] $$
00181 
00182 $subhead cosh$$
00183 $latex \[
00184 \begin{array}{lcr}
00185         \D{[ \cosh (x) ]}{x} & = & \sinh (x)  \\
00186         \D{[ \sin (x) ]}{x}  & = & \cosh (x)
00187 \end{array}
00188 \] $$
00189 
00190 $subhead exp$$
00191 $latex \[
00192 \begin{array}{lcr}
00193         \D{[ \exp (x) ]}{x} & = & \exp (x)
00194 \end{array}
00195 \] $$
00196 
00197 $subhead log$$
00198 $latex \[
00199 \begin{array}{lcr}
00200         \D{[ \log (x) ]}{x} & = & \frac{1}{x}
00201 \end{array}
00202 \] $$
00203 
00204 $subhead log10$$
00205 This function is special in that it's derivatives are calculated
00206 using the relation
00207 $latex \[
00208 \begin{array}{lcr}
00209         {\rm log10} (x) & = & \log(x) / \log(10)
00210 \end{array}
00211 \] $$
00212 
00213 $subhead sin$$
00214 $latex \[
00215 \begin{array}{lcr}
00216         \D{[ \sin (x) ]}{x} & = & \cos (x) \\
00217         \D{[ \cos (x) ]}{x} & = & - \sin (x) 
00218 \end{array}
00219 \] $$
00220 
00221 $subhead sinh$$
00222 $latex \[
00223 \begin{array}{lcr}
00224         \D{[ \sinh (x) ]}{x} & = & \cosh (x)   \\
00225         \D{[ \cosh (x) ]}{x} & = & \sinh (x)
00226 \end{array}
00227 \] $$
00228 
00229 $subhead sqrt$$
00230 $latex \[
00231 \begin{array}{lcr}
00232         \D{[ {\rm sqrt} (x) ]}{x} & = & \frac{1}{2 {\rm sqrt} (x) }
00233 \end{array}
00234 \] $$
00235 
00236 $subhead tan$$
00237 $latex \[
00238 \begin{array}{lcr}
00239         \D{[ \tan (x) ]}{x} & = & 1 + \tan (x)^2
00240 \end{array}
00241 \] $$
00242 
00243 $subhead tanh$$
00244 $latex \[
00245 \begin{array}{lcr}
00246         \D{[ \tanh (x) ]}{x} & = & 1 - \tanh (x)^2
00247 \end{array}
00248 \] $$
00249 
00250 $end
00251 -------------------------------------------------------------------------------
00252 */
00253 
00254 /*!
00255 \defgroup std_math_ad_hpp std_math_ad.hpp
00256 \{
00257 \file std_math_ad.hpp
00258 Define AD<Base> standard math functions (using their Base versions)
00259 */
00260 
00261 /*!
00262 \def CPPAD_STANDARD_MATH_UNARY_AD(Name, Op)
00263 Defines function Name with argument type AD<Base> and tape operation Op
00264 
00265 The macro defines the function x.Name() where x has type AD<Base>.
00266 It then uses this funciton to define Name(x) where x has type
00267 AD<Base> or VecAD_reference<Base>. 
00268      
00269 If x is a variable, the tape unary operator Op is used
00270 to record the operation and the result is identified as correspoding
00271 to this operation; i.e., Name(x).taddr_ idendifies the operation and 
00272 Name(x).tape_id_ identifies the tape.
00273 
00274 This macro is used to define AD<Base> versions of 
00275 acos, asin, atan, cos, cosh, exp, fabs, log, sin, sinh, sqrt, tan, tanh.
00276 */
00277 
00278 # define CPPAD_STANDARD_MATH_UNARY_AD(Name, Op)                   \
00279     template <class Base>                                         \
00280     inline AD<Base> Name(const AD<Base> &x)                       \
00281     {   return x.Name(); }                                        \
00282     template <class Base>                                         \
00283     inline AD<Base> AD<Base>::Name (void) const                   \
00284     {                                                             \
00285         AD<Base> result;                                          \
00286         result.value_ = CppAD::Name(value_);                      \
00287         CPPAD_ASSERT_UNKNOWN( Parameter(result) );                \
00288                                                                   \
00289         if( Variable(*this) )                                     \
00290         {   CPPAD_ASSERT_UNKNOWN( NumRes(Op) <= 2 );              \
00291             CPPAD_ASSERT_UNKNOWN( NumArg(Op) == 1 );              \
00292             ADTape<Base> *tape = tape_this();                     \
00293             tape->Rec_.PutArg(taddr_);                            \
00294             result.taddr_ = tape->Rec_.PutOp(Op);                 \
00295             result.tape_id_    = tape->id_;                            \
00296         }                                                         \
00297         return result;                                            \
00298     }                                                             \
00299     template <class Base>                                         \
00300     inline AD<Base> Name(const VecAD_reference<Base> &x)          \
00301     {   return Name( x.ADBase() ); }
00302 
00303 //  BEGIN CppAD namespace
00304 namespace CppAD {
00305 
00306      CPPAD_STANDARD_MATH_UNARY_AD(acos, AcosOp)
00307      CPPAD_STANDARD_MATH_UNARY_AD(asin, AsinOp)
00308      CPPAD_STANDARD_MATH_UNARY_AD(atan, AtanOp)
00309      CPPAD_STANDARD_MATH_UNARY_AD(cos, CosOp)
00310      CPPAD_STANDARD_MATH_UNARY_AD(cosh, CoshOp)
00311      CPPAD_STANDARD_MATH_UNARY_AD(exp, ExpOp)
00312      CPPAD_STANDARD_MATH_UNARY_AD(fabs, AbsOp)
00313      CPPAD_STANDARD_MATH_UNARY_AD(log, LogOp)
00314      CPPAD_STANDARD_MATH_UNARY_AD(sin, SinOp)
00315      CPPAD_STANDARD_MATH_UNARY_AD(sinh, SinhOp)
00316      CPPAD_STANDARD_MATH_UNARY_AD(sqrt, SqrtOp)
00317      CPPAD_STANDARD_MATH_UNARY_AD(tan, TanOp)
00318      CPPAD_STANDARD_MATH_UNARY_AD(tanh, TanhOp)
00319 
00320      /*!
00321      Compute the log of base 10 of x where  has type AD<Base>
00322 
00323      \tparam Base
00324      is the base type (different from base for log) 
00325      for this AD type, see base_require.
00326 
00327      \param x
00328      is the argument for the log10 function.
00329 
00330      \result
00331      if the result is y, then \f$ x = 10^y \f$.
00332      */
00333      template <class Base>
00334      inline AD<Base> log10(const AD<Base> &x)
00335      {    return CppAD::log(x) / CppAD::log( Base(10) ); }
00336      template <class Base>
00337      inline AD<Base> log10(const VecAD_reference<Base> &x)
00338      {    return CppAD::log(x.ADBase()) / CppAD::log( Base(10) ); }
00339 }
00340 
00341 # undef CPPAD_STANDARD_MATH_UNARY_AD
00342 
00343 /*! \} */
00344 # endif 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines