CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
add_op.hpp
Go to the documentation of this file.
1 // $Id: add_op.hpp 3865 2017-01-19 01:57:55Z bradbell $
2 # ifndef CPPAD_LOCAL_ADD_OP_HPP
3 # define CPPAD_LOCAL_ADD_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 add_op.hpp
19 Forward and reverse mode calculations for z = x + y.
20 */
21 
22 // --------------------------- Addvv -----------------------------------------
23 /*!
24 Compute forward mode Taylor coefficients for result of op = AddvvOp.
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_addvv_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 j = p; j <= q; j++)
59  z[j] = x[j] + y[j];
60 }
61 /*!
62 Multiple directions forward mode Taylor coefficients for op = AddvvOp.
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( q < cap_order );
89  CPPAD_ASSERT_UNKNOWN( 0 < q );
90 
91  // Taylor coefficients corresponding to arguments and result
92  size_t num_taylor_per_var = (cap_order-1) * r + 1;
93  Base* x = taylor + arg[0] * num_taylor_per_var;
94  Base* y = taylor + arg[1] * num_taylor_per_var;
95  Base* z = taylor + i_z * num_taylor_per_var;
96 
97  size_t m = (q-1)*r + 1 ;
98  for(size_t ell = 0; ell < r; ell++)
99  z[m+ell] = x[m+ell] + y[m+ell];
100 }
101 
102 /*!
103 Compute zero order forward mode Taylor coefficients for result of op = AddvvOp.
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_addvv_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 = AddvvOp.
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_addvv_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 // --------------------------- Addpv -----------------------------------------
182 /*!
183 Compute forward mode Taylor coefficients for result of op = AddpvOp.
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 template <class Base>
195 inline void forward_addpv_op(
196  size_t p ,
197  size_t q ,
198  size_t i_z ,
199  const addr_t* arg ,
200  const Base* parameter ,
201  size_t cap_order ,
202  Base* taylor )
203 {
204  // check assumptions
207  CPPAD_ASSERT_UNKNOWN( q < cap_order );
208  CPPAD_ASSERT_UNKNOWN( p <= q );
209 
210  // Taylor coefficients corresponding to arguments and result
211  Base* y = taylor + arg[1] * cap_order;
212  Base* z = taylor + i_z * cap_order;
213 
214  if( p == 0 )
215  { // Paraemter value
216  Base x = parameter[ arg[0] ];
217  z[0] = x + y[0];
218  p++;
219  }
220  for(size_t j = p; j <= q; j++)
221  z[j] = y[j];
222 }
223 /*!
224 Multiple directions forward mode Taylor coefficients for op = AddpvOp.
225 
226 The C++ source code corresponding to this operation is
227 \verbatim
228  z = x + y
229 \endverbatim
230 In the documentation below,
231 this operations is for the case where x is a parameter and y is a variable.
232 
233 \copydetails CppAD::local::forward_binary_op_dir
234 */
235 template <class Base>
237  size_t q ,
238  size_t r ,
239  size_t i_z ,
240  const addr_t* arg ,
241  const Base* parameter ,
242  size_t cap_order ,
243  Base* taylor )
244 {
245  // check assumptions
248  CPPAD_ASSERT_UNKNOWN( 0 < q );
249  CPPAD_ASSERT_UNKNOWN( q < cap_order );
250 
251  // Taylor coefficients corresponding to arguments and result
252  size_t num_taylor_per_var = (cap_order-1) * r + 1;
253  size_t m = (q-1) * r + 1;
254  Base* y = taylor + arg[1] * num_taylor_per_var + m;
255  Base* z = taylor + i_z * num_taylor_per_var + m;
256 
257  for(size_t ell = 0; ell < r; ell++)
258  z[ell] = y[ell];
259 }
260 /*!
261 Compute zero order forward mode Taylor coefficient for result of op = AddpvOp.
262 
263 The C++ source code corresponding to this operation is
264 \verbatim
265  z = x + y
266 \endverbatim
267 In the documentation below,
268 this operations is for the case where x is a parameter and y is a variable.
269 
270 \copydetails CppAD::local::forward_binary_op_0
271 */
272 
273 template <class Base>
274 inline void forward_addpv_op_0(
275  size_t i_z ,
276  const addr_t* arg ,
277  const Base* parameter ,
278  size_t cap_order ,
279  Base* taylor )
280 {
281  // check assumptions
284 
285  // Paraemter value
286  Base x = parameter[ arg[0] ];
287 
288  // Taylor coefficients corresponding to arguments and result
289  Base* y = taylor + arg[1] * cap_order;
290  Base* z = taylor + i_z * cap_order;
291 
292  z[0] = x + y[0];
293 }
294 
295 /*!
296 Compute reverse mode partial derivative for result of op = AddpvOp.
297 
298 The C++ source code corresponding to this operation is
299 \verbatim
300  z = x + y
301 \endverbatim
302 In the documentation below,
303 this operations is for the case where x is a parameter and y is a variable.
304 
305 \copydetails CppAD::local::reverse_binary_op
306 */
307 
308 template <class Base>
309 inline void reverse_addpv_op(
310  size_t d ,
311  size_t i_z ,
312  const addr_t* arg ,
313  const Base* parameter ,
314  size_t cap_order ,
315  const Base* taylor ,
316  size_t nc_partial ,
317  Base* partial )
318 {
319  // check assumptions
322  CPPAD_ASSERT_UNKNOWN( d < cap_order );
323  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
324 
325  // Partial derivatives corresponding to arguments and result
326  Base* py = partial + arg[1] * nc_partial;
327  Base* pz = partial + i_z * nc_partial;
328 
329  // number of indices to access
330  size_t i = d + 1;
331  while(i)
332  { --i;
333  py[i] += pz[i];
334  }
335 }
336 
337 
338 } } // END_CPPAD_LOCAL_NAMESPACE
339 # endif
CPPAD_TAPE_ADDR_TYPE addr_t
Definition: declare_ad.hpp:44
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
void forward_addpv_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 = AddpvOp.
Definition: add_op.hpp:236
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
void reverse_addpv_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 = AddpvOp.
Definition: add_op.hpp:309
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
void forward_addvv_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 = AddvvOp.
Definition: add_op.hpp:38
void forward_addvv_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 = AddvvOp.
Definition: add_op.hpp:117
void reverse_addvv_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 = AddvvOp.
Definition: add_op.hpp:151
void forward_addpv_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 = AddpvOp.
Definition: add_op.hpp:195
void forward_addpv_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 = AddpvOp.
Definition: add_op.hpp:274
void forward_addvv_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 = AddvvOp.
Definition: add_op.hpp:76