CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
poly.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_UTILITY_POLY_HPP
2 # define CPPAD_UTILITY_POLY_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 $begin Poly$$
16 $spell
17  cppad.hpp
18  CppAD
19  namespace
20  cstddef
21  ifndef
22  endif
23  deg
24  const
25  std
26  da
27 $$
28 
29 
30 $section Evaluate a Polynomial or its Derivative$$
31 $mindex Poly template$$
32 
33 $head Syntax$$
34 $codei%# include <cppad/utility/poly.hpp>
35 %$$
36 $icode%p% = Poly(%k%, %a%, %z%)%$$
37 
38 
39 $head Description$$
40 Computes the $th k$$ derivative of the polynomial
41 $latex \[
42  P(z) = a_0 + a_1 z^1 + \cdots + a_d z^d
43 \] $$
44 If $icode k$$ is equal to zero, the return value is $latex P(z)$$.
45 
46 $head Include$$
47 The file $code cppad/poly.hpp$$ is included by $code cppad/cppad.hpp$$
48 but it can also be included separately with out the rest of
49 the $code CppAD$$ routines.
50 Including this file defines
51 $code Poly$$ within the $code CppAD$$ namespace.
52 
53 $head k$$
54 The argument $icode k$$ has prototype
55 $codei%
56  size_t %k%
57 %$$
58 It specifies the order of the derivative to calculate.
59 
60 $head a$$
61 The argument $icode a$$ has prototype
62 $codei%
63  const %Vector% &%a%
64 %$$
65 (see $cref/Vector/Poly/Vector/$$ below).
66 It specifies the vector corresponding to the polynomial $latex P(z)$$.
67 
68 $head z$$
69 The argument $icode z$$ has prototype
70 $codei%
71  const %Type% &%z%
72 %$$
73 (see $icode Type$$ below).
74 It specifies the point at which to evaluate the polynomial
75 
76 $head p$$
77 The result $icode p$$ has prototype
78 $codei%
79  %Type% %p%
80 %$$
81 (see $cref/Type/Poly/Type/$$ below)
82 and it is equal to the $th k$$ derivative of $latex P(z)$$; i.e.,
83 $latex \[
84 p = \frac{k !}{0 !} a_k
85  + \frac{(k+1) !}{1 !} a_{k+1} z^1
86  + \ldots
87  + \frac{d !}{(d - k) !} a_d z^{d - k}
88 \]
89 $$
90 If $latex k > d$$, $icode%p% = %Type%(0)%$$.
91 
92 $head Type$$
93 The type $icode Type$$ is determined by the argument $icode z$$.
94 It is assumed that
95 multiplication and addition of $icode Type$$ objects
96 are commutative.
97 
98 $subhead Operations$$
99 The following operations must be supported where
100 $icode x$$ and $icode y$$ are objects of type $icode Type$$
101 and $icode i$$ is an $code int$$:
102 $table
103 $icode%x% = %i%$$ $cnext assignment $rnext
104 $icode%x% = %y%$$ $cnext assignment $rnext
105 $icode%x% *= %y%$$ $cnext multiplication compound assignment $rnext
106 $icode%x% += %y%$$ $cnext addition compound assignment
107 
108 $tend
109 
110 
111 $head Vector$$
112 The type $icode Vector$$ must be a $cref SimpleVector$$ class with
113 $cref/elements of type/SimpleVector/Elements of Specified Type/$$
114 $icode Type$$.
115 The routine $cref CheckSimpleVector$$ will generate an error message
116 if this is not the case.
117 
118 $head Operation Sequence$$
119 The $icode Type$$ operation sequence used to calculate $icode p$$ is
120 $cref/independent/glossary/Operation/Independent/$$
121 of $icode z$$ and the elements of $icode a$$
122 (it does depend on the size of the vector $icode a$$).
123 
124 
125 $children%
126  example/general/poly.cpp%
127  omh/poly_hpp.omh
128 %$$
129 
130 $head Example$$
131 The file
132 $cref poly.cpp$$
133 contains an example and test of this routine.
134 It returns true if it succeeds and false otherwise.
135 
136 $head Source$$
137 The file $cref poly.hpp$$ contains the
138 current source code that implements these specifications.
139 
140 $end
141 ------------------------------------------------------------------------------
142 */
143 // BEGIN C++
144 # include <cstddef> // used to defined size_t
146 
147 namespace CppAD { // BEGIN CppAD namespace
148 
149 template <class Type, class Vector>
150 Type Poly(size_t k, const Vector &a, const Type &z)
151 { size_t i;
152  size_t d = a.size() - 1;
153 
154  Type tmp;
155 
156  // check Vector is Simple Vector class with Type elements
157  CheckSimpleVector<Type, Vector>();
158 
159  // case where derivative order greater than degree of polynomial
160  if( k > d )
161  { tmp = 0;
162  return tmp;
163  }
164  // case where we are evaluating a derivative
165  if( k > 0 )
166  { // initialize factor as (k-1) !
167  size_t factor = 1;
168  for(i = 2; i < k; i++)
169  factor *= i;
170 
171  // set b to coefficient vector corresponding to derivative
172  Vector b(d - k + 1);
173  for(i = k; i <= d; i++)
174  { factor *= i;
175  tmp = double( factor );
176  b[i - k] = a[i] * tmp;
177  factor /= (i - k + 1);
178  }
179  // value of derivative polynomial
180  return Poly(0, b, z);
181  }
182  // case where we are evaluating the original polynomial
183  Type sum = a[d];
184  i = d;
185  while(i > 0)
186  { sum *= z;
187  sum += a[--i];
188  }
189  return sum;
190 }
191 } // END CppAD namespace
192 // END C++
193 # endif
Type Poly(size_t k, const Vector &a, const Type &z)
Definition: poly.hpp:150