source: trunk/cppad/local/abs_op.hpp @ 3301

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

merge in multiple forward direcitons from branches/forward_dir

  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1/* $Id: abs_op.hpp 3301 2014-05-24 05:20:21Z 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( i_x < i_z );
46        CPPAD_ASSERT_UNKNOWN( q < cap_order );
47        CPPAD_ASSERT_UNKNOWN( p <= q );
48
49        // Taylor coefficients corresponding to argument and result
50        Base* x = taylor + i_x * cap_order;
51        Base* z = taylor + i_z * cap_order;
52
53        for(size_t j = p; j <= q; j++)
54                z[j] = sign(x[0]) * x[j];
55}
56
57/*!
58Multiple directions forward mode Taylor coefficient for op = AbsOp.
59
60The C++ source code corresponding to this operation is
61\verbatim
62        z = abs(x)
63\endverbatim
64
65\copydetails forward_unary1_op_dir
66*/
67template <class Base>
68inline void forward_abs_op_dir(
69        size_t q           ,
70        size_t r           ,
71        size_t i_z         ,
72        size_t i_x         ,
73        size_t cap_order   , 
74        Base*  taylor      )
75{
76        // check assumptions
77        CPPAD_ASSERT_UNKNOWN( NumArg(AbsOp) == 1 );
78        CPPAD_ASSERT_UNKNOWN( NumRes(AbsOp) == 1 );
79        CPPAD_ASSERT_UNKNOWN( i_x < i_z );
80        CPPAD_ASSERT_UNKNOWN( 0 < q );
81        CPPAD_ASSERT_UNKNOWN( q < cap_order );
82
83        // Taylor coefficients corresponding to argument and result
84        size_t num_taylor_per_var = (cap_order-1) * r + 1;
85        Base* x = taylor + i_x * num_taylor_per_var;
86        Base* z = taylor + i_z * num_taylor_per_var;
87
88        size_t m = (q-1) * r + 1;
89        for(size_t ell = 0; ell < r; ell++)
90                z[m + ell] = sign(x[0]) * x[m + ell];
91}
92
93/*!
94Compute zero order forward mode Taylor coefficient for result of op = AbsOp.
95
96The C++ source code corresponding to this operation is
97\verbatim
98        z = abs(x)
99\endverbatim
100
101\copydetails forward_unary1_op_0
102*/
103template <class Base>
104inline void forward_abs_op_0(
105        size_t i_z         ,
106        size_t i_x         ,
107        size_t cap_order   , 
108        Base*  taylor      )
109{
110
111        // check assumptions
112        CPPAD_ASSERT_UNKNOWN( NumArg(AbsOp) == 1 );
113        CPPAD_ASSERT_UNKNOWN( NumRes(AbsOp) == 1 );
114        CPPAD_ASSERT_UNKNOWN( i_x < i_z );
115        CPPAD_ASSERT_UNKNOWN( 0 < cap_order );
116
117        // Taylor coefficients corresponding to argument and result
118        Base x0 = *(taylor + i_x * cap_order);
119        Base* z = taylor + i_z * cap_order;
120
121        z[0] = abs(x0);
122}
123/*!
124Compute reverse mode partial derivatives for result of op = AbsOp.
125
126The C++ source code corresponding to this operation is
127\verbatim
128        z = abs(x)
129\endverbatim
130
131\copydetails reverse_unary1_op
132*/
133
134template <class Base>
135inline void reverse_abs_op(
136        size_t      d            ,
137        size_t      i_z          ,
138        size_t      i_x          ,
139        size_t      cap_order    , 
140        const Base* taylor       ,
141        size_t      nc_partial   ,
142        Base*       partial      )
143{       size_t j;       
144
145        // check assumptions
146        CPPAD_ASSERT_UNKNOWN( NumArg(AbsOp) == 1 );
147        CPPAD_ASSERT_UNKNOWN( NumRes(AbsOp) == 1 );
148        CPPAD_ASSERT_UNKNOWN( i_x < i_z );
149        CPPAD_ASSERT_UNKNOWN( d < cap_order );
150        CPPAD_ASSERT_UNKNOWN( d < nc_partial );
151
152        // Taylor coefficients and partials corresponding to argument
153        const Base* x  = taylor  + i_x * cap_order;
154        Base* px       = partial + i_x * nc_partial;
155
156        // Taylor coefficients and partials corresponding to result
157        Base* pz       = partial +    i_z * nc_partial;
158
159        for(j = 0; j <= d; j++)
160                px[j] += sign(x[0]) * pz[j];
161}
162
163} // END_CPPAD_NAMESPACE
164# endif
Note: See TracBrowser for help on using the repository browser.