source: branches/cache/cppad/local/add_op.hpp @ 3324

Last change on this file since 3324 was 3324, checked in by bradbell, 6 years ago

merge trunk changes into cache

  • Property svn:keywords set to Id
File size: 8.9 KB
Line 
1/* $Id: add_op.hpp 3324 2014-09-12 12:14:53Z bradbell $ */
2# ifndef CPPAD_ADD_OP_INCLUDED
3# define CPPAD_ADD_OP_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-14 Bradley M. Bell
7
8CppAD is distributed under multiple licenses. This distribution is under
9the terms of the
10                    Eclipse Public License Version 1.0.
11
12A copy of this license is included in the COPYING file of this distribution.
13Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14-------------------------------------------------------------------------- */
15
16namespace CppAD { // BEGIN_CPPAD_NAMESPACE
17/*!
18\file add_op.hpp
19Forward and reverse mode calculations for z = x + y.
20*/
21
22// --------------------------- Addvv -----------------------------------------
23/*!
24Compute forward mode Taylor coefficients for result of op = AddvvOp.
25
26The C++ source code corresponding to this operation is
27\verbatim
28        z = x + y
29\endverbatim
30In the documentation below,
31this operations is for the case where both x and y are variables
32and the argument \a parameter is not used.
33
34\copydetails forward_binary_op
35*/
36
37template <class Base>
38inline 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
48        CPPAD_ASSERT_UNKNOWN( NumArg(AddvvOp) == 2 );
49        CPPAD_ASSERT_UNKNOWN( NumRes(AddvvOp) == 1 );
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/*!
62Multiple directions forward mode Taylor coefficients for op = AddvvOp.
63
64The C++ source code corresponding to this operation is
65\verbatim
66        z = x + y
67\endverbatim
68In the documentation below,
69this operations is for the case where both x and y are variables
70and the argument \a parameter is not used.
71
72\copydetails forward_binary_op_dir
73*/
74
75template <class Base>
76inline void forward_addvv_op_dir(
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
86        CPPAD_ASSERT_UNKNOWN( NumArg(AddvvOp) == 2 );
87        CPPAD_ASSERT_UNKNOWN( NumRes(AddvvOp) == 1 );
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/*!
103Compute zero order forward mode Taylor coefficients for result of op = AddvvOp.
104
105The C++ source code corresponding to this operation is
106\verbatim
107        z = x + y
108\endverbatim
109In the documentation below,
110this operations is for the case where both x and y are variables
111and the argument \a parameter is not used.
112
113\copydetails forward_binary_op_0
114*/
115
116template <class Base>
117inline 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
125        CPPAD_ASSERT_UNKNOWN( NumArg(AddvvOp) == 2 );
126        CPPAD_ASSERT_UNKNOWN( NumRes(AddvvOp) == 1 );
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/*!
137Compute reverse mode partial derivatives for result of op = AddvvOp.
138
139The C++ source code corresponding to this operation is
140\verbatim
141        z = x + y
142\endverbatim
143In the documentation below,
144this operations is for the case where both x and y are variables
145and the argument \a parameter is not used.
146
147\copydetails reverse_binary_op
148*/
149
150template <class Base>
151inline 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
162        CPPAD_ASSERT_UNKNOWN( NumArg(AddvvOp) == 2 );
163        CPPAD_ASSERT_UNKNOWN( NumRes(AddvvOp) == 1 );
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/*!
183Compute forward mode Taylor coefficients for result of op = AddpvOp.
184
185The C++ source code corresponding to this operation is
186\verbatim
187        z = x + y
188\endverbatim
189In the documentation below,
190this operations is for the case where x is a parameter and y is a variable.
191
192\copydetails forward_binary_op
193*/
194template <class Base>
195inline 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
205        CPPAD_ASSERT_UNKNOWN( NumArg(AddpvOp) == 2 );
206        CPPAD_ASSERT_UNKNOWN( NumRes(AddpvOp) == 1 );
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/*!
224Multiple directions forward mode Taylor coefficients for op = AddpvOp.
225
226The C++ source code corresponding to this operation is
227\verbatim
228        z = x + y
229\endverbatim
230In the documentation below,
231this operations is for the case where x is a parameter and y is a variable.
232
233\copydetails forward_binary_op_dir
234*/
235template <class Base>
236inline void forward_addpv_op_dir(
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
246        CPPAD_ASSERT_UNKNOWN( NumArg(AddpvOp) == 2 );
247        CPPAD_ASSERT_UNKNOWN( NumRes(AddpvOp) == 1 );
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/*!
261Compute zero order forward mode Taylor coefficient for result of op = AddpvOp.
262
263The C++ source code corresponding to this operation is
264\verbatim
265        z = x + y
266\endverbatim
267In the documentation below,
268this operations is for the case where x is a parameter and y is a variable.
269
270\copydetails forward_binary_op_0
271*/
272
273template <class Base>
274inline 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
282        CPPAD_ASSERT_UNKNOWN( NumArg(AddpvOp) == 2 );
283        CPPAD_ASSERT_UNKNOWN( NumRes(AddpvOp) == 1 );
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/*!
296Compute reverse mode partial derivative for result of op = AddpvOp.
297
298The C++ source code corresponding to this operation is
299\verbatim
300        z = x + y
301\endverbatim
302In the documentation below,
303this operations is for the case where x is a parameter and y is a variable.
304
305\copydetails reverse_binary_op
306*/
307
308template <class Base>
309inline 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
320        CPPAD_ASSERT_UNKNOWN( NumArg(AddvvOp) == 2 );
321        CPPAD_ASSERT_UNKNOWN( NumRes(AddvvOp) == 1 );
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_NAMESPACE
339# endif
Note: See TracBrowser for help on using the repository browser.