00001 # ifndef CPPAD_EXP_2_INCLUDED 00002 # define CPPAD_EXP_2_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 $begin exp_2$$ 00016 $spell 00017 cppad-%yy%-%mm%-%dd% 00018 hpp 00019 Apx 00020 cpp 00021 const 00022 exp 00023 bool 00024 $$ 00025 00026 $section Second Order Exponential Approximation$$ 00027 00028 $index exp_2$$ 00029 $index example, algorithm$$ 00030 $index algorithm, example$$ 00031 $index exp, example$$ 00032 00033 $head Syntax$$ 00034 $syntax%# include "exp_2.hpp"%$$ 00035 $pre 00036 $$ 00037 $syntax%%y% = exp_2(%x%)%$$ 00038 00039 00040 $head Purpose$$ 00041 This is a simple example algorithm that is used to demonstrate 00042 Algorithmic Differentiation 00043 (see $cref exp_eps$$ for a more complex example). 00044 00045 $head Mathematical Form$$ 00046 The exponential function can be defined by 00047 $latex \[ 00048 \exp (x) = 1 + x^1 / 1 ! + x^2 / 2 ! + \cdots 00049 \] $$ 00050 The second order approximation for the exponential function is 00051 $latex \[ 00052 {\rm exp\_2} (x) = 1 + x + x^2 / 2 00053 \] $$ 00054 00055 00056 $head include$$ 00057 The include command in the syntax is relative to 00058 $syntax% 00059 cppad-%yy%-%mm%-%dd%/introduction/exp_apx 00060 %$$ 00061 where $syntax%cppad-%yy%-%mm%-%dd%$$ is the distribution directory 00062 created during the beginning steps of the 00063 $cref%installation%Install%$$ of CppAD. 00064 00065 $head x$$ 00066 The argument $italic x$$ has prototype 00067 $syntax% 00068 const %Type% &%x% 00069 %$$ 00070 (see $italic Type$$ below). 00071 It specifies the point at which to evaluate the 00072 approximation for the second order exponential approximation. 00073 00074 $head y$$ 00075 The result $italic y$$ has prototype 00076 $syntax% 00077 %Type% %y% 00078 %$$ 00079 It is the value of the exponential function 00080 approximation defined above. 00081 00082 $head Type$$ 00083 If $italic u$$ and $italic v$$ are $italic Type$$ objects and $italic i$$ 00084 is an $code int$$: 00085 00086 $table 00087 $bold Operation$$ $cnext $bold Result Type$$ $cnext $bold Description$$ 00088 $rnext 00089 $syntax%%Type%(%i%)%$$ 00090 $cnext $italic Type$$ 00091 $cnext object with value equal to $italic i$$ 00092 $rnext 00093 $syntax%%u% = %v%$$ 00094 $cnext $italic Type$$ 00095 $cnext new $italic u$$ (and result) is value of $italic v$$ 00096 $rnext 00097 $syntax%%u% * %v%$$ 00098 $cnext $italic Type$$ 00099 $cnext result is value of $latex u * v$$ 00100 $rnext 00101 $syntax%%u% / %v%$$ 00102 $cnext $italic Type$$ 00103 $cnext result is value of $latex u / v$$ 00104 $rnext 00105 $syntax%%u% + %v%$$ 00106 $cnext $italic Type$$ 00107 $cnext result is value of $latex u + v$$ 00108 $tend 00109 00110 $childtable% 00111 introduction/exp_apx/exp_2.omh% 00112 introduction/exp_apx/exp_2_cppad.cpp 00113 %$$ 00114 00115 00116 $head Implementation$$ 00117 The file $xref/exp_2.hpp/$$ 00118 contains a C++ implementation of this function. 00119 00120 $head Test$$ 00121 The file $xref/exp_2.cpp/$$ 00122 contains a test of this implementation. 00123 It returns true for success and false for failure. 00124 00125 00126 $head Exercises$$ 00127 $list number$$ 00128 Suppose that we make the call 00129 $codep 00130 double x = .1; 00131 double y = exp_2(x); 00132 $$ 00133 What is the value assigned to 00134 $code v1$$, $code v2$$, ... ,$code v5$$ in $cref/exp_2.hpp/$$ ? 00135 $lnext 00136 Extend the routine $code exp_2.hpp$$ to 00137 a routine $code exp_3.hpp$$ that computes 00138 $latex \[ 00139 1 + x^2 / 2 ! + x^3 / 3 ! 00140 \] $$ 00141 Do this in a way that only assigns one value to each variable 00142 (as $code exp_2$$ does). 00143 $lnext 00144 Suppose that we make the call 00145 $codep 00146 double x = .5; 00147 double y = exp_3(x); 00148 $$ 00149 using $code exp_3$$ created in the previous problem. 00150 What is the value assigned to the new variables in $code exp_3$$ 00151 (variables that are in $code exp_3$$ and not in $code exp_2$$) ? 00152 $lend 00153 00154 $end 00155 ------------------------------------------------------------------------------ 00156 */ 00157 // BEGIN PROGRAM 00158 template <class Type> 00159 Type exp_2(const Type &x) 00160 { Type v1 = x; // v1 = x 00161 Type v2 = Type(1) + v1; // v2 = 1 + x 00162 Type v3 = v1 * v1; // v3 = x^2 00163 Type v4 = v3 / Type(2); // v4 = x^2 / 2 00164 Type v5 = v2 + v4; // v5 = 1 + x + x^2 / 2 00165 return v5; // exp_2(x) = 1 + x + x^2 / 2 00166 } 00167 // END PROGRAM 00168 00169 # endif