00001 # ifndef CPPAD_SIN_COS_INCLUDED 00002 # define CPPAD_SIN_COS_INCLUDED 00003 00004 /* -------------------------------------------------------------------------- 00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-06 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 $begin ForSinCos$$ 00017 $spell 00018 Hyp 00019 Sin 00020 Cos 00021 Taylor 00022 const 00023 inline 00024 Op 00025 ps 00026 $$ 00027 00028 $index forward, cos$$ 00029 $index forward, cosh$$ 00030 $index forward, sin$$ 00031 $index forward, sinh$$ 00032 $index cos, forward$$ 00033 $index cosh, forward$$ 00034 $index sin, forward$$ 00035 $index sinh, forward$$ 00036 $index ForSinCos$$ 00037 00038 $section Forward Mode Trigonometric and Hyperbolic Sine and Cosine$$ 00039 00040 $head Syntax$$ 00041 00042 $syntax%inline void For%Type%SinCos(size_t %d%, 00043 %Base% *%s%, %Base% *%c%, const %Base% *%x%)%$$ 00044 00045 $head Description$$ 00046 Computes the $italic d$$ order Taylor coefficient for $latex S$$ 00047 and $latex C$$ where are given by 00048 00049 $table 00050 $italic Type$$ 00051 $cnext $latex S$$ 00052 $cnext $latex C$$ 00053 $rnext 00054 $code Trig$$ 00055 $cnext $latex \sin [ X(t) ]$$ 00056 $cnext $latex \cos [ X(t) ]$$ 00057 $rnext 00058 $code Hyp$$ 00059 $cnext $latex \sinh[ X(t) ]$$ 00060 $cnext $latex \cosh [ X(t) ]$$ 00061 $tend 00062 00063 $head x$$ 00064 The vector $italic x$$ has length $latex d+1$$ and contains the 00065 $th d$$ order Taylor coefficient matrix for $italic X$$. 00066 00067 $head s$$ 00068 The vector $italic s$$ has length $latex d+1$$. 00069 On input it contains the 00070 $th d-1$$ order Taylor coefficient matrix for $italic S$$. 00071 On output it contains the 00072 $th d$$ order Taylor coefficient matrix for $italic S$$; i.e., 00073 $syntax%%s%[%d%]%$$ is set equal to the $th d$$ Taylor coefficient for 00074 the function $latex S$$. 00075 00076 $head c$$ 00077 The vector $italic c$$ has length $latex d+1$$. 00078 On input it contains the 00079 $th d-1$$ order Taylor coefficient matrix for $italic C$$. 00080 On output it contains the 00081 $th d$$ order Taylor coefficient matrix for $italic C$$; i.e., 00082 $syntax%%c%[%d%]%$$ is set equal to the $th d$$ Taylor coefficient for 00083 the function $latex C$$. 00084 00085 $end 00086 ------------------------------------------------------------------------------ 00087 $begin RevSinCos$$ 00088 $spell 00089 Hyp 00090 Cos 00091 Cosh 00092 ps 00093 Sin 00094 Taylor 00095 const 00096 inline 00097 Op 00098 px 00099 py 00100 pz 00101 $$ 00102 00103 $index reverse, cos$$ 00104 $index reverse, cosh$$ 00105 $index reverse, sin$$ 00106 $index reverse, sinh$$ 00107 $index cos, reverse$$ 00108 $index cosh, reverse$$ 00109 $index sin, reverse$$ 00110 $index sinh, reverse$$ 00111 $index RevSinCos$$ 00112 00113 $section Reverse Mode Sine and Cosine Functions$$ 00114 00115 $head Syntax$$ 00116 00117 $syntax%inline void Rev%Type%SinCos(size_t %d%, 00118 const %Base% *%s%, const %Base% *%c%, const %Base% *%x%, 00119 %Base% *%ps%, %Base% *%pc%, %Base% *%px%)%$$ 00120 00121 $head Description$$ 00122 We are given the partial derivatives for a function 00123 $latex G(s, c, x)$$ and we wish to compute the partial derivatives for 00124 the function 00125 $latex \[ 00126 H(x) = G [ S(x) , C(x) , x ] 00127 \]$$ 00128 where $latex S(x)$$ and $latex C(x)$$ are defined as the 00129 $th d$$ order Taylor coefficient matrix for sine and cosine 00130 as a function of the corresponding matrix for $italic X$$; i.e., 00131 00132 $table 00133 $italic Type$$ 00134 $cnext $latex S$$ 00135 $cnext $latex C$$ 00136 $rnext 00137 $code Trig$$ 00138 $cnext $latex \sin [ X(t) ]$$ 00139 $cnext $latex \cos [ X(t) ]$$ 00140 $rnext 00141 $code Hyp$$ 00142 $cnext $latex \sinh[ X(t) ]$$ 00143 $cnext $latex \cosh [ X(t) ]$$ 00144 $tend 00145 00146 Note that $italic S$$ and $latex C$$ have 00147 been used both the original 00148 functions and for the corresponding mapping of Taylor coefficients. 00149 00150 $head x$$ 00151 The vector $italic x$$ has length $latex d+1$$ and contains the 00152 $th d$$ order Taylor coefficient matrix for $italic X$$. 00153 00154 00155 $head s$$ 00156 The vector $italic s$$ has length $latex d+1$$ and contains 00157 $th d$$ order Taylor coefficient matrix for $italic S$$. 00158 00159 $head c$$ 00160 The vector $italic c$$ has length $latex d+1$$ and contains 00161 $th d$$ order Taylor coefficient matrix for $italic C$$. 00162 00163 00164 $head On Input$$ 00165 00166 $subhead px$$ 00167 The vector $italic px$$ has length $latex d+1$$ and 00168 $syntax%%px%[%j%]%$$ contains the partial for $italic G$$ 00169 with respect to the $th j$$ order Taylor coefficient for $italic X$$. 00170 00171 $subhead ps$$ 00172 The vector $italic ps$$ has length $latex d+1$$ and 00173 $syntax%%ps%[%j%]%$$ contains the partial for $italic G$$ 00174 with respect to the $th j$$ order Taylor coefficient for $italic S$$. 00175 00176 $subhead pc$$ 00177 The vector $italic pc$$ has length $latex d+1$$ and 00178 $syntax%%pc%[%j%]%$$ contains the partial for $italic G$$ 00179 with respect to the $th j$$ order Taylor coefficient for $italic C$$. 00180 00181 $head On Output$$ 00182 00183 $subhead px$$ 00184 The vector $italic px$$ has length $latex d+1$$ and 00185 $syntax%%px%[%j%]%$$ contains the partial for $italic H$$ 00186 with respect to the $th j$$ order Taylor coefficient for $italic X$$. 00187 00188 $subhead ps$$ 00189 The vector $italic ps$$ has length $latex d+1$$ and 00190 its contents are no longer specified; i.e., it has 00191 been used for work space. 00192 00193 $subhead pc$$ 00194 The vector $italic pc$$ has length $latex d+1$$ and 00195 its contents are no longer specified; i.e., it has 00196 been used for work space. 00197 00198 $end 00199 ------------------------------------------------------------------------------ 00200 */ 00201 00202 // BEGIN CppAD namespace 00203 namespace CppAD { 00204 00205 template <class Base> 00206 inline void ForTrigSinCos(size_t j, 00207 Base *s, Base *c, const Base *x) 00208 { size_t k; 00209 00210 if( j == 0 ) 00211 { s[j] = sin( x[0] ); 00212 c[j] = cos( x[0] ); 00213 } 00214 else 00215 { 00216 s[j] = Base(0); 00217 c[j] = Base(0); 00218 for(k = 1; k <= j; k++) 00219 { s[j] += Base(k) * x[k] * c[j-k]; 00220 c[j] -= Base(k) * x[k] * s[j-k]; 00221 } 00222 s[j] /= Base(j); 00223 c[j] /= Base(j); 00224 } 00225 } 00226 00227 template <class Base> 00228 inline void ForHypSinCos(size_t j, 00229 Base *s, Base *c, const Base *x) 00230 { size_t k; 00231 00232 if( j == 0 ) 00233 { s[j] = sinh( x[0] ); 00234 c[j] = cosh( x[0] ); 00235 } 00236 else 00237 { 00238 s[j] = Base(0); 00239 c[j] = Base(0); 00240 for(k = 1; k <= j; k++) 00241 { s[j] += Base(k) * x[k] * c[j-k]; 00242 c[j] += Base(k) * x[k] * s[j-k]; 00243 } 00244 s[j] /= Base(j); 00245 c[j] /= Base(j); 00246 } 00247 } 00248 00249 template <class Base> 00250 inline void RevTrigSinCos(size_t d, 00251 const Base *s, const Base *c, const Base *x, 00252 Base *ps, Base *pc, Base *px) 00253 { size_t k; 00254 00255 // number of indices to access 00256 size_t j = d; 00257 00258 while(j) 00259 { 00260 ps[j] /= Base(j); 00261 pc[j] /= Base(j); 00262 for(k = 1; k <= j; k++) 00263 { 00264 px[k] += ps[j] * Base(k) * c[j-k]; 00265 px[k] -= pc[j] * Base(k) * s[j-k]; 00266 00267 ps[j-k] -= pc[j] * Base(k) * x[k]; 00268 pc[j-k] += ps[j] * Base(k) * x[k]; 00269 00270 } 00271 --j; 00272 } 00273 px[0] += ps[0] * c[0]; 00274 px[0] -= pc[0] * s[0]; 00275 } 00276 00277 template <class Base> 00278 inline void RevHypSinCos(size_t d, 00279 const Base *s, const Base *c, const Base *x, 00280 Base *ps, Base *pc, Base *px) 00281 { size_t k; 00282 00283 // number of indices to access 00284 size_t j = d; 00285 00286 while(j) 00287 { 00288 ps[j] /= Base(j); 00289 pc[j] /= Base(j); 00290 for(k = 1; k <= j; k++) 00291 { 00292 px[k] += ps[j] * Base(k) * c[j-k]; 00293 px[k] += pc[j] * Base(k) * s[j-k]; 00294 00295 ps[j-k] += pc[j] * Base(k) * x[k]; 00296 pc[j-k] += ps[j] * Base(k) * x[k]; 00297 00298 } 00299 --j; 00300 } 00301 px[0] += ps[0] * c[0]; 00302 px[0] += pc[0] * s[0]; 00303 } 00304 00305 } // END CppAD namespace 00306 00307 # endif