1 /* --------------------------------------------------------------------------
2 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
3 
4 CppAD is distributed under the terms of the
5              Eclipse Public License Version 2.0.
6 
7 This Source Code may also be made available under the following
8 Secondary License when the conditions for such availability set forth
9 in the Eclipse Public License, Version 2.0 are satisfied:
10       GNU General Public License, Version 2.0 or later.
11 ---------------------------------------------------------------------------- */
12 /*
13 $begin sacado_poly.cpp$$
14 $spell
15     onetape
16     cppad
17     cpp
18     tadiff
19     ddp
20     Taylor
21     dz
22     eval
23     cppad
24     vector Vector
25     typedef
26     sacado
27     Lu
28     CppAD
29     det
30     hpp
31     const
32     bool
33     Tay
34     resize
35     Coeff
36 $$
37 
38 $section Sacado Speed: Second Derivative of a Polynomial$$
39 
40 
41 $head Specifications$$
42 See $cref link_poly$$.
43 
44 $head Implementation$$
45 
46 
47 $srccode%cpp% */
48 // suppress conversion warnings before other includes
49 # include <cppad/wno_conversion.hpp>
50 //
51 # include <Sacado.hpp>
52 # include <cppad/utility/vector.hpp>
53 # include <cppad/utility/poly.hpp>
54 # include <cppad/speed/uniform_01.hpp>
55 
56 // list of possible options
57 # include <map>
58 extern std::map<std::string, bool> global_option;
59 
link_poly(size_t size,size_t repeat,CppAD::vector<double> & a,CppAD::vector<double> & z,CppAD::vector<double> & ddp)60 bool link_poly(
61     size_t                     size     ,
62     size_t                     repeat   ,
63     CppAD::vector<double>     &a        ,  // coefficients of polynomial
64     CppAD::vector<double>     &z        ,  // polynomial argument value
65     CppAD::vector<double>     &ddp      )  // second derivative w.r.t z
66 {
67     if( global_option["atomic"] )
68         return false;
69     if( global_option["memory"] || global_option["onetape"] || global_option["optimize"] )
70         return false;
71     // -----------------------------------------------------
72     // setup
73     typedef Sacado::Tay::Taylor<double>  ADScalar;
74     CppAD::vector<ADScalar>              A(size);
75 
76     size_t i;               // temporary index
77     ADScalar   Z;           // domain space AD value
78     ADScalar   P;           // range space AD value
79     unsigned int order = 2; // order of Taylor coefficients
80     Z.resize(order+1, false);
81     P.resize(order+1, false);
82 
83     // choose the polynomial coefficients
84     CppAD::uniform_01(size, a);
85 
86     // AD copy of the polynomial coefficients
87     for(i = 0; i < size; i++)
88         A[i] = a[i];
89 
90     // ------------------------------------------------------
91     while(repeat--)
92     {   // get the next argument value
93         CppAD::uniform_01(1, z);
94 
95         // independent variable value
96         Z.fastAccessCoeff(0)   = z[0]; // argument value
97         Z.fastAccessCoeff(1)   = 1.;   // first order coefficient
98         Z.fastAccessCoeff(2)   = 0.;   // second order coefficient
99 
100         // AD computation of the dependent variable
101         P = CppAD::Poly(0, A, Z);
102 
103         // second derivative is twice second order Taylor coefficient
104         ddp[0] = 2. * P.fastAccessCoeff(2);
105     }
106     // ------------------------------------------------------
107     return true;
108 }
109 /* %$$
110 $end
111 */
112