CppAD: A C++ Algorithmic Differentiation Package 20110419
|
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-09 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 $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 sqrt 00029 CppAD 00030 namespace 00031 tanh 00032 $$ 00033 00034 $index standard, AD math unary$$ 00035 $index math, AD unary$$ 00036 $index unary, AD math$$ 00037 00038 $index acos, AD$$ 00039 $index asin, AD$$ 00040 $index atan, AD$$ 00041 $index cos, AD$$ 00042 $index cosh, AD$$ 00043 $index exp, AD$$ 00044 $index log, AD$$ 00045 $index log10, AD$$ 00046 $index sin, AD$$ 00047 $index sinh, AD$$ 00048 $index sqrt, AD$$ 00049 $index tan, AD$$ 00050 $index tanh, AD$$ 00051 00052 $section AD Standard Math Unary Functions$$ 00053 00054 $head Syntax$$ 00055 $syntax%%y% = %fun%(%x%)%$$ 00056 00057 00058 $head Purpose$$ 00059 Evaluates the one argument standard math function 00060 $italic fun$$ where its argument is an 00061 $xref/glossary/AD of Base/AD of/$$ $italic Base$$ object. 00062 00063 $head x$$ 00064 The argument $italic x$$ has one of the following prototypes 00065 $syntax% 00066 const AD<%Base%> &%x% 00067 const VecAD<%Base%>::reference &%x% 00068 %$$ 00069 00070 $head y$$ 00071 The result $italic y$$ has prototype 00072 $syntax% 00073 AD<%Base%> %y% 00074 %$$ 00075 00076 00077 $head Operation Sequence$$ 00078 Most of these functions are AD of $italic Base$$ 00079 $xref/glossary/Operation/Atomic/atomic operations/1/$$. 00080 In all cases, 00081 The AD of $italic Base$$ 00082 operation sequence used to calculate $italic y$$ is 00083 $xref/glossary/Operation/Independent/independent/1/$$ 00084 of $italic x$$. 00085 00086 $head fun$$ 00087 A definition of $italic fun$$ is included 00088 for each of the following functions: 00089 $code acos$$, 00090 $code asin$$, 00091 $code atan$$, 00092 $code cos$$, 00093 $code cosh$$, 00094 $code exp$$, 00095 $code log$$, 00096 $code log10$$, 00097 $code sin$$, 00098 $code sinh$$, 00099 $code sqrt$$, 00100 $code tan$$, 00101 $code tanh$$. 00102 00103 00104 $head Examples$$ 00105 The following files 00106 contain examples and tests of these functions. 00107 Each test returns true if it succeeds and false otherwise. 00108 $children% 00109 example/acos.cpp% 00110 example/asin.cpp% 00111 example/atan.cpp% 00112 example/cos.cpp% 00113 example/cosh.cpp% 00114 example/exp.cpp% 00115 example/log.cpp% 00116 example/log_10.cpp% 00117 example/sin.cpp% 00118 example/sinh.cpp% 00119 example/sqrt.cpp% 00120 example/tan.cpp% 00121 example/tanh.cpp 00122 %$$ 00123 $table 00124 $rref Acos.cpp$$ 00125 $rref Asin.cpp$$ 00126 $rref Atan.cpp$$ 00127 $rref Cos.cpp$$ 00128 $rref Cosh.cpp$$ 00129 $rref Exp.cpp$$ 00130 $rref Log.cpp$$ 00131 $rref Log10.cpp$$ 00132 $rref Sin.cpp$$ 00133 $rref Sinh.cpp$$ 00134 $rref Sqrt.cpp$$ 00135 $rref Tan.cpp$$ 00136 $rref Tanh.cpp$$ 00137 $tend 00138 00139 00140 $head Derivatives$$ 00141 Each of these functions satisfy a standard math function differential equation. 00142 Calculating derivatives using this differential equation 00143 is discussed for 00144 both $xref/ForwardTheory/Standard Math Functions/forward/$$ 00145 and $xref/ReverseTheory/Standard Math Functions/reverse/$$ mode. 00146 The exact form of the differential equation 00147 for each of these functions is listed below: 00148 00149 $subhead acos$$ 00150 $latex \[ 00151 \begin{array}{lcr} 00152 \D{[ {\rm acos} (x) ]}{x} & = & - (1 - x * x)^{-1/2} 00153 \end{array} 00154 \] $$ 00155 00156 $subhead asin$$ 00157 $latex \[ 00158 \begin{array}{lcr} 00159 \D{[ {\rm asin} (x) ]}{x} & = & (1 - x * x)^{-1/2} 00160 \end{array} 00161 \] $$ 00162 00163 $subhead atan$$ 00164 $latex \[ 00165 \begin{array}{lcr} 00166 \D{[ {\rm atan} (x) ]}{x} & = & \frac{1}{1 + x^2} 00167 \end{array} 00168 \] $$ 00169 00170 $subhead cos$$ 00171 $latex \[ 00172 \begin{array}{lcr} 00173 \D{[ \cos (x) ]}{x} & = & - \sin (x) \\ 00174 \D{[ \sin (x) ]}{x} & = & \cos (x) 00175 \end{array} 00176 \] $$ 00177 00178 $subhead cosh$$ 00179 $latex \[ 00180 \begin{array}{lcr} 00181 \D{[ \cosh (x) ]}{x} & = & \sinh (x) \\ 00182 \D{[ \sin (x) ]}{x} & = & \cosh (x) 00183 \end{array} 00184 \] $$ 00185 00186 $subhead exp$$ 00187 $latex \[ 00188 \begin{array}{lcr} 00189 \D{[ \exp (x) ]}{x} & = & \exp (x) 00190 \end{array} 00191 \] $$ 00192 00193 $subhead log$$ 00194 $latex \[ 00195 \begin{array}{lcr} 00196 \D{[ \log (x) ]}{x} & = & \frac{1}{x} 00197 \end{array} 00198 \] $$ 00199 00200 $subhead log10$$ 00201 This function is special in that it's derivatives are calculated 00202 using the relation 00203 $latex \[ 00204 \begin{array}{lcr} 00205 {\rm log10} (x) & = & \log(x) / \log(10) 00206 \end{array} 00207 \] $$ 00208 00209 $subhead sin$$ 00210 $latex \[ 00211 \begin{array}{lcr} 00212 \D{[ \sin (x) ]}{x} & = & \cos (x) \\ 00213 \D{[ \cos (x) ]}{x} & = & - \sin (x) 00214 \end{array} 00215 \] $$ 00216 00217 $subhead sinh$$ 00218 $latex \[ 00219 \begin{array}{lcr} 00220 \D{[ \sinh (x) ]}{x} & = & \cosh (x) \\ 00221 \D{[ \cosh (x) ]}{x} & = & \sinh (x) 00222 \end{array} 00223 \] $$ 00224 00225 $subhead sqrt$$ 00226 $latex \[ 00227 \begin{array}{lcr} 00228 \D{[ {\rm sqrt} (x) ]}{x} & = & \frac{1}{2 {\rm sqrt} (x) } 00229 \end{array} 00230 \] $$ 00231 00232 $subhead tan$$ 00233 This function is special in that it's derivatives are calculated 00234 using the relation 00235 $latex \[ 00236 \begin{array}{lcr} 00237 \tan (x) & = & \sin(x) / \cos(x) 00238 \end{array} 00239 \] $$ 00240 00241 $subhead tanh$$ 00242 This function is also special in that it's derivatives are calculated 00243 using the relation 00244 $latex \[ 00245 \begin{array}{lcr} 00246 \tanh (x) & = & \sinh(x) / \cosh(x) 00247 \end{array} 00248 \] $$ 00249 00250 $end 00251 ------------------------------------------------------------------------------- 00252 */ 00253 00254 # include <cppad/std_math_unary.hpp> 00255 00256 # define CPPAD_STANDARD_MATH_UNARY_AD(Name, Op) \ 00257 template <class Base> \ 00258 inline AD<Base> AD<Base>::Name (void) const \ 00259 { \ 00260 AD<Base> result; \ 00261 result.value_ = CppAD::Name(value_); \ 00262 CPPAD_ASSERT_UNKNOWN( Parameter(result) ); \ 00263 \ 00264 if( Variable(*this) ) \ 00265 { CPPAD_ASSERT_UNKNOWN( NumRes(Op) <= 2 ); \ 00266 CPPAD_ASSERT_UNKNOWN( NumArg(Op) == 1 ); \ 00267 ADTape<Base> *tape = tape_this(); \ 00268 tape->Rec_.PutArg(taddr_); \ 00269 result.taddr_ = tape->Rec_.PutOp(Op); \ 00270 result.id_ = tape->id_; \ 00271 } \ 00272 return result; \ 00273 } \ 00274 template <class Base> \ 00275 inline AD<Base> Name(const AD<Base> &x) \ 00276 { return x.Name(); } \ 00277 template <class Base> \ 00278 inline AD<Base> Name(const VecAD_reference<Base> &x) \ 00279 { return Name( x.ADBase() ); } 00280 00281 // BEGIN CppAD namespace 00282 namespace CppAD { 00283 00284 CPPAD_STANDARD_MATH_UNARY_AD(acos, AcosOp) 00285 CPPAD_STANDARD_MATH_UNARY_AD(asin, AsinOp) 00286 CPPAD_STANDARD_MATH_UNARY_AD(atan, AtanOp) 00287 CPPAD_STANDARD_MATH_UNARY_AD(cos, CosOp) 00288 CPPAD_STANDARD_MATH_UNARY_AD(cosh, CoshOp) 00289 CPPAD_STANDARD_MATH_UNARY_AD(exp, ExpOp) 00290 CPPAD_STANDARD_MATH_UNARY_AD(log, LogOp) 00291 // log10 00292 template <class Base> 00293 inline AD<Base> log10(const AD<Base> &x) 00294 { return CppAD::log(x) / CppAD::log( Base(10) ); } 00295 template <class Base> 00296 inline AD<Base> log10(const VecAD_reference<Base> &x) 00297 { return CppAD::log(x.ADBase()) / CppAD::log( Base(10) ); } 00298 CPPAD_STANDARD_MATH_UNARY_AD(sin, SinOp) 00299 CPPAD_STANDARD_MATH_UNARY_AD(sinh, SinhOp) 00300 CPPAD_STANDARD_MATH_UNARY_AD(sqrt, SqrtOp) 00301 // tan 00302 template <class Base> 00303 inline AD<Base> tan(const AD<Base> &x) 00304 { return CppAD::sin(x) / CppAD::cos(x); } 00305 template <class Base> 00306 inline AD<Base> tan(const VecAD_reference<Base> &x) 00307 { return CppAD::sin(x.ADBase()) / CppAD::cos(x.ADBase()); } 00308 // tanh 00309 template <class Base> 00310 inline AD<Base> tanh(const AD<Base> &x) 00311 { return CppAD::sinh(x) / CppAD::cosh(x); } 00312 template <class Base> 00313 inline AD<Base> tanh(const VecAD_reference<Base> &x) 00314 { return CppAD::sinh(x.ADBase()) / CppAD::cosh(x.ADBase()); } 00315 } 00316 00317 # undef CPPAD_STANDARD_MATH_UNARY_AD 00318 00319 # endif