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