CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_COS_OP_INCLUDED 00003 # define CPPAD_COS_OP_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 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 namespace CppAD { // BEGIN_CPPAD_NAMESPACE 00018 /*! 00019 \file cos_op.hpp 00020 Forward and reverse mode calculations for z = cos(x). 00021 */ 00022 00023 00024 /*! 00025 Compute forward mode Taylor coefficient for result of op = CosOp. 00026 00027 The C++ source code corresponding to this operation is 00028 \verbatim 00029 z = cos(x) 00030 \endverbatim 00031 The auxillary result is 00032 \verbatim 00033 y = sin(x) 00034 \endverbatim 00035 The value of y, and its derivatives, are computed along with the value 00036 and derivatives of z. 00037 00038 \copydetails forward_unary2_op 00039 */ 00040 template <class Base> 00041 inline void forward_cos_op( 00042 size_t p , 00043 size_t q , 00044 size_t i_z , 00045 size_t i_x , 00046 size_t nc_taylor , 00047 Base* taylor ) 00048 { 00049 // check assumptions 00050 CPPAD_ASSERT_UNKNOWN( NumArg(CosOp) == 1 ); 00051 CPPAD_ASSERT_UNKNOWN( NumRes(CosOp) == 2 ); 00052 CPPAD_ASSERT_UNKNOWN( i_x + 1 < i_z ); 00053 CPPAD_ASSERT_UNKNOWN( q < nc_taylor ); 00054 CPPAD_ASSERT_UNKNOWN( p <= q ); 00055 00056 // Taylor coefficients corresponding to argument and result 00057 Base* x = taylor + i_x * nc_taylor; 00058 Base* c = taylor + i_z * nc_taylor; 00059 Base* s = c - nc_taylor; 00060 00061 00062 // rest of this routine is identical for the following cases: 00063 // forward_sin_op, forward_cos_op, forward_sinh_op, forward_cosh_op. 00064 size_t k; 00065 if( p == 0 ) 00066 { s[0] = sin( x[0] ); 00067 c[0] = cos( x[0] ); 00068 p++; 00069 } 00070 for(size_t j = p; j <= q; j++) 00071 { 00072 s[j] = Base(0); 00073 c[j] = Base(0); 00074 for(k = 1; k <= j; k++) 00075 { s[j] += Base(k) * x[k] * c[j-k]; 00076 c[j] -= Base(k) * x[k] * s[j-k]; 00077 } 00078 s[j] /= Base(j); 00079 c[j] /= Base(j); 00080 } 00081 } 00082 00083 /*! 00084 Compute zero order forward mode Taylor coefficient for result of op = CosOp. 00085 00086 The C++ source code corresponding to this operation is 00087 \verbatim 00088 z = cos(x) 00089 \endverbatim 00090 The auxillary result is 00091 \verbatim 00092 y = sin(x) 00093 \endverbatim 00094 The value of y is computed along with the value of z. 00095 00096 \copydetails forward_unary2_op_0 00097 */ 00098 template <class Base> 00099 inline void forward_cos_op_0( 00100 size_t i_z , 00101 size_t i_x , 00102 size_t nc_taylor , 00103 Base* taylor ) 00104 { 00105 // check assumptions 00106 CPPAD_ASSERT_UNKNOWN( NumArg(CosOp) == 1 ); 00107 CPPAD_ASSERT_UNKNOWN( NumRes(CosOp) == 2 ); 00108 CPPAD_ASSERT_UNKNOWN( i_x + 1 < i_z ); 00109 CPPAD_ASSERT_UNKNOWN( 0 < nc_taylor ); 00110 00111 // Taylor coefficients corresponding to argument and result 00112 Base* x = taylor + i_x * nc_taylor; 00113 Base* c = taylor + i_z * nc_taylor; // called z in documentation 00114 Base* s = c - nc_taylor; // called y in documentation 00115 00116 c[0] = cos( x[0] ); 00117 s[0] = sin( x[0] ); 00118 } 00119 /*! 00120 Compute reverse mode partial derivatives for result of op = CosOp. 00121 00122 The C++ source code corresponding to this operation is 00123 \verbatim 00124 z = cos(x) 00125 \endverbatim 00126 The auxillary result is 00127 \verbatim 00128 y = sin(x) 00129 \endverbatim 00130 The value of y is computed along with the value of z. 00131 00132 \copydetails reverse_unary2_op 00133 */ 00134 00135 template <class Base> 00136 inline void reverse_cos_op( 00137 size_t d , 00138 size_t i_z , 00139 size_t i_x , 00140 size_t nc_taylor , 00141 const Base* taylor , 00142 size_t nc_partial , 00143 Base* partial ) 00144 { 00145 // check assumptions 00146 CPPAD_ASSERT_UNKNOWN( NumArg(CosOp) == 1 ); 00147 CPPAD_ASSERT_UNKNOWN( NumRes(CosOp) == 2 ); 00148 CPPAD_ASSERT_UNKNOWN( i_x + 1 < i_z ); 00149 CPPAD_ASSERT_UNKNOWN( d < nc_taylor ); 00150 CPPAD_ASSERT_UNKNOWN( d < nc_partial ); 00151 00152 // Taylor coefficients and partials corresponding to argument 00153 const Base* x = taylor + i_x * nc_taylor; 00154 Base* px = partial + i_x * nc_partial; 00155 00156 // Taylor coefficients and partials corresponding to first result 00157 const Base* c = taylor + i_z * nc_taylor; // called z in doc 00158 Base* pc = partial + i_z * nc_partial; 00159 00160 // Taylor coefficients and partials corresponding to auxillary result 00161 const Base* s = c - nc_taylor; // called y in documentation 00162 Base* ps = pc - nc_partial; 00163 00164 00165 // rest of this routine is identical for the following cases: 00166 // reverse_sin_op, reverse_cos_op, reverse_sinh_op, reverse_cosh_op. 00167 size_t j = d; 00168 size_t k; 00169 while(j) 00170 { 00171 ps[j] /= Base(j); 00172 pc[j] /= Base(j); 00173 for(k = 1; k <= j; k++) 00174 { 00175 px[k] += ps[j] * Base(k) * c[j-k]; 00176 px[k] -= pc[j] * Base(k) * s[j-k]; 00177 00178 ps[j-k] -= pc[j] * Base(k) * x[k]; 00179 pc[j-k] += ps[j] * Base(k) * x[k]; 00180 00181 } 00182 --j; 00183 } 00184 px[0] += ps[0] * c[0]; 00185 px[0] -= pc[0] * s[0]; 00186 } 00187 00188 } // END_CPPAD_NAMESPACE 00189 # endif