00001 # ifndef CPPAD_STD_MATH_UNARY_INCLUDED 00002 # define CPPAD_STD_MATH_UNARY_INCLUDED 00003 00004 /* -------------------------------------------------------------------------- 00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell 00006 00007 CppAD is distributed under multiple licenses. This distribution is under 00008 the terms of the 00009 Common Public License Version 1.0. 00010 00011 A copy of this license is included in the COPYING file of this distribution. 00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00013 -------------------------------------------------------------------------- */ 00014 00015 /* 00016 ------------------------------------------------------------------------------- 00017 $begin StdMathUnary$$ 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 $$ 00032 00033 $index standard, AD math unary$$ 00034 $index math, AD unary$$ 00035 $index unary, AD math$$ 00036 00037 $index acos, AD$$ 00038 $index asin, AD$$ 00039 $index atan, AD$$ 00040 $index cos, AD$$ 00041 $index cosh, AD$$ 00042 $index exp, AD$$ 00043 $index log, AD$$ 00044 $index log10, AD$$ 00045 $index sin, AD$$ 00046 $index sinh, AD$$ 00047 $index sqrt, AD$$ 00048 $index tan, AD$$ 00049 00050 $section AD Standard Math Unary Functions$$ 00051 00052 $head Syntax$$ 00053 $syntax%%y% = %fun%(%x%)%$$ 00054 00055 00056 $head Purpose$$ 00057 Evaluates the one argument standard math function 00058 $italic fun$$ where its argument is an 00059 $xref/glossary/AD of Base/AD of/$$ $italic Base$$ object. 00060 00061 $head x$$ 00062 The argument $italic x$$ has one of the following prototypes 00063 $syntax% 00064 const AD<%Base%> &%x% 00065 const VecAD<%Base%>::reference &%x% 00066 %$$ 00067 00068 $head y$$ 00069 The result $italic y$$ has prototype 00070 $syntax% 00071 AD<%Base%> %y% 00072 %$$ 00073 00074 00075 $head Operation Sequence$$ 00076 Most of these functions are AD of $italic Base$$ 00077 $xref/glossary/Operation/Atomic/atomic operations/1/$$. 00078 In all cases, 00079 The AD of $italic Base$$ 00080 operation sequence used to calculate $italic y$$ is 00081 $xref/glossary/Operation/Independent/independent/1/$$ 00082 of $italic x$$. 00083 00084 $head fun$$ 00085 A definition of $italic fun$$ for each of the argument types 00086 list below is included in the $code CppAD$$ namespace 00087 (the corresponding result has the same type as the argument). 00088 These definitions extend to the 00089 $cref/AD levels above/glossary/AD Levels Above Base/$$ 00090 above each of the types list below. 00091 00092 $table 00093 $bold fun$$ 00094 $cnext $bold Meaning$$ 00095 $cnext $code float$$ 00096 $cnext $code double$$ 00097 $cnext $code std::complex<float>$$ 00098 $cnext $code std::complex<double>$$ 00099 $rnext 00100 $code acos$$ 00101 $cnext inverse cosine function 00102 $cnext yes $cnext yes $cnext no $cnext no $rnext 00103 $code asin$$ 00104 $cnext inverse cosine function 00105 $cnext yes $cnext yes $cnext no $cnext no $rnext 00106 $code atan$$ 00107 $cnext inverse tangent function 00108 $cnext yes $cnext yes $cnext no $cnext no $rnext 00109 $code cos$$ 00110 $cnext trigonometric cosine function 00111 $cnext yes $cnext yes $cnext yes $cnext yes $rnext 00112 $code cosh$$ 00113 $cnext hyperbolic cosine function 00114 $cnext yes $cnext yes $cnext yes $cnext yes $rnext 00115 $code exp$$ 00116 $cnext exponential function 00117 $cnext yes $cnext yes $cnext yes $cnext yes $rnext 00118 $code log$$ 00119 $cnext natural logarithm function 00120 $cnext yes $cnext yes $cnext yes $cnext yes $rnext 00121 $code log10$$ 00122 $cnext log base 10 function 00123 $cnext yes $cnext yes $cnext yes $cnext yes $rnext 00124 $code sin$$ 00125 $cnext trigonometric sine function 00126 $cnext yes $cnext yes $cnext yes $cnext yes $rnext 00127 $code sinh$$ 00128 $cnext hyperbolic sine function 00129 $cnext yes $cnext yes $cnext yes $cnext yes $rnext 00130 $code sqrt$$ 00131 $cnext square root function 00132 $cnext yes $cnext yes $cnext yes $cnext yes $rnext 00133 $code tan$$ 00134 $cnext tangent function 00135 $cnext yes $cnext yes $cnext yes $cnext yes $rnext 00136 $tend 00137 00138 00139 $head Examples$$ 00140 The following files 00141 contain examples and tests of these functions. 00142 Each test returns true if it succeeds and false otherwise. 00143 $children% 00144 example/acos.cpp% 00145 example/asin.cpp% 00146 example/atan.cpp% 00147 example/cos.cpp% 00148 example/cosh.cpp% 00149 example/exp.cpp% 00150 example/log.cpp% 00151 example/log_10.cpp% 00152 example/sin.cpp% 00153 example/sinh.cpp% 00154 example/sqrt.cpp% 00155 example/tan.cpp 00156 %$$ 00157 $table 00158 $rref Acos.cpp$$ 00159 $rref Asin.cpp$$ 00160 $rref Atan.cpp$$ 00161 $rref Cos.cpp$$ 00162 $rref Cosh.cpp$$ 00163 $rref Exp.cpp$$ 00164 $rref Log.cpp$$ 00165 $rref Log10.cpp$$ 00166 $rref Sin.cpp$$ 00167 $rref Sinh.cpp$$ 00168 $rref Sqrt.cpp$$ 00169 $rref Tan.cpp$$ 00170 $tend 00171 00172 00173 $head Derivatives$$ 00174 Each of these functions satisfy a standard math function differential equation. 00175 Calculating derivatives using this differential equation 00176 is discussed for 00177 both $xref/ForwardTheory/Standard Math Functions/forward/$$ 00178 and $xref/ReverseTheory/Standard Math Functions/reverse/$$ mode. 00179 The exact form of the differential equation 00180 for each of these functions is listed below: 00181 00182 $subhead acos$$ 00183 $latex \[ 00184 \begin{array}{lcr} 00185 \D{[ {\rm acos} (x) ]}{x} & = & - (1 - x * x)^{-1/2} 00186 \end{array} 00187 \] $$ 00188 00189 $subhead asin$$ 00190 $latex \[ 00191 \begin{array}{lcr} 00192 \D{[ {\rm asin} (x) ]}{x} & = & (1 - x * x)^{-1/2} 00193 \end{array} 00194 \] $$ 00195 00196 $subhead atan$$ 00197 $latex \[ 00198 \begin{array}{lcr} 00199 \D{[ {\rm atan} (x) ]}{x} & = & \frac{1}{1 + x^2} 00200 \end{array} 00201 \] $$ 00202 00203 $subhead cos$$ 00204 $latex \[ 00205 \begin{array}{lcr} 00206 \D{[ \cos (x) ]}{x} & = & - \sin (x) \\ 00207 \D{[ \sin (x) ]}{x} & = & \cos (x) 00208 \end{array} 00209 \] $$ 00210 00211 $subhead cosh$$ 00212 $latex \[ 00213 \begin{array}{lcr} 00214 \D{[ \cosh (x) ]}{x} & = & \sinh (x) \\ 00215 \D{[ \sin (x) ]}{x} & = & \cosh (x) 00216 \end{array} 00217 \] $$ 00218 00219 $subhead exp$$ 00220 $latex \[ 00221 \begin{array}{lcr} 00222 \D{[ \exp (x) ]}{x} & = & \exp (x) 00223 \end{array} 00224 \] $$ 00225 00226 $subhead log$$ 00227 $latex \[ 00228 \begin{array}{lcr} 00229 \D{[ \log (x) ]}{x} & = & \frac{1}{x} 00230 \end{array} 00231 \] $$ 00232 00233 $subhead log10$$ 00234 This function is special in that it's derivatives are calculated 00235 using the relation 00236 $latex \[ 00237 \begin{array}{lcr} 00238 {\rm log10} (x) & = & \log(x) / \log(10) 00239 \end{array} 00240 \] $$ 00241 00242 $subhead sin$$ 00243 $latex \[ 00244 \begin{array}{lcr} 00245 \D{[ \sin (x) ]}{x} & = & \cos (x) \\ 00246 \D{[ \cos (x) ]}{x} & = & - \sin (x) 00247 \end{array} 00248 \] $$ 00249 00250 $subhead sinh$$ 00251 $latex \[ 00252 \begin{array}{lcr} 00253 \D{[ \sinh (x) ]}{x} & = & \cosh (x) \\ 00254 \D{[ \cosh (x) ]}{x} & = & \sinh (x) 00255 \end{array} 00256 \] $$ 00257 00258 $subhead sqrt$$ 00259 $latex \[ 00260 \begin{array}{lcr} 00261 \D{[ {\rm sqrt} (x) ]}{x} & = & \frac{1}{2 {\rm sqrt} (x) } 00262 \end{array} 00263 \] $$ 00264 00265 $subhead tan$$ 00266 This function is special in that it's derivatives are calculated 00267 using the relation 00268 $latex \[ 00269 \begin{array}{lcr} 00270 \tan (x) & = & \sin(x) / \cos(x) 00271 \end{array} 00272 \] $$ 00273 00274 $end 00275 ------------------------------------------------------------------------------- 00276 */ 00277 00278 # define CPPAD_STANDARD_MATH_UNARY_BASE(Name) \ 00279 \ 00280 inline float Name(const float &x) \ 00281 { return std::Name(x); } \ 00282 \ 00283 inline double Name(const double &x) \ 00284 { return std::Name(x); } \ 00285 \ 00286 00287 # define CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(Name, Op) \ 00288 template <class Base> \ 00289 inline AD<Base> AD<Base>::Name (void) const \ 00290 { using CppAD::Name; \ 00291 AD<Base> result; \ 00292 CPPAD_ASSERT_UNKNOWN( Parameter(result) ); \ 00293 result.value_ = Name(value_); \ 00294 if( Variable(*this) ) \ 00295 tape_this()->RecordOp(Op, result, taddr_); \ 00296 return result; \ 00297 } \ 00298 template <class Base> \ 00299 inline AD<Base> Name(const AD<Base> &x) \ 00300 { return x.Name(); } \ 00301 template <class Base> \ 00302 inline AD<Base> Name(const VecAD_reference<Base> &x) \ 00303 { return Name( x.ADBase() ); } 00304 00305 // BEGIN CppAD namespace 00306 namespace CppAD { 00307 00308 // acos 00309 CPPAD_STANDARD_MATH_UNARY_BASE(acos) 00310 CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(acos, AcosOp) 00311 00312 // asin 00313 CPPAD_STANDARD_MATH_UNARY_BASE(asin) 00314 CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(asin, AsinOp) 00315 00316 // atan 00317 CPPAD_STANDARD_MATH_UNARY_BASE(atan) 00318 CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(atan, AtanOp) 00319 00320 // cos 00321 CPPAD_STANDARD_MATH_UNARY_BASE(cos) 00322 CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(cos, CosOp) 00323 00324 // cosh 00325 CPPAD_STANDARD_MATH_UNARY_BASE(cosh) 00326 CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(cosh, CoshOp) 00327 00328 // exp 00329 CPPAD_STANDARD_MATH_UNARY_BASE(exp) 00330 CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(exp, ExpOp) 00331 00332 // log 00333 CPPAD_STANDARD_MATH_UNARY_BASE(log) 00334 CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(log, LogOp) 00335 00336 // log10 00337 template <class Base> 00338 inline AD<Base> log10(const AD<Base> &x) 00339 { return CppAD::log(x) / CppAD::log( Base(10) ); } 00340 template <class Base> 00341 inline AD<Base> log10(const VecAD_reference<Base> &x) 00342 { return CppAD::log(x.ADBase()) / CppAD::log( Base(10) ); } 00343 00344 // sin 00345 CPPAD_STANDARD_MATH_UNARY_BASE(sin) 00346 CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(sin, SinOp) 00347 00348 // sinh 00349 CPPAD_STANDARD_MATH_UNARY_BASE(sinh) 00350 CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(sinh, SinhOp) 00351 00352 // sqrt 00353 CPPAD_STANDARD_MATH_UNARY_BASE(sqrt) 00354 CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE(sqrt, SqrtOp) 00355 00356 // tan 00357 template <class Base> 00358 inline AD<Base> tan(const AD<Base> &x) 00359 { return CppAD::sin(x) / CppAD::cos(x); } 00360 template <class Base> 00361 inline AD<Base> tan(const VecAD_reference<Base> &x) 00362 { return CppAD::sin(x.ADBase()) / CppAD::cos(x.ADBase()); } 00363 } 00364 00365 # undef CPPAD_STANDARD_MATH_UNARY_BASE 00366 # undef CPPAD_STANDARD_MATH_UNARY_BASE_TEMPLATE 00367 00368 # endif