/home/coin/SVN-release/CoinAll-1.1.0/cppad/cppad/local/pow.hpp

Go to the documentation of this file.
00001 # ifndef CPPAD_POW_INCLUDED
00002 # define CPPAD_POW_INCLUDED
00003 
00004 /* --------------------------------------------------------------------------
00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell
00006 
00007 CppAD is distributed under multiple licenses. This distribution is under
00008 the terms of the 
00009                     Common Public License Version 1.0.
00010 
00011 A copy of this license is included in the COPYING file of this distribution.
00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00013 -------------------------------------------------------------------------- */
00014 
00015 /*
00016 $begin pow$$
00017 $spell
00018         Vec
00019         std
00020         namespace
00021         CppAD
00022         const
00023 $$
00024 
00025 $index pow, AD$$
00026 $index exponent, AD function$$
00027 
00028 $section The AD Power Function$$
00029 
00030 $head Syntax$$
00031 $syntax%%z% = pow(%x%, %y%)%$$
00032 
00033 
00034 $head Purpose$$
00035 Determines the value of the power function which is defined by
00036 $latex \[
00037         {\rm pow} (x, y) = x^y
00038 \] $$
00039 This version of the $code pow$$ function may use
00040 logarithms and exponentiation to compute derivatives.
00041 This will not work if $italic x$$ is less than or equal zero.
00042 If the value of $italic y$$ is an integer, 
00043 the $cref/pow_int/$$ function is used to compute this value 
00044 using only multiplication (and division if $italic y$$ is negative). 
00045 (This will work even if $italic x$$ is less than or equal zero.)
00046 
00047 $head x$$
00048 The argument $italic x$$ has the following prototype
00049 $syntax%
00050         const %Type% &%x%
00051 %$$
00052 where $italic Type$$ is
00053 $syntax%VecAD<%Base%>::reference%$$,
00054 $syntax%AD<%Base%>%$$,
00055 $syntax%%Base%$$,
00056 $syntax%double%$$,
00057 or
00058 $syntax%int%$$.
00059 
00060 $head y$$
00061 The argument $italic y$$ has the following prototype
00062 $syntax%
00063         const %Type% &%y%
00064 %$$
00065 where $italic Type$$ is
00066 $syntax%VecAD<%Base%>::reference%$$,
00067 $syntax%AD<%Base%>%$$,
00068 $syntax%%Base%$$,
00069 $syntax%double%$$,
00070 or
00071 $syntax%int%$$.
00072 
00073 $head z$$
00074 The result $italic z$$ has prototype
00075 $syntax%
00076         AD<%Base%> %z%
00077 %$$
00078 
00079 $head Standard Types$$
00080 A definition for the $code pow$$ function is included
00081 in the CppAD namespace for the case where both $italic x$$
00082 and $italic y$$ have the same type and that type is
00083 $code float$$ or $code double$$.
00084  
00085 $head Operation Sequence$$
00086 This is an AD of $italic Base$$
00087 $xref/glossary/Operation/Atomic/atomic operation/1/$$
00088 and hence is part of the current
00089 AD of $italic Base$$
00090 $xref/glossary/Operation/Sequence/operation sequence/1/$$.
00091 
00092 $head Example$$
00093 $children%
00094         example/pow.cpp%
00095         example/pow_int.cpp
00096 %$$
00097 The files
00098 $xref/Pow.cpp/$$, $cref/pow_int.cpp/$$
00099 contain an examples and tests of this function.   
00100 They returns true if they succeed and false otherwise.
00101 
00102 $end
00103 -------------------------------------------------------------------------------
00104 */
00105 //  BEGIN CppAD namespace
00106 namespace CppAD {
00107  
00108 // copy of standard functions in CppAD namespace
00109 inline float pow(const float &x, const float &y)
00110 { return std::pow(x, y); }
00111 
00112 inline double pow(const double &x, const double &y)
00113 { return std::pow(x, y); }
00114 
00115 // case where x and y are AD<Base> -----------------------------------------
00116 template <class Base> AD<Base> 
00117 pow(const AD<Base> &x, const AD<Base> &y)
00118 {       AD<Base> p;
00119         CPPAD_ASSERT_UNKNOWN( Parameter(p) );
00120 
00121         // base type result
00122         p.value_  = pow(x.value_, y.value_);
00123 
00124         if( Variable(x) )
00125         {       if( Variable(y) )
00126                 {       // result = variable + variable
00127                         CPPAD_ASSERT_KNOWN(
00128                                 x.id_ == y.id_,
00129                                 "pow: arguments are AD objects that are"
00130                                 " variables on different tapes."
00131                         );
00132                         x.tape_this()-> 
00133                                 RecordOp(PowvvOp, p, x.taddr_, y.taddr_
00134                         );
00135                 }
00136                 // if IdenticalZero(y.value_), variable^0 is a parameter
00137                 else if( ! IdenticalZero(y.value_) )
00138                         x.tape_this()->
00139                                 RecordOp(PowvpOp, p, x.taddr_, y.value_);
00140         }
00141         else if( Variable(y) )
00142         {       // if IdenticalZero(x.value_), 0^variable is a parameter
00143                 if( ! IdenticalZero(x.value_) )
00144                         y.tape_this()->
00145                                 RecordOp(PowpvOp, p, x.value_, y.taddr_);
00146         }
00147 
00148         return p;
00149 }
00150 // =========================================================================
00151 // Fold operations in same way as CPPAD_FOLD_AD_VALUED_BINARY_OPERATION(Op)
00152 // -------------------------------------------------------------------------
00153 // Operations with VecAD_reference<Base> and AD<Base> only
00154 
00155 template <class Base> AD<Base>
00156 pow(const AD<Base> &x, const VecAD_reference<Base> &y)
00157 {       return pow(x, y.ADBase()); }
00158 
00159 template <class Base> AD<Base> 
00160 pow(const VecAD_reference<Base> &x, const VecAD_reference<Base> &y) 
00161 {       return pow(x.ADBase(), y.ADBase()); }
00162 
00163 template <class Base> AD<Base>
00164 pow(const VecAD_reference<Base> &x, const AD<Base> &y)
00165 {       return pow(x.ADBase(), y); }
00166 // -------------------------------------------------------------------------
00167 // Operations with Base
00168 
00169 template <class Base> AD<Base>
00170 pow(const Base &x, const AD<Base> &y)
00171 {       return pow(AD<Base>(x), y); }
00172 
00173 template <class Base> AD<Base>
00174 pow(const Base &x, const VecAD_reference<Base> &y)
00175 {       return pow(AD<Base>(x), y.ADBase()); }
00176 
00177 template <class Base> AD<Base>
00178 pow(const AD<Base> &x, const Base &y)
00179 {       return pow(x, AD<Base>(y)); }
00180 
00181 template <class Base> AD<Base>
00182 pow(const VecAD_reference<Base> &x, const Base &y)
00183 {       return pow(x.ADBase(), AD<Base>(y)); }
00184 // -------------------------------------------------------------------------
00185 // Operations with double
00186 
00187 template <class Base> AD<Base>
00188 pow(const double &x, const AD<Base> &y)
00189 {       return pow(AD<Base>(x), y); }
00190 
00191 template <class Base> AD<Base>
00192 pow(const double &x, const VecAD_reference<Base> &y)
00193 {       return pow(AD<Base>(x), y.ADBase()); }
00194 
00195 template <class Base> AD<Base>
00196 pow(const AD<Base> &x, const double &y)
00197 {       return pow(x, AD<Base>(y)); }
00198 
00199 template <class Base> AD<Base>
00200 pow(const VecAD_reference<Base> &x, const double &y)
00201 {       return pow(x.ADBase(), AD<Base>(y)); }
00202 // -------------------------------------------------------------------------
00203 // Special case to avoid ambuigity when Base is double
00204 
00205 inline AD<double>
00206 pow(const double &x, const AD<double> &y)
00207 {       return pow(AD<double>(x), y); }
00208 
00209 inline AD<double>
00210 pow(const double &x, const VecAD_reference<double> &y)
00211 {       return pow(AD<double>(x), y.ADBase()); }
00212 
00213 inline AD<double>
00214 pow(const AD<double> &x, const double &y)
00215 {       return pow(x, AD<double>(y)); }
00216 
00217 inline AD<double>
00218 pow(const VecAD_reference<double> &x, const double &y)
00219 {       return pow(x.ADBase(), AD<double>(y)); }
00220 
00221 // =========================================================================
00222 // Fold operations for the cases where x is an int, 
00223 // but let cppad/pow_int.hpp handle the cases where y is an int.
00224 // -------------------------------------------------------------------------
00225 template <class Base> AD<Base> pow
00226 (int x, const VecAD_reference<Base> &y)
00227 {       return pow(AD<Base>(x), y.ADBase()); }
00228 
00229 template <class Base> AD<Base> pow
00230 (int x, const AD<Base> &y)
00231 {       return pow(AD<Base>(x), y); }
00232 
00233 } // END CppAD namespace
00234 
00235 # endif 

Generated on Sun Nov 14 14:06:33 2010 for Coin-All by  doxygen 1.4.7