CppAD: A C++ Algorithmic Differentiation Package  20171217
tan_op.hpp
Go to the documentation of this file.
3
4 /* --------------------------------------------------------------------------
6
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.
13 -------------------------------------------------------------------------- */
14
15
17 /*!
18 \file tan_op.hpp
19 Forward and reverse mode calculations for z = tan(x).
20 */
21
22
23 /*!
24 Compute forward mode Taylor coefficient for result of op = TanOp.
25
26 The C++ source code corresponding to this operation is
27 \verbatim
28  z = tan(x)
29 \endverbatim
30 The auxillary result is
31 \verbatim
32  y = tan(x)^2
33 \endverbatim
34 The value of y, and its derivatives, are computed along with the value
35 and derivatives of z.
36
38 */
39 template <class Base>
40 inline void forward_tan_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* z = taylor + i_z * cap_order;
57  Base* y = z - cap_order;
58
59  size_t k;
60  if( p == 0 )
61  { z[0] = tan( x[0] );
62  y[0] = z[0] * z[0];
63  p++;
64  }
65  for(size_t j = p; j <= q; j++)
66  { Base base_j = static_cast<Base>(double(j));
67
68  z[j] = x[j];
69  for(k = 1; k <= j; k++)
70  z[j] += Base(double(k)) * x[k] * y[j-k] / base_j;
71
72  y[j] = z[0] * z[j];
73  for(k = 1; k <= j; k++)
74  y[j] += z[k] * z[j-k];
75  }
76 }
77
78 /*!
79 Multiple directions forward mode Taylor coefficient for op = TanOp.
80
81 The C++ source code corresponding to this operation is
82 \verbatim
83  z = tan(x)
84 \endverbatim
85 The auxillary result is
86 \verbatim
87  y = tan(x)^2
88 \endverbatim
89 The value of y, and its derivatives, are computed along with the value
90 and derivatives of z.
91
93 */
94 template <class Base>
95 inline void forward_tan_op_dir(
96  size_t q ,
97  size_t r ,
98  size_t i_z ,
99  size_t i_x ,
100  size_t cap_order ,
101  Base* taylor )
102 {
103  // check assumptions
106  CPPAD_ASSERT_UNKNOWN( 0 < q );
107  CPPAD_ASSERT_UNKNOWN( q < cap_order );
108
109  // Taylor coefficients corresponding to argument and result
110  size_t num_taylor_per_var = (cap_order-1) * r + 1;
111  Base* x = taylor + i_x * num_taylor_per_var;
112  Base* z = taylor + i_z * num_taylor_per_var;
113  Base* y = z - num_taylor_per_var;
114
115  size_t k;
116  size_t m = (q-1) * r + 1;
117  for(size_t ell = 0; ell < r; ell++)
118  { z[m+ell] = Base(double(q)) * ( x[m+ell] + x[m+ell] * y[0]);
119  for(k = 1; k < q; k++)
120  z[m+ell] += Base(double(k)) * x[(k-1)*r+1+ell] * y[(q-k-1)*r+1+ell];
121  z[m+ell] /= Base(double(q));
122  //
123  y[m+ell] = Base(2.0) * z[m+ell] * z[0];
124  for(k = 1; k < q; k++)
125  y[m+ell] += z[(k-1)*r+1+ell] * z[(q-k-1)*r+1+ell];
126  }
127 }
128
129
130 /*!
131 Compute zero order forward mode Taylor coefficient for result of op = TanOp.
132
133 The C++ source code corresponding to this operation is
134 \verbatim
135  z = tan(x)
136 \endverbatim
137 The auxillary result is
138 \verbatim
139  y = cos(x)
140 \endverbatim
141 The value of y is computed along with the value of z.
142
144 */
145 template <class Base>
146 inline void forward_tan_op_0(
147  size_t i_z ,
148  size_t i_x ,
149  size_t cap_order ,
150  Base* taylor )
151 {
152  // check assumptions
155  CPPAD_ASSERT_UNKNOWN( 0 < cap_order );
156
157  // Taylor coefficients corresponding to argument and result
158  Base* x = taylor + i_x * cap_order;
159  Base* z = taylor + i_z * cap_order; // called z in documentation
160  Base* y = z - cap_order; // called y in documentation
161
162  z[0] = tan( x[0] );
163  y[0] = z[0] * z[0];
164 }
165
166 /*!
167 Compute reverse mode partial derivatives for result of op = TanOp.
168
169 The C++ source code corresponding to this operation is
170 \verbatim
171  z = tan(x)
172 \endverbatim
173 The auxillary result is
174 \verbatim
175  y = cos(x)
176 \endverbatim
177 The value of y is computed along with the value of z.
178
180 */
181
182 template <class Base>
183 inline void reverse_tan_op(
184  size_t d ,
185  size_t i_z ,
186  size_t i_x ,
187  size_t cap_order ,
188  const Base* taylor ,
189  size_t nc_partial ,
190  Base* partial )
191 {
192  // check assumptions
195  CPPAD_ASSERT_UNKNOWN( d < cap_order );
196  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
197
198  // Taylor coefficients and partials corresponding to argument
199  const Base* x = taylor + i_x * cap_order;
200  Base* px = partial + i_x * nc_partial;
201
202  // Taylor coefficients and partials corresponding to first result
203  const Base* z = taylor + i_z * cap_order; // called z in doc
204  Base* pz = partial + i_z * nc_partial;
205
206  // Taylor coefficients and partials corresponding to auxillary result
207  const Base* y = z - cap_order; // called y in documentation
208  Base* py = pz - nc_partial;
209
210
211  size_t j = d;
212  size_t k;
213  Base base_two(2);
214  while(j)
215  {
216  px[j] += pz[j];
217  pz[j] /= Base(double(j));
218  for(k = 1; k <= j; k++)
219  { px[k] += azmul(pz[j], y[j-k]) * Base(double(k));
220  py[j-k] += azmul(pz[j], x[k]) * Base(double(k));
221  }
222  for(k = 0; k < j; k++)
223  pz[k] += azmul(py[j-1], z[j-k-1]) * base_two;
224
225  --j;
226  }
227  px[0] += azmul(pz[0], Base(1.0) + y[0]);
228 }
229
231 # endif
Definition: azmul.hpp:94
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281