Last change on this file since 2506 was 2506, checked in by bradbell, 8 years ago

Change Licenses: CPL-1.0 -> EPL-1.0, GPL-2.0->GPL-3.0

• Property svn:keywords set to Id
File size: 7.1 KB
Line
1/* $Id: pow.hpp 2506 2012-10-24 19:36:49Z bradbell$ */
4
5/* --------------------------------------------------------------------------
7
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.
14-------------------------------------------------------------------------- */
15
16/*
17$begin pow$$18spell 19 Vec 20 std 21 namespace 22 CppAD 23 const 24$$ 25 26$index pow, AD$$27index exponent, AD function$$
28
29$section The AD Power Function$$30 31head Syntax$$ 32$icode%z% = pow(%x%, %y%)%$$33 34head See Also$$
35$cref pow_int$$36 37 38head Purpose$$ 39Determines the value of the power function which is defined by 40$latex $41 {\rm pow} (x, y) = x^y 42$ $$43This version of the code pow$$ function may use
44logarithms and exponentiation to compute derivatives.
45This will not work if $icode x$$is less than or equal zero. 46If the value of icode y$$ is an integer, 47the$cref pow_int$$function is used to compute this value 48using only multiplication (and division if icode y$$ is negative).
49(This will work even if $icode x$$is less than or equal zero.) 50 51head x$$ 52The argument$icode x$$has one of the following prototypes 53codei% 54 const %Base%& %x% 55 const AD<%Base%>& %x% 56 const VecAD<%Base%>::reference& %x% 57%$$
58
59$head y$$60The argument icode y$$ has one of the following prototypes 61$codei%
62        const %Base%&                    %y%
65%$$66 67head z$$
68If both $icode x$$and icode y$$ are$icode Base$$objects, 69the result icode z$$ is also a $icode Base$$object. 70Otherwise, it has prototype 71codei% 72 AD<%Base%> %z% 73%$$ 74 75$head Operation Sequence$$76This is an AD of icode Base$$
77$cref/atomic operation/glossary/Operation/Atomic/$$78and hence is part of the current 79AD of icode Base$$ 80$cref/operation sequence/glossary/Operation/Sequence/$$. 81 82head Example$$
83$children% 84 example/pow.cpp 85%$$86The file 87cref pow.cpp$$ 88is an examples and tests of this function. 89It returns true if it succeeds and false otherwise. 90 91$end
92-------------------------------------------------------------------------------
93*/
94
97
98// case where x and y are AD<Base> -----------------------------------------
101{
102        // compute the Base part
104        result.value_  = pow(x.value_, y.value_);
106
107        // check if there is a recording in progress
109        if( tape == CPPAD_NULL )
110                return result;
111        tape_id_t tape_id = tape->id_;
112
113        // tape_id cannot match the default value for tape_id_; i.e., 0
114        CPPAD_ASSERT_UNKNOWN( tape_id > 0 );
115        bool var_x = x.tape_id_ == tape_id;
116        bool var_y = y.tape_id_ == tape_id;
117
118        if( var_x )
119        {       if( var_y )
120                {       // result = variable^variable
121                        CPPAD_ASSERT_UNKNOWN( NumRes(PowvvOp) == 3 );
122                        CPPAD_ASSERT_UNKNOWN( NumArg(PowvvOp) == 2 );
123
124                        // put operand addresses in tape
126
127                        // put operator in the tape
129
130                        // make result a variable
131                        result.tape_id_ = tape_id;
132                }
133                else if( IdenticalZero( y.value_ ) )
134                {       // result = variable^0
135                }
136                else
137                {       // result = variable^parameter
138                        CPPAD_ASSERT_UNKNOWN( NumRes(PowvpOp) == 3 );
139                        CPPAD_ASSERT_UNKNOWN( NumArg(PowvpOp) == 2 );
140
141                        // put operand addresses in tape
144
145                        // put operator in the tape
147
148                        // make result a variable
149                        result.tape_id_ = tape_id;
150                }
151        }
152        else if( var_y )
153        {       if( IdenticalZero(x.value_) )
154                {       // result = 0^variable
155                }
156                else
157                {       // result = variable^parameter
158                        CPPAD_ASSERT_UNKNOWN( NumRes(PowpvOp) == 3 );
159                        CPPAD_ASSERT_UNKNOWN( NumArg(PowpvOp) == 2 );
160
161                        // put operand addresses in tape
164
165                        // put operator in the tape
167
168                        // make result a variable
169                        result.tape_id_ = tape_id;
170                }
171        }
172        return result;
173}
174// =========================================================================
176// -------------------------------------------------------------------------
178
182
186
190// -------------------------------------------------------------------------
191// Operations with Base
192
194pow(const Base& x, const AD<Base>& y)
196
198pow(const Base& x, const VecAD_reference<Base>& y)
200
202pow(const AD<Base>& x, const Base& y)
204
206pow(const VecAD_reference<Base>& x, const Base& y)
208// -------------------------------------------------------------------------
209// Operations with double
210
212pow(const double& x, const AD<Base>& y)
214
216pow(const double& x, const VecAD_reference<Base>& y)
218
220pow(const AD<Base>& x, const double& y)
222
224pow(const VecAD_reference<Base>& x, const double& y)
226// -------------------------------------------------------------------------
227// Special case to avoid ambuigity when Base is double
228
230pow(const double& x, const AD<double>& y)
232
234pow(const double& x, const VecAD_reference<double>& y)
236
238pow(const AD<double>& x, const double& y)
240
242pow(const VecAD_reference<double>& x, const double& y)
244
245// =========================================================================
246// Fold operations for the cases where x is an int,
247// but let cppad/pow_int.hpp handle the cases where y is an int.
248// -------------------------------------------------------------------------