CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mul_op.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_LOCAL_MUL_OP_HPP
2 # define CPPAD_LOCAL_MUL_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 namespace CppAD { namespace local { // BEGIN_CPPAD_LOCAL_NAMESPACE
16 /*!
17 \file mul_op.hpp
18 Forward and reverse mode calculations for z = x * y.
19 */
20 
21 // --------------------------- Mulvv -----------------------------------------
22 /*!
23 Compute forward mode Taylor coefficients for result of op = MulvvOp.
24 
25 The C++ source code corresponding to this operation is
26 \verbatim
27  z = x * y
28 \endverbatim
29 In the documentation below,
30 this operations is for the case where both x and y are variables
31 and the argument \a parameter is not used.
32 
33 \copydetails CppAD::local::forward_binary_op
34 */
35 
36 template <class Base>
37 inline void forward_mulvv_op(
38  size_t p ,
39  size_t q ,
40  size_t i_z ,
41  const addr_t* arg ,
42  const Base* parameter ,
43  size_t cap_order ,
44  Base* taylor )
45 {
46  // check assumptions
49  CPPAD_ASSERT_UNKNOWN( q < cap_order );
50  CPPAD_ASSERT_UNKNOWN( p <= q );
51 
52  // Taylor coefficients corresponding to arguments and result
53  Base* x = taylor + arg[0] * cap_order;
54  Base* y = taylor + arg[1] * cap_order;
55  Base* z = taylor + i_z * cap_order;
56 
57  size_t k;
58  for(size_t d = p; d <= q; d++)
59  { z[d] = Base(0.0);
60  for(k = 0; k <= d; k++)
61  z[d] += x[d-k] * y[k];
62  }
63 }
64 /*!
65 Multiple directions forward mode Taylor coefficients for op = MulvvOp.
66 
67 The C++ source code corresponding to this operation is
68 \verbatim
69  z = x * y
70 \endverbatim
71 In the documentation below,
72 this operations is for the case where both x and y are variables
73 and the argument \a parameter is not used.
74 
75 \copydetails CppAD::local::forward_binary_op_dir
76 */
77 
78 template <class Base>
80  size_t q ,
81  size_t r ,
82  size_t i_z ,
83  const addr_t* arg ,
84  const Base* parameter ,
85  size_t cap_order ,
86  Base* taylor )
87 {
88  // check assumptions
91  CPPAD_ASSERT_UNKNOWN( 0 < q );
92  CPPAD_ASSERT_UNKNOWN( q < cap_order );
93 
94  // Taylor coefficients corresponding to arguments and result
95  size_t num_taylor_per_var = (cap_order-1) * r + 1;
96  Base* x = taylor + arg[0] * num_taylor_per_var;
97  Base* y = taylor + arg[1] * num_taylor_per_var;
98  Base* z = taylor + i_z * num_taylor_per_var;
99 
100  size_t k, ell, m;
101  for(ell = 0; ell < r; ell++)
102  { m = (q-1)*r + ell + 1;
103  z[m] = x[0] * y[m] + x[m] * y[0];
104  for(k = 1; k < q; k++)
105  z[m] += x[(q-k-1)*r + ell + 1] * y[(k-1)*r + ell + 1];
106  }
107 }
108 
109 /*!
110 Compute zero order forward mode Taylor coefficients for result of op = MulvvOp.
111 
112 The C++ source code corresponding to this operation is
113 \verbatim
114  z = x * y
115 \endverbatim
116 In the documentation below,
117 this operations is for the case where both x and y are variables
118 and the argument \a parameter is not used.
119 
120 \copydetails CppAD::local::forward_binary_op_0
121 */
122 
123 template <class Base>
124 inline void forward_mulvv_op_0(
125  size_t i_z ,
126  const addr_t* arg ,
127  const Base* parameter ,
128  size_t cap_order ,
129  Base* taylor )
130 {
131  // check assumptions
134 
135  // Taylor coefficients corresponding to arguments and result
136  Base* x = taylor + arg[0] * cap_order;
137  Base* y = taylor + arg[1] * cap_order;
138  Base* z = taylor + i_z * cap_order;
139 
140  z[0] = x[0] * y[0];
141 }
142 
143 /*!
144 Compute reverse mode partial derivatives for result of op = MulvvOp.
145 
146 The C++ source code corresponding to this operation is
147 \verbatim
148  z = x * y
149 \endverbatim
150 In the documentation below,
151 this operations is for the case where both x and y are variables
152 and the argument \a parameter is not used.
153 
154 \copydetails CppAD::local::reverse_binary_op
155 */
156 
157 template <class Base>
158 inline void reverse_mulvv_op(
159  size_t d ,
160  size_t i_z ,
161  const addr_t* arg ,
162  const Base* parameter ,
163  size_t cap_order ,
164  const Base* taylor ,
165  size_t nc_partial ,
166  Base* partial )
167 {
168  // check assumptions
171  CPPAD_ASSERT_UNKNOWN( d < cap_order );
172  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
173 
174  // Arguments
175  const Base* x = taylor + arg[0] * cap_order;
176  const Base* y = taylor + arg[1] * cap_order;
177 
178  // Partial derivatives corresponding to arguments and result
179  Base* px = partial + arg[0] * nc_partial;
180  Base* py = partial + arg[1] * nc_partial;
181  Base* pz = partial + i_z * nc_partial;
182 
183 
184  // number of indices to access
185  size_t j = d + 1;
186  size_t k;
187  while(j)
188  { --j;
189  for(k = 0; k <= j; k++)
190  {
191  px[j-k] += azmul(pz[j], y[k]);
192  py[k] += azmul(pz[j], x[j-k]);
193  }
194  }
195 }
196 // --------------------------- Mulpv -----------------------------------------
197 /*!
198 Compute forward mode Taylor coefficients for result of op = MulpvOp.
199 
200 The C++ source code corresponding to this operation is
201 \verbatim
202  z = x * y
203 \endverbatim
204 In the documentation below,
205 this operations is for the case where x is a parameter and y is a variable.
206 
207 \copydetails CppAD::local::forward_binary_op
208 */
209 
210 template <class Base>
211 inline void forward_mulpv_op(
212  size_t p ,
213  size_t q ,
214  size_t i_z ,
215  const addr_t* arg ,
216  const Base* parameter ,
217  size_t cap_order ,
218  Base* taylor )
219 {
220  // check assumptions
223  CPPAD_ASSERT_UNKNOWN( q < cap_order );
224  CPPAD_ASSERT_UNKNOWN( p <= q );
225 
226  // Taylor coefficients corresponding to arguments and result
227  Base* y = taylor + arg[1] * cap_order;
228  Base* z = taylor + i_z * cap_order;
229 
230  // Paraemter value
231  Base x = parameter[ arg[0] ];
232 
233  for(size_t d = p; d <= q; d++)
234  z[d] = x * y[d];
235 }
236 /*!
237 Multiple directions forward mode Taylor coefficients for op = MulpvOp.
238 
239 The C++ source code corresponding to this operation is
240 \verbatim
241  z = x * y
242 \endverbatim
243 In the documentation below,
244 this operations is for the case where x is a parameter and y is a variable.
245 
246 \copydetails CppAD::local::forward_binary_op_dir
247 */
248 
249 template <class Base>
251  size_t q ,
252  size_t r ,
253  size_t i_z ,
254  const addr_t* arg ,
255  const Base* parameter ,
256  size_t cap_order ,
257  Base* taylor )
258 {
259  // check assumptions
262  CPPAD_ASSERT_UNKNOWN( 0 < q );
263  CPPAD_ASSERT_UNKNOWN( q < cap_order );
264 
265  // Taylor coefficients corresponding to arguments and result
266  size_t num_taylor_per_var = (cap_order-1) * r + 1;
267  size_t m = (q-1) * r + 1;
268  Base* y = taylor + arg[1] * num_taylor_per_var + m;
269  Base* z = taylor + i_z * num_taylor_per_var + m;
270 
271  // Paraemter value
272  Base x = parameter[ arg[0] ];
273 
274  for(size_t ell = 0; ell < r; ell++)
275  z[ell] = x * y[ell];
276 }
277 /*!
278 Compute zero order forward mode Taylor coefficient for result of op = MulpvOp.
279 
280 The C++ source code corresponding to this operation is
281 \verbatim
282  z = x * y
283 \endverbatim
284 In the documentation below,
285 this operations is for the case where x is a parameter and y is a variable.
286 
287 \copydetails CppAD::local::forward_binary_op_0
288 */
289 
290 template <class Base>
291 inline void forward_mulpv_op_0(
292  size_t i_z ,
293  const addr_t* arg ,
294  const Base* parameter ,
295  size_t cap_order ,
296  Base* taylor )
297 {
298  // check assumptions
301 
302  // Paraemter value
303  Base x = parameter[ arg[0] ];
304 
305  // Taylor coefficients corresponding to arguments and result
306  Base* y = taylor + arg[1] * cap_order;
307  Base* z = taylor + i_z * cap_order;
308 
309  z[0] = x * y[0];
310 }
311 
312 /*!
313 Compute reverse mode partial derivative for result of op = MulpvOp.
314 
315 The C++ source code corresponding to this operation is
316 \verbatim
317  z = x * y
318 \endverbatim
319 In the documentation below,
320 this operations is for the case where x is a parameter and y is a variable.
321 
322 \copydetails CppAD::local::reverse_binary_op
323 */
324 
325 template <class Base>
326 inline void reverse_mulpv_op(
327  size_t d ,
328  size_t i_z ,
329  const addr_t* arg ,
330  const Base* parameter ,
331  size_t cap_order ,
332  const Base* taylor ,
333  size_t nc_partial ,
334  Base* partial )
335 {
336  // check assumptions
339  CPPAD_ASSERT_UNKNOWN( d < cap_order );
340  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
341 
342  // Arguments
343  Base x = parameter[ arg[0] ];
344 
345  // Partial derivatives corresponding to arguments and result
346  Base* py = partial + arg[1] * nc_partial;
347  Base* pz = partial + i_z * nc_partial;
348 
349  // number of indices to access
350  size_t j = d + 1;
351  while(j)
352  { --j;
353  py[j] += azmul(pz[j], x);
354  }
355 }
356 
357 
358 } } // END_CPPAD_LOCAL_NAMESPACE
359 # endif
AD< Base > azmul(const AD< Base > &x, const AD< Base > &y)
Definition: azmul.hpp:94
void reverse_mulvv_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 = MulvvOp.
Definition: mul_op.hpp:158
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 reverse_mulpv_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 = MulpvOp.
Definition: mul_op.hpp:326
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
void forward_mulpv_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 = MulpvOp.
Definition: mul_op.hpp:291
void forward_mulvv_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 = MulvvOp.
Definition: mul_op.hpp:79
void forward_mulvv_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 = MulvvOp.
Definition: mul_op.hpp:37
void forward_mulpv_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 = MulpvOp.
Definition: mul_op.hpp:211
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
void forward_mulpv_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 = MulpvOp.
Definition: mul_op.hpp:250
void forward_mulvv_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 = MulvvOp.
Definition: mul_op.hpp:124