CppAD: A C++ Algorithmic Differentiation Package 20110419
pow_int.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_POW_INT_INCLUDED
00003 # define CPPAD_POW_INT_INCLUDED
00004 
00005 /* --------------------------------------------------------------------------
00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-08 Bradley M. Bell
00007 
00008 CppAD is distributed under multiple licenses. This distribution is under
00009 the terms of the 
00010                     Common Public License Version 1.0.
00011 
00012 A copy of this license is included in the COPYING file of this distribution.
00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00014 -------------------------------------------------------------------------- */
00015 
00016 /*
00017 -------------------------------------------------------------------------------
00018 $begin pow_int$$
00019 $spell
00020         cppad.hpp
00021         CppAD
00022         namespace
00023         const
00024 $$
00025 
00026 $index pow, integer$$
00027 $index exponent, integer$$
00028 $index integer, pow$$
00029 
00030 $section The Integer Power Function$$
00031 
00032 $head Syntax$$
00033 $code # include <cppad/pow_int.h>$$
00034 $pre
00035 $$
00036 $syntax%%z% = pow(%x%, %y%)%$$
00037 
00038 $head Purpose$$
00039 Determines the value of the power function 
00040 $latex \[
00041         {\rm pow} (x, y) = x^y
00042 \] $$
00043 for integer exponents $italic n$$ 
00044 using multiplication and possibly division to compute the value.
00045 The other CppAD $cref/pow/$$ function may use logarithms and exponentiation 
00046 to compute derivatives of the same value
00047 (which will not work if $italic x$$ is less than or equal zero).
00048 
00049 $head Include$$
00050 The file $code cppad/pow_int.h$$ is included by $code cppad/cppad.hpp$$
00051 but it can also be included separately with out the rest of 
00052 the $code CppAD$$ routines.
00053 Including this file defines
00054 this version of the $code pow$$ within the $code CppAD$$ namespace.
00055 
00056 $head x$$
00057 The argument $italic x$$ has prototype
00058 $syntax%
00059         const %Type% &%x%
00060 %$$ 
00061 
00062 $head y$$
00063 The argument $italic y$$ has prototype
00064 $syntax%
00065         const int &%y%
00066 %$$ 
00067 
00068 $head z$$
00069 The result $italic z$$ has prototype
00070 $syntax%
00071         %Type% %z%
00072 %$$
00073 
00074 $head Type$$
00075 The type $italic Type$$ must support the following operations
00076 where $italic a$$ and $italic b$$ are $italic Type$$ objects
00077 and $italic i$$ is an $code int$$:
00078 $table
00079 $bold Operation$$  $pre  $$
00080         $cnext $bold Description$$ 
00081         $cnext $bold Result Type$$ 
00082 $rnext
00083 $syntax%%Type% %a%(%i%)%$$ 
00084         $cnext construction of a $italic Type$$ object from an $code int$$
00085         $cnext $italic Type$$
00086 $rnext
00087 $syntax%%a% * %b%$$ 
00088         $cnext binary multiplication of $italic Type$$ objects
00089         $cnext $italic Type$$
00090 $rnext
00091 $syntax%%a% / %b%$$ 
00092         $cnext binary division of $italic Type$$ objects
00093         $cnext $italic Type$$
00094 $tend
00095 
00096 $head Operation Sequence$$
00097 The $italic Type$$ operation sequence used to calculate $italic z$$ is 
00098 $xref/glossary/Operation/Independent/independent/1/$$
00099 of $italic x$$.
00100 
00101 
00102 $end
00103 -------------------------------------------------------------------------------
00104 */
00105 
00106 namespace CppAD { 
00107 
00108         template <class Type>
00109         inline Type pow (const Type &x, const int &n)
00110         {
00111                 Type p(1);
00112                 int n2 = n / 2;
00113 
00114                 if( n == 0 )
00115                         return p;
00116                 if( n < 0 )
00117                         return p / pow(x, -n);
00118                 if( n == 1 )
00119                         return x;
00120 
00121                 // p = (x^2)^(n/2)
00122                 p = pow( x * x , n2 );
00123 
00124                 // n is even case
00125                 if( n % 2 == 0 )
00126                         return p;
00127 
00128                 // n is odd case
00129                 return p * x;
00130         }
00131 
00132 }
00133 
00134 # endif