CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
exp_op.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_LOCAL_EXP_OP_HPP
2 # define CPPAD_LOCAL_EXP_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 
16 namespace CppAD { namespace local { // BEGIN_CPPAD_LOCAL_NAMESPACE
17 /*!
18 \file exp_op.hpp
19 Forward and reverse mode calculations for z = exp(x).
20 */
21 
22 
23 /*!
24 Forward mode Taylor coefficient for result of op = ExpOp.
25 
26 The C++ source code corresponding to this operation is
27 \verbatim
28  z = exp(x)
29 \endverbatim
30 
31 \copydetails CppAD::local::forward_unary1_op
32 */
33 template <class Base>
34 inline void forward_exp_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
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  size_t k;
53  if( p == 0 )
54  { z[0] = exp( x[0] );
55  p++;
56  }
57  for(size_t j = p; j <= q; j++)
58  {
59  z[j] = x[1] * z[j-1];
60  for(k = 2; k <= j; k++)
61  z[j] += Base(double(k)) * x[k] * z[j-k];
62  z[j] /= Base(double(j));
63  }
64 }
65 
66 
67 /*!
68 Multiple direction forward mode Taylor coefficient for op = ExpOp.
69 
70 The C++ source code corresponding to this operation is
71 \verbatim
72  z = exp(x)
73 \endverbatim
74 
75 \copydetails CppAD::local::forward_unary1_op_dir
76 */
77 template <class Base>
78 inline void forward_exp_op_dir(
79  size_t q ,
80  size_t r ,
81  size_t i_z ,
82  size_t i_x ,
83  size_t cap_order ,
84  Base* taylor )
85 {
86  // check assumptions
89  CPPAD_ASSERT_UNKNOWN( q < cap_order );
90  CPPAD_ASSERT_UNKNOWN( 0 < q );
91 
92  // Taylor coefficients corresponding to argument and result
93  size_t num_taylor_per_var = (cap_order-1) * r + 1;
94  Base* x = taylor + i_x * 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] = Base(double(q)) * x[m+ell] * z[0];
100  for(size_t k = 1; k < q; k++)
101  z[m+ell] += Base(double(k)) * x[(k-1)*r+ell+1] * z[(q-k-1)*r+ell+1];
102  z[m+ell] /= Base(double(q));
103  }
104 }
105 
106 /*!
107 Zero order forward mode Taylor coefficient for result of op = ExpOp.
108 
109 The C++ source code corresponding to this operation is
110 \verbatim
111  z = exp(x)
112 \endverbatim
113 
114 \copydetails CppAD::local::forward_unary1_op_0
115 */
116 template <class Base>
117 inline void forward_exp_op_0(
118  size_t i_z ,
119  size_t i_x ,
120  size_t cap_order ,
121  Base* taylor )
122 {
123  // check assumptions
126  CPPAD_ASSERT_UNKNOWN( 0 < cap_order );
127 
128  // Taylor coefficients corresponding to argument and result
129  Base* x = taylor + i_x * cap_order;
130  Base* z = taylor + i_z * cap_order;
131 
132  z[0] = exp( x[0] );
133 }
134 /*!
135 Reverse mode partial derivatives for result of op = ExpOp.
136 
137 The C++ source code corresponding to this operation is
138 \verbatim
139  z = exp(x)
140 \endverbatim
141 
142 \copydetails CppAD::local::reverse_unary1_op
143 */
144 
145 template <class Base>
146 inline void reverse_exp_op(
147  size_t d ,
148  size_t i_z ,
149  size_t i_x ,
150  size_t cap_order ,
151  const Base* taylor ,
152  size_t nc_partial ,
153  Base* partial )
154 {
155  // check assumptions
158  CPPAD_ASSERT_UNKNOWN( d < cap_order );
159  CPPAD_ASSERT_UNKNOWN( d < nc_partial );
160 
161  // Taylor coefficients and partials corresponding to argument
162  const Base* x = taylor + i_x * cap_order;
163  Base* px = partial + i_x * nc_partial;
164 
165  // Taylor coefficients and partials corresponding to result
166  const Base* z = taylor + i_z * cap_order;
167  Base* pz = partial + i_z * nc_partial;
168 
169  // If pz is zero, make sure this operation has no effect
170  // (zero times infinity or nan would be non-zero).
171  bool skip(true);
172  for(size_t i_d = 0; i_d <= d; i_d++)
173  skip &= IdenticalZero(pz[i_d]);
174  if( skip )
175  return;
176 
177  // loop through orders in reverse
178  size_t j, k;
179  j = d;
180  while(j)
181  { // scale partial w.r.t z[j]
182  pz[j] /= Base(double(j));
183 
184  for(k = 1; k <= j; k++)
185  { px[k] += Base(double(k)) * azmul(pz[j], z[j-k]);
186  pz[j-k] += Base(double(k)) * azmul(pz[j], x[k]);
187  }
188  --j;
189  }
190  px[0] += azmul(pz[0], z[0]);
191 }
192 
193 } } // END_CPPAD_LOCAL_NAMESPACE
194 # endif
AD< Base > azmul(const AD< Base > &x, const AD< Base > &y)
Definition: azmul.hpp:94
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
void reverse_exp_op(size_t d, size_t i_z, size_t i_x, size_t cap_order, const Base *taylor, size_t nc_partial, Base *partial)
Reverse mode partial derivatives for result of op = ExpOp.
Definition: exp_op.hpp:146
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
bool IdenticalZero(const std::complex< double > &x)
AD< Base > exp(const AD< Base > &x)
void forward_exp_op_dir(size_t q, size_t r, size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Multiple direction forward mode Taylor coefficient for op = ExpOp.
Definition: exp_op.hpp:78
void forward_exp_op_0(size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Zero order forward mode Taylor coefficient for result of op = ExpOp.
Definition: exp_op.hpp:117
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
void forward_exp_op(size_t p, size_t q, size_t i_z, size_t i_x, size_t cap_order, Base *taylor)
Forward mode Taylor coefficient for result of op = ExpOp.
Definition: exp_op.hpp:34