$\newcommand{\W}[1]{ \; #1 \; } \newcommand{\R}[1]{ {\rm #1} } \newcommand{\B}[1]{ {\bf #1} } \newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} } \newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} } \newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} } \newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }$
Adolc Speed: Second Derivative of a Polynomial

Specifications

Implementation
// suppress conversion warnings before other includes
//
# include <vector>

// list of possible options
# include <map>
extern std::map<std::string, bool> global_option;

size_t                     size     ,
size_t                     repeat   ,
CppAD::vector<double>     &a        ,  // coefficients of polynomial
CppAD::vector<double>     &z        ,  // polynomial argument value
CppAD::vector<double>     &ddp      )  // second derivative w.r.t z
{
if( global_option["atomic"] )
return false;
if( global_option["memory"] || global_option["optimize"] )
return false;
// -----------------------------------------------------
// setup
size_t i;
int tag  = 0;  // tape identifier
int keep = 0;  // do not keep forward mode results in buffer
int m    = 1;  // number of dependent variables
int n    = 1;  // number of independent variables
int d    = 2;  // highest derivative degree
double f;      // function value

// set up for thread_alloc memory allocator (fast and checks for leaks)
size_t capacity;           // capacity of an allocation

// choose a vector of polynomial coefficients

// AD copy of the polynomial coefficients
for(i = 0; i < size; i++)
A[i] = a[i];

// domain and range space AD values

// allocate arguments to hos_forward

// Taylor coefficient for argument
x[0][0] = 1.;  // first order
x[0][1] = 0.;  // second order

// ----------------------------------------------------------------------
if( ! global_option["onetape"] ) while(repeat--)
{     // choose an argument value

// declare independent variables
trace_on(tag, keep);
Z <<= z[0];

// AD computation of the function value

// create function object f : Z -> P
P >>= f;
trace_off();

// set the argument value
x0[0] = z[0];

// evaluate the polynomial at the new argument value
hos_forward(tag, m, n, d, keep, x0, x, y0, y);

// second derivative is twice second order Taylor coef
ddp[0] = 2. * y[0][1];
}
else
{
// choose an argument value

// declare independent variables
trace_on(tag, keep);
Z <<= z[0];

// AD computation of the function value

// create function object f : Z -> P
P >>= f;
trace_off();

while(repeat--)
{     // get the next argument value
x0[0] = z[0];

// evaluate the polynomial at the new argument value
hos_forward(tag, m, n, d, keep, x0, x, y0, y);

// second derivative is twice second order Taylor coef
ddp[0] = 2. * y[0][1];
}
}
// ------------------------------------------------------
// tear down