CppAD: A C++ Algorithmic Differentiation Package  20130102
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-12 Bradley M. Bell
00007 
00008 CppAD is distributed under multiple licenses. This distribution is under
00009 the terms of the 
00010                     Eclipse 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 $icode%z% = pow(%x%, %y%)%$$
00037 
00038 $head See Also$$
00039 $cref pow$$
00040 
00041 $head Purpose$$
00042 Determines the value of the power function 
00043 $latex \[
00044      {\rm pow} (x, y) = x^y
00045 \] $$
00046 for integer exponents $icode n$$ 
00047 using multiplication and possibly division to compute the value.
00048 The other CppAD $cref pow$$ function may use logarithms and exponentiation 
00049 to compute derivatives of the same value
00050 (which will not work if $icode x$$ is less than or equal zero).
00051 
00052 $head Include$$
00053 The file $code cppad/pow_int.h$$ is included by $code cppad/cppad.hpp$$
00054 but it can also be included separately with out the rest of 
00055 the $code CppAD$$ routines.
00056 Including this file defines
00057 this version of the $code pow$$ within the $code CppAD$$ namespace.
00058 
00059 $head x$$
00060 The argument $icode x$$ has prototype
00061 $codei%
00062      const %Type%& %x%
00063 %$$ 
00064 
00065 $head y$$
00066 The argument $icode y$$ has prototype
00067 $codei%
00068      const int& %y%
00069 %$$ 
00070 
00071 $head z$$
00072 The result $icode z$$ has prototype
00073 $codei%
00074      %Type% %z%
00075 %$$
00076 
00077 $head Type$$
00078 The type $icode Type$$ must support the following operations
00079 where $icode a$$ and $icode b$$ are $icode Type$$ objects
00080 and $icode i$$ is an $code int$$:
00081 $table
00082 $bold Operation$$  $pre  $$
00083      $cnext $bold Description$$ 
00084      $cnext $bold Result Type$$ 
00085 $rnext
00086 $icode%Type% %a%(%i%)%$$ 
00087      $cnext construction of a $icode Type$$ object from an $code int$$
00088      $cnext $icode Type$$
00089 $rnext
00090 $icode%a% * %b%$$ 
00091      $cnext binary multiplication of $icode Type$$ objects
00092      $cnext $icode Type$$
00093 $rnext
00094 $icode%a% / %b%$$ 
00095      $cnext binary division of $icode Type$$ objects
00096      $cnext $icode Type$$
00097 $tend
00098 
00099 $head Operation Sequence$$
00100 The $icode Type$$ operation sequence used to calculate $icode z$$ is 
00101 $cref/independent/glossary/Operation/Independent/$$
00102 of $icode x$$.
00103 
00104 $head Example$$
00105 $children%
00106      example/pow_int.cpp
00107 %$$
00108 The file $cref pow_int.cpp$$
00109 is an example and test of this function.   
00110 It returns true if it succeeds and false otherwise.
00111 
00112 
00113 $end
00114 -------------------------------------------------------------------------------
00115 */
00116 
00117 namespace CppAD { 
00118 
00119      template <class Type>
00120      inline Type pow (const Type& x, const int& n)
00121      {
00122           Type p(1);
00123           int n2 = n / 2;
00124 
00125           if( n == 0 )
00126                return p;
00127           if( n < 0 )
00128                return p / pow(x, -n);
00129           if( n == 1 )
00130                return x;
00131 
00132           // p = (x^2)^(n/2)
00133           p = pow( x * x , n2 );
00134 
00135           // n is even case
00136           if( n % 2 == 0 )
00137                return p;
00138 
00139           // n is odd case
00140           return p * x;
00141      }
00142 
00143 }
00144 
00145 # endif 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines