source: branches/cache/cppad/local/abs_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: 4.0 KB
Line 
1/* $Id: abs_op.hpp 3324 2014-09-12 12:14:53Z bradbell $ */
2# ifndef CPPAD_ABS_OP_INCLUDED
3# define CPPAD_ABS_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
16
17namespace CppAD { // BEGIN_CPPAD_NAMESPACE
18/*!
19\file abs_op.hpp
20Forward and reverse mode calculations for z = abs(x).
21*/
22
23/*!
24Compute forward mode Taylor coefficient for result of op = AbsOp.
25
26The C++ source code corresponding to this operation is
27\verbatim
28        z = abs(x)
29\endverbatim
30
31\copydetails forward_unary1_op
32*/
33template <class Base>
34inline void forward_abs_op(
35        size_t p           ,
36        size_t q           ,
37        size_t i_z         ,
38        size_t i_x         ,
39        size_t cap_order   , 
40        Base*  taylor      )
41{
42        // check assumptions
43        CPPAD_ASSERT_UNKNOWN( NumArg(AbsOp) == 1 );
44        CPPAD_ASSERT_UNKNOWN( NumRes(AbsOp) == 1 );
45        CPPAD_ASSERT_UNKNOWN( q < cap_order );
46        CPPAD_ASSERT_UNKNOWN( p <= q );
47
48        // Taylor coefficients corresponding to argument and result
49        Base* x = taylor + i_x * cap_order;
50        Base* z = taylor + i_z * cap_order;
51
52        for(size_t j = p; j <= q; j++)
53                z[j] = sign(x[0]) * x[j];
54}
55
56/*!
57Multiple directions forward mode Taylor coefficient for op = AbsOp.
58
59The C++ source code corresponding to this operation is
60\verbatim
61        z = abs(x)
62\endverbatim
63
64\copydetails forward_unary1_op_dir
65*/
66template <class Base>
67inline void forward_abs_op_dir(
68        size_t q           ,
69        size_t r           ,
70        size_t i_z         ,
71        size_t i_x         ,
72        size_t cap_order   , 
73        Base*  taylor      )
74{
75        // check assumptions
76        CPPAD_ASSERT_UNKNOWN( NumArg(AbsOp) == 1 );
77        CPPAD_ASSERT_UNKNOWN( NumRes(AbsOp) == 1 );
78        CPPAD_ASSERT_UNKNOWN( 0 < q );
79        CPPAD_ASSERT_UNKNOWN( q < cap_order );
80
81        // Taylor coefficients corresponding to argument and result
82        size_t num_taylor_per_var = (cap_order-1) * r + 1;
83        Base* x = taylor + i_x * num_taylor_per_var;
84        Base* z = taylor + i_z * num_taylor_per_var;
85
86        size_t m = (q-1) * r + 1;
87        for(size_t ell = 0; ell < r; ell++)
88                z[m + ell] = sign(x[0]) * x[m + ell];
89}
90
91/*!
92Compute zero order forward mode Taylor coefficient for result of op = AbsOp.
93
94The C++ source code corresponding to this operation is
95\verbatim
96        z = abs(x)
97\endverbatim
98
99\copydetails forward_unary1_op_0
100*/
101template <class Base>
102inline void forward_abs_op_0(
103        size_t i_z         ,
104        size_t i_x         ,
105        size_t cap_order   , 
106        Base*  taylor      )
107{
108
109        // check assumptions
110        CPPAD_ASSERT_UNKNOWN( NumArg(AbsOp) == 1 );
111        CPPAD_ASSERT_UNKNOWN( NumRes(AbsOp) == 1 );
112        CPPAD_ASSERT_UNKNOWN( 0 < cap_order );
113
114        // Taylor coefficients corresponding to argument and result
115        Base x0 = *(taylor + i_x * cap_order);
116        Base* z = taylor + i_z * cap_order;
117
118        z[0] = abs(x0);
119}
120/*!
121Compute reverse mode partial derivatives for result of op = AbsOp.
122
123The C++ source code corresponding to this operation is
124\verbatim
125        z = abs(x)
126\endverbatim
127
128\copydetails reverse_unary1_op
129*/
130
131template <class Base>
132inline void reverse_abs_op(
133        size_t      d            ,
134        size_t      i_z          ,
135        size_t      i_x          ,
136        size_t      cap_order    , 
137        const Base* taylor       ,
138        size_t      nc_partial   ,
139        Base*       partial      )
140{       size_t j;       
141
142        // check assumptions
143        CPPAD_ASSERT_UNKNOWN( NumArg(AbsOp) == 1 );
144        CPPAD_ASSERT_UNKNOWN( NumRes(AbsOp) == 1 );
145        CPPAD_ASSERT_UNKNOWN( d < cap_order );
146        CPPAD_ASSERT_UNKNOWN( d < nc_partial );
147
148        // Taylor coefficients and partials corresponding to argument
149        const Base* x  = taylor  + i_x * cap_order;
150        Base* px       = partial + i_x * nc_partial;
151
152        // Taylor coefficients and partials corresponding to result
153        Base* pz       = partial +    i_z * nc_partial;
154
155        for(j = 0; j <= d; j++)
156                px[j] += sign(x[0]) * pz[j];
157}
158
159} // END_CPPAD_NAMESPACE
160# endif
Note: See TracBrowser for help on using the repository browser.