CppAD: A C++ Algorithmic Differentiation Package  20171217
abs_op.hpp
Go to the documentation of this file.
3
4 /* --------------------------------------------------------------------------
6
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.
13 -------------------------------------------------------------------------- */
14
15
17 /*!
18 \file abs_op.hpp
19 Forward and reverse mode calculations for z = fabs(x).
20 */
21
22 /*!
23 Compute forward mode Taylor coefficient for result of op = AbsOp.
24
25 The C++ source code corresponding to this operation is
26 \verbatim
27  z = fabs(x)
28 \endverbatim
29
31 */
32 template <class Base>
33 inline void forward_abs_op(
34  size_t p ,
35  size_t q ,
36  size_t i_z ,
37  size_t i_x ,
38  size_t cap_order ,
39  Base* taylor )
40 {
41  // check assumptions
44  CPPAD_ASSERT_UNKNOWN( q < cap_order );
45  CPPAD_ASSERT_UNKNOWN( p <= q );
46
47  // Taylor coefficients corresponding to argument and result
48  Base* x = taylor + i_x * cap_order;
49  Base* z = taylor + i_z * cap_order;
50
51  for(size_t j = p; j <= q; j++)
52  z[j] = sign(x[0]) * x[j];
53 }
54
55 /*!
56 Multiple directions forward mode Taylor coefficient for op = AbsOp.
57
58 The C++ source code corresponding to this operation is
59 \verbatim
60  z = fabs(x)
61 \endverbatim
62
64 */
65 template <class Base>
66 inline void forward_abs_op_dir(
67  size_t q ,
68  size_t r ,
69  size_t i_z ,
70  size_t i_x ,
71  size_t cap_order ,
72  Base* taylor )
73 {
74  // check assumptions
77  CPPAD_ASSERT_UNKNOWN( 0 < q );
78  CPPAD_ASSERT_UNKNOWN( q < cap_order );
79
80  // Taylor coefficients corresponding to argument and result
81  size_t num_taylor_per_var = (cap_order-1) * r + 1;
82  Base* x = taylor + i_x * num_taylor_per_var;
83  Base* z = taylor + i_z * num_taylor_per_var;
84
85  size_t m = (q-1) * r + 1;
86  for(size_t ell = 0; ell < r; ell++)
87  z[m + ell] = sign(x[0]) * x[m + ell];
88 }
89
90 /*!
91 Compute zero order forward mode Taylor coefficient for result of op = AbsOp.
92
93 The C++ source code corresponding to this operation is
94 \verbatim
95  z = fabs(x)
96 \endverbatim
97
99 */
100 template <class Base>
101 inline void forward_abs_op_0(
102  size_t i_z ,
103  size_t i_x ,
104  size_t cap_order ,
105  Base* taylor )
106 {
107
108  // check assumptions
111  CPPAD_ASSERT_UNKNOWN( 0 < cap_order );
112
113  // Taylor coefficients corresponding to argument and result
114  Base x0 = *(taylor + i_x * cap_order);
115  Base* z = taylor + i_z * cap_order;
116
117  z[0] = fabs(x0);
118 }
119 /*!
120 Compute reverse mode partial derivatives for result of op = AbsOp.
121
122 The C++ source code corresponding to this operation is
123 \verbatim
124  z = fabs(x)
125 \endverbatim
126
128 */
129
130 template <class Base>
131 inline void reverse_abs_op(
132  size_t d ,
133  size_t i_z ,
134  size_t i_x ,
135  size_t cap_order ,
136  const Base* taylor ,
137  size_t nc_partial ,
138  Base* partial )
139 { size_t j;
140
141  // check assumptions
144  CPPAD_ASSERT_UNKNOWN( d < cap_order );
145  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
146
147  // Taylor coefficients and partials corresponding to argument
148  const Base* x = taylor + i_x * cap_order;
149  Base* px = partial + i_x * nc_partial;
150
151  // Taylor coefficients and partials corresponding to result
152  Base* pz = partial + i_z * nc_partial;
153
154  // do not need azmul because sign is either +1, -1, or zero
155  for(j = 0; j <= d; j++)
156  px[j] += sign(x[0]) * pz[j];
157 }
158
160 # endif
void forward_abs_op(size_t p, size_t q, size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Compute forward mode Taylor coefficient for result of op = AbsOp.
Definition: abs_op.hpp:33
void forward_abs_op_0(size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Compute zero order forward mode Taylor coefficient for result of op = AbsOp.
Definition: abs_op.hpp:101
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
void forward_abs_op_dir(size_t q, size_t r, size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Multiple directions forward mode Taylor coefficient for op = AbsOp.
Definition: abs_op.hpp:66