CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sin_op.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_LOCAL_SIN_OP_HPP
2 # define CPPAD_LOCAL_SIN_OP_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 
15 
16 namespace CppAD { namespace local { // BEGIN_CPPAD_LOCAL_NAMESPACE
17 /*!
18 \file sin_op.hpp
19 Forward and reverse mode calculations for z = sin(x).
20 */
21 
22 
23 /*!
24 Compute forward mode Taylor coefficient for result of op = SinOp.
25 
26 The C++ source code corresponding to this operation is
27 \verbatim
28  z = sin(x)
29 \endverbatim
30 The auxillary result is
31 \verbatim
32  y = cos(x)
33 \endverbatim
34 The value of y, and its derivatives, are computed along with the value
35 and derivatives of z.
36 
37 \copydetails CppAD::local::forward_unary2_op
38 */
39 template <class Base>
40 inline void forward_sin_op(
41  size_t p ,
42  size_t q ,
43  size_t i_z ,
44  size_t i_x ,
45  size_t cap_order ,
46  Base* taylor )
47 {
48  // check assumptions
51  CPPAD_ASSERT_UNKNOWN( q < cap_order );
52  CPPAD_ASSERT_UNKNOWN( p <= q );
53 
54  // Taylor coefficients corresponding to argument and result
55  Base* x = taylor + i_x * cap_order;
56  Base* s = taylor + i_z * cap_order;
57  Base* c = s - cap_order;
58 
59  // rest of this routine is identical for the following cases:
60  // forward_sin_op, forward_cos_op, forward_sinh_op, forward_cosh_op.
61  // (except that there is a sign difference for the hyperbolic case).
62  size_t k;
63  if( p == 0 )
64  { s[0] = sin( x[0] );
65  c[0] = cos( x[0] );
66  p++;
67  }
68  for(size_t j = p; j <= q; j++)
69  {
70  s[j] = Base(0.0);
71  c[j] = Base(0.0);
72  for(k = 1; k <= j; k++)
73  { s[j] += Base(double(k)) * x[k] * c[j-k];
74  c[j] -= Base(double(k)) * x[k] * s[j-k];
75  }
76  s[j] /= Base(double(j));
77  c[j] /= Base(double(j));
78  }
79 }
80 /*!
81 Compute forward mode Taylor coefficient for result of op = SinOp.
82 
83 The C++ source code corresponding to this operation is
84 \verbatim
85  z = sin(x)
86 \endverbatim
87 The auxillary result is
88 \verbatim
89  y = cos(x)
90 \endverbatim
91 The value of y, and its derivatives, are computed along with the value
92 and derivatives of z.
93 
94 \copydetails CppAD::local::forward_unary2_op_dir
95 */
96 template <class Base>
97 inline void forward_sin_op_dir(
98  size_t q ,
99  size_t r ,
100  size_t i_z ,
101  size_t i_x ,
102  size_t cap_order ,
103  Base* taylor )
104 {
105  // check assumptions
108  CPPAD_ASSERT_UNKNOWN( 0 < q );
109  CPPAD_ASSERT_UNKNOWN( q < cap_order );
110 
111  // Taylor coefficients corresponding to argument and result
112  size_t num_taylor_per_var = (cap_order-1) * r + 1;
113  Base* x = taylor + i_x * num_taylor_per_var;
114  Base* s = taylor + i_z * num_taylor_per_var;
115  Base* c = s - num_taylor_per_var;
116 
117 
118  // rest of this routine is identical for the following cases:
119  // forward_sin_op, forward_cos_op, forward_sinh_op, forward_cosh_op
120  // (except that there is a sign difference for the hyperbolic case).
121  size_t m = (q-1) * r + 1;
122  for(size_t ell = 0; ell < r; ell++)
123  { s[m+ell] = Base(double(q)) * x[m + ell] * c[0];
124  c[m+ell] = - Base(double(q)) * x[m + ell] * s[0];
125  for(size_t k = 1; k < q; k++)
126  { s[m+ell] += Base(double(k)) * x[(k-1)*r+1+ell] * c[(q-k-1)*r+1+ell];
127  c[m+ell] -= Base(double(k)) * x[(k-1)*r+1+ell] * s[(q-k-1)*r+1+ell];
128  }
129  s[m+ell] /= Base(double(q));
130  c[m+ell] /= Base(double(q));
131  }
132 }
133 
134 
135 /*!
136 Compute zero order forward mode Taylor coefficient for result of op = SinOp.
137 
138 The C++ source code corresponding to this operation is
139 \verbatim
140  z = sin(x)
141 \endverbatim
142 The auxillary result is
143 \verbatim
144  y = cos(x)
145 \endverbatim
146 The value of y is computed along with the value of z.
147 
148 \copydetails CppAD::local::forward_unary2_op_0
149 */
150 template <class Base>
151 inline void forward_sin_op_0(
152  size_t i_z ,
153  size_t i_x ,
154  size_t cap_order ,
155  Base* taylor )
156 {
157  // check assumptions
160  CPPAD_ASSERT_UNKNOWN( 0 < cap_order );
161 
162  // Taylor coefficients corresponding to argument and result
163  Base* x = taylor + i_x * cap_order;
164  Base* s = taylor + i_z * cap_order; // called z in documentation
165  Base* c = s - cap_order; // called y in documentation
166 
167  s[0] = sin( x[0] );
168  c[0] = cos( x[0] );
169 }
170 
171 /*!
172 Compute reverse mode partial derivatives for result of op = SinOp.
173 
174 The C++ source code corresponding to this operation is
175 \verbatim
176  z = sin(x)
177 \endverbatim
178 The auxillary result is
179 \verbatim
180  y = cos(x)
181 \endverbatim
182 The value of y is computed along with the value of z.
183 
184 \copydetails CppAD::local::reverse_unary2_op
185 */
186 
187 template <class Base>
188 inline void reverse_sin_op(
189  size_t d ,
190  size_t i_z ,
191  size_t i_x ,
192  size_t cap_order ,
193  const Base* taylor ,
194  size_t nc_partial ,
195  Base* partial )
196 {
197  // check assumptions
200  CPPAD_ASSERT_UNKNOWN( d < cap_order );
201  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
202 
203  // Taylor coefficients and partials corresponding to argument
204  const Base* x = taylor + i_x * cap_order;
205  Base* px = partial + i_x * nc_partial;
206 
207  // Taylor coefficients and partials corresponding to first result
208  const Base* s = taylor + i_z * cap_order; // called z in doc
209  Base* ps = partial + i_z * nc_partial;
210 
211  // Taylor coefficients and partials corresponding to auxillary result
212  const Base* c = s - cap_order; // called y in documentation
213  Base* pc = ps - nc_partial;
214 
215 
216  // rest of this routine is identical for the following cases:
217  // reverse_sin_op, reverse_cos_op, reverse_sinh_op, reverse_cosh_op.
218  size_t j = d;
219  size_t k;
220  while(j)
221  {
222  ps[j] /= Base(double(j));
223  pc[j] /= Base(double(j));
224  for(k = 1; k <= j; k++)
225  {
226  px[k] += Base(double(k)) * azmul(ps[j], c[j-k]);
227  px[k] -= Base(double(k)) * azmul(pc[j], s[j-k]);
228 
229  ps[j-k] -= Base(double(k)) * azmul(pc[j], x[k]);
230  pc[j-k] += Base(double(k)) * azmul(ps[j], x[k]);
231 
232  }
233  --j;
234  }
235  px[0] += azmul(ps[0], c[0]);
236  px[0] -= azmul(pc[0], s[0]);
237 }
238 
239 } } // END_CPPAD_LOCAL_NAMESPACE
240 # endif
void reverse_sin_op(size_t d, size_t i_z, size_t i_x, size_t cap_order, const Base *taylor, size_t nc_partial, Base *partial)
Compute reverse mode partial derivatives for result of op = SinOp.
Definition: sin_op.hpp:188
AD< Base > azmul(const AD< Base > &x, const AD< Base > &y)
Definition: azmul.hpp:94
void forward_sin_op_dir(size_t q, size_t r, size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Compute forward mode Taylor coefficient for result of op = SinOp.
Definition: sin_op.hpp:97
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
void forward_sin_op_0(size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Compute zero order forward mode Taylor coefficient for result of op = SinOp.
Definition: sin_op.hpp:151
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
void forward_sin_op(size_t p, size_t q, size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Compute forward mode Taylor coefficient for result of op = SinOp.
Definition: sin_op.hpp:40
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
AD< Base > sin(const AD< Base > &x)
AD< Base > cos(const AD< Base > &x)