CppAD: A C++ Algorithmic Differentiation Package  20171217
pow.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 /*
16 $begin pow$$17 spell 18 Vec 19 std 20 namespace 21 CppAD 22 const 23$$ 24 25 26$section The AD Power Function$$27 mindex pow exponent$$
28
29 $head Syntax$$30 icode%z% = pow(%x%, %y%)%$$ 31 32$head See Also$$33 cref pow_int$$
34
35
36 $head Purpose$$37 Determines the value of the power function which is defined by 38 latex $39 {\rm pow} (x, y) = x^y 40$$$ 41 This version of the$code pow$$function may use 42 logarithms and exponentiation to compute derivatives. 43 This will not work if icode x$$ is less than or equal zero.
44 If the value of $icode y$$is an integer, 45 the cref pow_int$$ function is used to compute this value 46 using only multiplication (and division if$icode y$$is negative). 47 (This will work even if icode x$$ is less than or equal zero.)
48
49 $head x$$50 The argument icode x$$ has one of the following prototypes 51$codei%
52  const %Base%& %x%
55 %$$56 57 head y$$
58 The argument $icode y$$has one of the following prototypes 59 codei% 60 const %Base%& %y% 61 const AD<%Base%>& %y% 62 const VecAD<%Base%>::reference& %y% 63 %$$ 64 65$head z$$66 If both icode x$$ and $icode y$$are icode Base$$ objects, 67 the result$icode z$$is also a icode Base$$ object.
68 Otherwise, it has prototype
69 $codei% 70 AD<%Base%> %z% 71 %$$72 73 head Operation Sequence$$ 74 This is an AD of$icode Base$$75 cref/atomic operation/glossary/Operation/Atomic/$$
76 and hence is part of the current
77 AD of $icode Base$$78 cref/operation sequence/glossary/Operation/Sequence/$$. 79 80$head Example$$81 children% 82 example/general/pow.cpp 83 %$$
84 The file
85 $cref pow.cpp$$86 is an examples and tests of this function. 87 It returns true if it succeeds and false otherwise. 88 89$end
90 -------------------------------------------------------------------------------
91 */
92
95
96 // case where x and y are AD<Base> -----------------------------------------
99 {
100  // compute the Base part
102  result.value_ = pow(x.value_, y.value_);
104
105  // check if there is a recording in progress
107  if( tape == CPPAD_NULL )
108  return result;
109  tape_id_t tape_id = tape->id_;
110
111  // tape_id cannot match the default value for tape_id_; i.e., 0
112  CPPAD_ASSERT_UNKNOWN( tape_id > 0 );
113  bool var_x = x.tape_id_ == tape_id;
114  bool var_y = y.tape_id_ == tape_id;
115
116  if( var_x )
117  { if( var_y )
118  { // result = variable^variable
121
122  // put operand addresses in tape
124
125  // put operator in the tape
127
128  // make result a variable
129  result.tape_id_ = tape_id;
130  }
131  else if( IdenticalZero( y.value_ ) )
132  { // result = variable^0
133  }
134  else
135  { // result = variable^parameter
138
139  // put operand addresses in tape
142
143  // put operator in the tape
145
146  // make result a variable
147  result.tape_id_ = tape_id;
148  }
149  }
150  else if( var_y )
151  { if( IdenticalZero(x.value_) )
152  { // result = 0^variable
153  }
154  else
155  { // result = parameter^variable
158
159  // put operand addresses in tape
162
163  // put operator in the tape
165
166  // make result a variable
167  result.tape_id_ = tape_id;
168  }
169  }
170  return result;
171 }
172 // =========================================================================
174 // -------------------------------------------------------------------------
176
179 { return pow(x, y.ADBase()); }
180
184
187 { return pow(x.ADBase(), y); }
188 // -------------------------------------------------------------------------
189 // Operations with Base
190
192 pow(const Base& x, const AD<Base>& y)
193 { return pow(AD<Base>(x), y); }
194
196 pow(const Base& x, const VecAD_reference<Base>& y)
198
200 pow(const AD<Base>& x, const Base& y)
201 { return pow(x, AD<Base>(y)); }
202
204 pow(const VecAD_reference<Base>& x, const Base& y)
206 // -------------------------------------------------------------------------
207 // Operations with double
208
210 pow(const double& x, const AD<Base>& y)
211 { return pow(AD<Base>(x), y); }
212
214 pow(const double& x, const VecAD_reference<Base>& y)
216
218 pow(const AD<Base>& x, const double& y)
219 { return pow(x, AD<Base>(y)); }
220
222 pow(const VecAD_reference<Base>& x, const double& y)
224 // -------------------------------------------------------------------------
225 // Special case to avoid ambuigity when Base is double
226
228 pow(const double& x, const AD<double>& y)
229 { return pow(AD<double>(x), y); }
230
232 pow(const double& x, const VecAD_reference<double>& y)
234
236 pow(const AD<double>& x, const double& y)
237 { return pow(x, AD<double>(y)); }
238
240 pow(const VecAD_reference<double>& x, const double& y)
242
243 // =========================================================================
244 // Fold operations for the cases where x is an int,
245 // but let cppad/utility/pow_int.hpp handle the cases where y is an int.
246 // -------------------------------------------------------------------------
247 template <class Base> AD<Base> pow
248 (const int& x, const VecAD_reference<Base>& y)
250
251 template <class Base> AD<Base> pow
252 (const int& x, const AD<Base>& y)
253 { return pow(AD<Base>(x), y); }
254
255 } // END CppAD namespace
256
257 # endif
Base value_
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
bool IdenticalZero(const std::complex< double > &x)
Type pow(const Type &x, const int &n)
Definition: pow_int.hpp:116
static local::ADTape< Base > * tape_ptr(void)
Pointer for the tape for this AD&lt;Base&gt; class and the current thread.
Check that exp is true, if not terminate execution.
local::recorder< Base > Rec_
This is where the information is recorded.
tape_id_t tape_id_