CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sub_op.hpp
Go to the documentation of this file.
1 // $Id: sub_op.hpp 3865 2017-01-19 01:57:55Z bradbell $
2 # ifndef CPPAD_LOCAL_SUB_OP_HPP
3 # define CPPAD_LOCAL_SUB_OP_HPP
4 
5 /* --------------------------------------------------------------------------
6 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
7 
8 CppAD is distributed under multiple licenses. This distribution is under
9 the terms of the
10  Eclipse Public License Version 1.0.
11 
12 A copy of this license is included in the COPYING file of this distribution.
13 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14 -------------------------------------------------------------------------- */
15 
16 namespace CppAD { namespace local { // BEGIN_CPPAD_LOCAL_NAMESPACE
17 /*!
18 \file sub_op.hpp
19 Forward and reverse mode calculations for z = x - y.
20 */
21 
22 // --------------------------- Subvv -----------------------------------------
23 /*!
24 Compute forward mode Taylor coefficients for result of op = SubvvOp.
25 
26 The C++ source code corresponding to this operation is
27 \verbatim
28  z = x - y
29 \endverbatim
30 In the documentation below,
31 this operations is for the case where both x and y are variables
32 and the argument \a parameter is not used.
33 
34 \copydetails CppAD::local::forward_binary_op
35 */
36 
37 template <class Base>
38 inline void forward_subvv_op(
39  size_t p ,
40  size_t q ,
41  size_t i_z ,
42  const addr_t* arg ,
43  const Base* parameter ,
44  size_t cap_order ,
45  Base* taylor )
46 {
47  // check assumptions
50  CPPAD_ASSERT_UNKNOWN( q < cap_order );
51  CPPAD_ASSERT_UNKNOWN( p <= q );
52 
53  // Taylor coefficients corresponding to arguments and result
54  Base* x = taylor + arg[0] * cap_order;
55  Base* y = taylor + arg[1] * cap_order;
56  Base* z = taylor + i_z * cap_order;
57 
58  for(size_t d = p; d <= q; d++)
59  z[d] = x[d] - y[d];
60 }
61 /*!
62 Multiple directions forward mode Taylor coefficients for op = SubvvOp.
63 
64 The C++ source code corresponding to this operation is
65 \verbatim
66  z = x - y
67 \endverbatim
68 In the documentation below,
69 this operations is for the case where both x and y are variables
70 and the argument \a parameter is not used.
71 
72 \copydetails CppAD::local::forward_binary_op_dir
73 */
74 
75 template <class Base>
77  size_t q ,
78  size_t r ,
79  size_t i_z ,
80  const addr_t* arg ,
81  const Base* parameter ,
82  size_t cap_order ,
83  Base* taylor )
84 {
85  // check assumptions
88  CPPAD_ASSERT_UNKNOWN( 0 < q );
89  CPPAD_ASSERT_UNKNOWN( q < cap_order );
90 
91  // Taylor coefficients corresponding to arguments and result
92  size_t num_taylor_per_var = (cap_order-1) * r + 1;
93  size_t m = (q-1) * r + 1;
94  Base* x = taylor + arg[0] * num_taylor_per_var + m;
95  Base* y = taylor + arg[1] * num_taylor_per_var + m;
96  Base* z = taylor + i_z * num_taylor_per_var + m;
97 
98  for(size_t ell = 0; ell < r; ell++)
99  z[ell] = x[ell] - y[ell];
100 }
101 
102 /*!
103 Compute zero order forward mode Taylor coefficients for result of op = SubvvOp.
104 
105 The C++ source code corresponding to this operation is
106 \verbatim
107  z = x - y
108 \endverbatim
109 In the documentation below,
110 this operations is for the case where both x and y are variables
111 and the argument \a parameter is not used.
112 
113 \copydetails CppAD::local::forward_binary_op_0
114 */
115 
116 template <class Base>
117 inline void forward_subvv_op_0(
118  size_t i_z ,
119  const addr_t* arg ,
120  const Base* parameter ,
121  size_t cap_order ,
122  Base* taylor )
123 {
124  // check assumptions
127 
128  // Taylor coefficients corresponding to arguments and result
129  Base* x = taylor + arg[0] * cap_order;
130  Base* y = taylor + arg[1] * cap_order;
131  Base* z = taylor + i_z * cap_order;
132 
133  z[0] = x[0] - y[0];
134 }
135 
136 /*!
137 Compute reverse mode partial derivatives for result of op = SubvvOp.
138 
139 The C++ source code corresponding to this operation is
140 \verbatim
141  z = x - y
142 \endverbatim
143 In the documentation below,
144 this operations is for the case where both x and y are variables
145 and the argument \a parameter is not used.
146 
147 \copydetails CppAD::local::reverse_binary_op
148 */
149 
150 template <class Base>
151 inline void reverse_subvv_op(
152  size_t d ,
153  size_t i_z ,
154  const addr_t* arg ,
155  const Base* parameter ,
156  size_t cap_order ,
157  const Base* taylor ,
158  size_t nc_partial ,
159  Base* partial )
160 {
161  // check assumptions
164  CPPAD_ASSERT_UNKNOWN( d < cap_order );
165  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
166 
167  // Partial derivatives corresponding to arguments and result
168  Base* px = partial + arg[0] * nc_partial;
169  Base* py = partial + arg[1] * nc_partial;
170  Base* pz = partial + i_z * nc_partial;
171 
172  // number of indices to access
173  size_t i = d + 1;
174  while(i)
175  { --i;
176  px[i] += pz[i];
177  py[i] -= pz[i];
178  }
179 }
180 
181 // --------------------------- Subpv -----------------------------------------
182 /*!
183 Compute forward mode Taylor coefficients for result of op = SubpvOp.
184 
185 The C++ source code corresponding to this operation is
186 \verbatim
187  z = x - y
188 \endverbatim
189 In the documentation below,
190 this operations is for the case where x is a parameter and y is a variable.
191 
192 \copydetails CppAD::local::forward_binary_op
193 */
194 
195 template <class Base>
196 inline void forward_subpv_op(
197  size_t p ,
198  size_t q ,
199  size_t i_z ,
200  const addr_t* arg ,
201  const Base* parameter ,
202  size_t cap_order ,
203  Base* taylor )
204 {
205  // check assumptions
208  CPPAD_ASSERT_UNKNOWN( q < cap_order );
209  CPPAD_ASSERT_UNKNOWN( p <= q );
210 
211  // Taylor coefficients corresponding to arguments and result
212  Base* y = taylor + arg[1] * cap_order;
213  Base* z = taylor + i_z * cap_order;
214 
215  // Paraemter value
216  Base x = parameter[ arg[0] ];
217  if( p == 0 )
218  { z[0] = x - y[0];
219  p++;
220  }
221  for(size_t d = p; d <= q; d++)
222  z[d] = - y[d];
223 }
224 /*!
225 Multiple directions forward mode Taylor coefficients for op = SubpvOp.
226 
227 The C++ source code corresponding to this operation is
228 \verbatim
229  z = x - y
230 \endverbatim
231 In the documentation below,
232 this operations is for the case where x is a parameter and y is a variable.
233 
234 \copydetails CppAD::local::forward_binary_op_dir
235 */
236 
237 template <class Base>
239  size_t q ,
240  size_t r ,
241  size_t i_z ,
242  const addr_t* arg ,
243  const Base* parameter ,
244  size_t cap_order ,
245  Base* taylor )
246 {
247  // check assumptions
250  CPPAD_ASSERT_UNKNOWN( 0 < q );
251  CPPAD_ASSERT_UNKNOWN( q < cap_order );
252 
253  // Taylor coefficients corresponding to arguments and result
254  size_t num_taylor_per_var = (cap_order-1) * r + 1;
255  size_t m = (q-1) * r + 1;
256  Base* y = taylor + arg[1] * num_taylor_per_var + m;
257  Base* z = taylor + i_z * num_taylor_per_var + m;
258 
259  // Paraemter value
260  for(size_t ell = 0; ell < r; ell++)
261  z[ell] = - y[ell];
262 }
263 /*!
264 Compute zero order forward mode Taylor coefficient for result of op = SubpvOp.
265 
266 The C++ source code corresponding to this operation is
267 \verbatim
268  z = x - y
269 \endverbatim
270 In the documentation below,
271 this operations is for the case where x is a parameter and y is a variable.
272 
273 \copydetails CppAD::local::forward_binary_op_0
274 */
275 
276 template <class Base>
277 inline void forward_subpv_op_0(
278  size_t i_z ,
279  const addr_t* arg ,
280  const Base* parameter ,
281  size_t cap_order ,
282  Base* taylor )
283 {
284  // check assumptions
287 
288  // Paraemter value
289  Base x = parameter[ arg[0] ];
290 
291  // Taylor coefficients corresponding to arguments and result
292  Base* y = taylor + arg[1] * cap_order;
293  Base* z = taylor + i_z * cap_order;
294 
295  z[0] = x - y[0];
296 }
297 
298 /*!
299 Compute reverse mode partial derivative for result of op = SubpvOp.
300 
301 The C++ source code corresponding to this operation is
302 \verbatim
303  z = x - y
304 \endverbatim
305 In the documentation below,
306 this operations is for the case where x is a parameter and y is a variable.
307 
308 \copydetails CppAD::local::reverse_binary_op
309 */
310 
311 template <class Base>
312 inline void reverse_subpv_op(
313  size_t d ,
314  size_t i_z ,
315  const addr_t* arg ,
316  const Base* parameter ,
317  size_t cap_order ,
318  const Base* taylor ,
319  size_t nc_partial ,
320  Base* partial )
321 {
322  // check assumptions
325  CPPAD_ASSERT_UNKNOWN( d < cap_order );
326  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
327 
328  // Partial derivatives corresponding to arguments and result
329  Base* py = partial + arg[1] * nc_partial;
330  Base* pz = partial + i_z * nc_partial;
331 
332  // number of indices to access
333  size_t i = d + 1;
334  while(i)
335  { --i;
336  py[i] -= pz[i];
337  }
338 }
339 
340 // --------------------------- Subvp -----------------------------------------
341 /*!
342 Compute forward mode Taylor coefficients for result of op = SubvvOp.
343 
344 The C++ source code corresponding to this operation is
345 \verbatim
346  z = x - y
347 \endverbatim
348 In the documentation below,
349 this operations is for the case where x is a variable and y is a parameter.
350 
351 \copydetails CppAD::local::forward_binary_op
352 */
353 
354 template <class Base>
355 inline void forward_subvp_op(
356  size_t p ,
357  size_t q ,
358  size_t i_z ,
359  const addr_t* arg ,
360  const Base* parameter ,
361  size_t cap_order ,
362  Base* taylor )
363 {
364  // check assumptions
367  CPPAD_ASSERT_UNKNOWN( q < cap_order );
368  CPPAD_ASSERT_UNKNOWN( p <= q );
369 
370  // Taylor coefficients corresponding to arguments and result
371  Base* x = taylor + arg[0] * cap_order;
372  Base* z = taylor + i_z * cap_order;
373 
374  // Parameter value
375  Base y = parameter[ arg[1] ];
376  if( p == 0 )
377  { z[0] = x[0] - y;
378  p++;
379  }
380  for(size_t d = p; d <= q; d++)
381  z[d] = x[d];
382 }
383 /*!
384 Multiple directions forward mode Taylor coefficients for op = SubvvOp.
385 
386 The C++ source code corresponding to this operation is
387 \verbatim
388  z = x - y
389 \endverbatim
390 In the documentation below,
391 this operations is for the case where x is a variable and y is a parameter.
392 
393 \copydetails CppAD::local::forward_binary_op_dir
394 */
395 
396 template <class Base>
398  size_t q ,
399  size_t r ,
400  size_t i_z ,
401  const addr_t* arg ,
402  const Base* parameter ,
403  size_t cap_order ,
404  Base* taylor )
405 {
406  // check assumptions
409  CPPAD_ASSERT_UNKNOWN( 0 < q );
410  CPPAD_ASSERT_UNKNOWN( q < cap_order );
411 
412  // Taylor coefficients corresponding to arguments and result
413  size_t num_taylor_per_var = (cap_order-1) * r + 1;
414  Base* x = taylor + arg[0] * num_taylor_per_var;
415  Base* z = taylor + i_z * num_taylor_per_var;
416 
417  // Parameter value
418  size_t m = (q-1) * r + 1;
419  for(size_t ell = 0; ell < r; ell++)
420  z[m+ell] = x[m+ell];
421 }
422 
423 /*!
424 Compute zero order forward mode Taylor coefficients for result of op = SubvvOp.
425 
426 The C++ source code corresponding to this operation is
427 \verbatim
428  z = x - y
429 \endverbatim
430 In the documentation below,
431 this operations is for the case where x is a variable and y is a parameter.
432 
433 \copydetails CppAD::local::forward_binary_op_0
434 */
435 
436 template <class Base>
437 inline void forward_subvp_op_0(
438  size_t i_z ,
439  const addr_t* arg ,
440  const Base* parameter ,
441  size_t cap_order ,
442  Base* taylor )
443 {
444  // check assumptions
447 
448  // Parameter value
449  Base y = parameter[ arg[1] ];
450 
451  // Taylor coefficients corresponding to arguments and result
452  Base* x = taylor + arg[0] * cap_order;
453  Base* z = taylor + i_z * cap_order;
454 
455  z[0] = x[0] - y;
456 }
457 
458 /*!
459 Compute reverse mode partial derivative for result of op = SubvpOp.
460 
461 The C++ source code corresponding to this operation is
462 \verbatim
463  z = x - y
464 \endverbatim
465 In the documentation below,
466 this operations is for the case where x is a variable and y is a parameter.
467 
468 \copydetails CppAD::local::reverse_binary_op
469 */
470 
471 template <class Base>
472 inline void reverse_subvp_op(
473  size_t d ,
474  size_t i_z ,
475  const addr_t* arg ,
476  const Base* parameter ,
477  size_t cap_order ,
478  const Base* taylor ,
479  size_t nc_partial ,
480  Base* partial )
481 {
482  // check assumptions
485  CPPAD_ASSERT_UNKNOWN( d < cap_order );
486  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
487 
488  // Partial derivatives corresponding to arguments and result
489  Base* px = partial + arg[0] * nc_partial;
490  Base* pz = partial + i_z * nc_partial;
491 
492  // number of indices to access
493  size_t i = d + 1;
494  while(i)
495  { --i;
496  px[i] += pz[i];
497  }
498 }
499 
500 } } // END_CPPAD_LOCAL_NAMESPACE
501 # endif
void forward_subvp_op(size_t p, size_t q, size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, Base *taylor)
Compute forward mode Taylor coefficients for result of op = SubvvOp.
Definition: sub_op.hpp:355
void reverse_subvv_op(size_t d, size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, const Base *taylor, size_t nc_partial, Base *partial)
Compute reverse mode partial derivatives for result of op = SubvvOp.
Definition: sub_op.hpp:151
void forward_subvv_op(size_t p, size_t q, size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, Base *taylor)
Compute forward mode Taylor coefficients for result of op = SubvvOp.
Definition: sub_op.hpp:38
CPPAD_TAPE_ADDR_TYPE addr_t
Definition: declare_ad.hpp:44
void forward_subvp_op_0(size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, Base *taylor)
Compute zero order forward mode Taylor coefficients for result of op = SubvvOp.
Definition: sub_op.hpp:437
void forward_subpv_op_dir(size_t q, size_t r, size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, Base *taylor)
Multiple directions forward mode Taylor coefficients for op = SubpvOp.
Definition: sub_op.hpp:238
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
void forward_subvv_op_dir(size_t q, size_t r, size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, Base *taylor)
Multiple directions forward mode Taylor coefficients for op = SubvvOp.
Definition: sub_op.hpp:76
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
void forward_subvv_op_0(size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, Base *taylor)
Compute zero order forward mode Taylor coefficients for result of op = SubvvOp.
Definition: sub_op.hpp:117
void forward_subpv_op(size_t p, size_t q, size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, Base *taylor)
Compute forward mode Taylor coefficients for result of op = SubpvOp.
Definition: sub_op.hpp:196
void reverse_subpv_op(size_t d, size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, const Base *taylor, size_t nc_partial, Base *partial)
Compute reverse mode partial derivative for result of op = SubpvOp.
Definition: sub_op.hpp:312
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
void forward_subvp_op_dir(size_t q, size_t r, size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, Base *taylor)
Multiple directions forward mode Taylor coefficients for op = SubvvOp.
Definition: sub_op.hpp:397
void reverse_subvp_op(size_t d, size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, const Base *taylor, size_t nc_partial, Base *partial)
Compute reverse mode partial derivative for result of op = SubvpOp.
Definition: sub_op.hpp:472
void forward_subpv_op_0(size_t i_z, const addr_t *arg, const Base *parameter, size_t cap_order, Base *taylor)
Compute zero order forward mode Taylor coefficient for result of op = SubpvOp.
Definition: sub_op.hpp:277