NAG Library Manual, Mark 27.2
/* E01BA_T1W_F C++ Header Example Program.
*
* Copyright 2021 Numerical Algorithms Group.
* Mark 27.2, 2021.
*/

#include "dco.hpp"
#include <iostream>
#include <math.h>
#include <nag.h>
#include <stdio.h>
using namespace std;

int main(void)
{
// Scalars
int           exit_status = 0;
const Integer m           = 7;

cout << "E01BA_T1W_F C++ Header Example Program Results\n\n";

// Data points and values.
double            xr[m], yr[m], dx[m], dy[m];

xr[0] = 0.0;
xr[1] = 0.2;
xr[2] = 0.4;
xr[3] = 0.6;
xr[4] = 0.75;
xr[5] = 0.9;
xr[6] = 1.0;

for (int i = 0; i < m; i++)
{
yr[i] = exp(xr[i]);
x[i]  = xr[i];
y[i]  = yr[i];
}

// Create AD configuration data object
Integer ifail     = 0;

xint = 0.5;

for (int i = 0; i < 2 * m; ++i)
{
double inc = 1.0;
if (i < m)
{
dco::derivative(x[i]) = inc;
}
else
{
dco::derivative(y[i - m]) = inc;
}

const Integer     lck = m + 4, lwrk = 6 * m + 16;
ifail = 0;

// Evaluate computed spline using e02bb
fit   = 0.0;
ifail = 0;

double zero = 0.0;
if (i < m)
{
dx[i]                 = dco::derivative(fit);
dco::derivative(x[i]) = zero;
}
else
{
dy[i - m]                 = dco::derivative(fit);
dco::derivative(y[i - m]) = zero;
}
}

cout << "\n Value of fitted spline at x = " << dco::value(xint);
cout.precision(5);
cout << " is: " << dco::value(fit) << endl;

cout << "\n Derivatives calculated: First order tangents\n";
cout << " Computational mode    : algorithmic\n";

// Get derivatives
cout << "\n Derivatives of fitted value w.r.t. data points:\n";
cout << "  j    d/dx(j)      d/y(j)\n";
cout.setf(ios::scientific, ios::floatfield);
cout.precision(4);
for (int j = 0; j < m; j++)
{
cout.width(3);
cout << j + 1;
cout.width(12);
cout << dx[j];
cout.width(12);
cout << dy[j] << endl;
}

// Remove computational data object