CppAD: A C++ Algorithmic Differentiation Package
20171217
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
pow_int.hpp
Go to the documentation of this file.
1
# ifndef CPPAD_UTILITY_POW_INT_HPP
2
# define CPPAD_UTILITY_POW_INT_HPP
3
4
/* --------------------------------------------------------------------------
5
CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6
7
CppAD is distributed under multiple licenses. This distribution is under
8
the terms of the
9
Eclipse Public License Version 1.0.
10
11
A copy of this license is included in the COPYING file of this distribution.
12
Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13
-------------------------------------------------------------------------- */
14
15
/*
16
-------------------------------------------------------------------------------
17
$begin pow_int$$
18
$spell
19
cppad.hpp
20
CppAD
21
namespace
22
const
23
$$
24
25
26
$section The Integer Power Function$$
27
$mindex pow exponent$$
28
29
$head Syntax$$
30
$codei%# include <cppad/utility/pow_int.hpp>
31
%$$
32
$icode%z% = pow(%x%, %y%)%$$
33
34
$head See Also$$
35
$cref pow$$
36
37
$head Purpose$$
38
Determines the value of the power function
39
$latex \[
40
{\rm pow} (x, y) = x^y
41
\] $$
42
for integer exponents $icode n$$
43
using multiplication and possibly division to compute the value.
44
The other CppAD $cref pow$$ function may use logarithms and exponentiation
45
to compute derivatives of the same value
46
(which will not work if $icode x$$ is less than or equal zero).
47
48
$head Include$$
49
The file $code cppad/pow_int.h$$ is included by $code cppad/cppad.hpp$$
50
but it can also be included separately with out the rest of
51
the $code CppAD$$ routines.
52
Including this file defines
53
this version of the $code pow$$ within the $code CppAD$$ namespace.
54
55
$head x$$
56
The argument $icode x$$ has prototype
57
$codei%
58
const %Type%& %x%
59
%$$
60
61
$head y$$
62
The argument $icode y$$ has prototype
63
$codei%
64
const int& %y%
65
%$$
66
67
$head z$$
68
The result $icode z$$ has prototype
69
$codei%
70
%Type% %z%
71
%$$
72
73
$head Type$$
74
The type $icode Type$$ must support the following operations
75
where $icode a$$ and $icode b$$ are $icode Type$$ objects
76
and $icode i$$ is an $code int$$:
77
$table
78
$bold Operation$$ $pre $$
79
$cnext $bold Description$$
80
$cnext $bold Result Type$$
81
$rnext
82
$icode%Type% %a%(%i%)%$$
83
$cnext construction of a $icode Type$$ object from an $code int$$
84
$cnext $icode Type$$
85
$rnext
86
$icode%a% * %b%$$
87
$cnext binary multiplication of $icode Type$$ objects
88
$cnext $icode Type$$
89
$rnext
90
$icode%a% / %b%$$
91
$cnext binary division of $icode Type$$ objects
92
$cnext $icode Type$$
93
$tend
94
95
$head Operation Sequence$$
96
The $icode Type$$ operation sequence used to calculate $icode z$$ is
97
$cref/independent/glossary/Operation/Independent/$$
98
of $icode x$$.
99
100
$head Example$$
101
$children%
102
example/general/pow_int.cpp
103
%$$
104
The file $cref pow_int.cpp$$
105
is an example and test of this function.
106
It returns true if it succeeds and false otherwise.
107
108
109
$end
110
-------------------------------------------------------------------------------
111
*/
112
113
namespace
CppAD {
114
115
template
<
class
Type>
116
inline
Type
pow
(
const
Type& x,
const
int
& n)
117
{
118
Type p(1);
119
int
n2 = n / 2;
120
121
if
( n == 0 )
122
return
p;
123
if
( n < 0 )
124
return
p /
pow
(x, -n);
125
if
( n == 1 )
126
return
x;
127
128
// p = (x^2)^(n/2)
129
p =
pow
( x * x , n2 );
130
131
// n is even case
132
if
( n % 2 == 0 )
133
return
p;
134
135
// n is odd case
136
return
p * x;
137
}
138
139
}
140
141
# endif
CppAD::pow
Type pow(const Type &x, const int &n)
Definition:
pow_int.hpp:116
cppad
utility
pow_int.hpp
Generated by
1.8.5