NAG Library Manual, Mark 27.3
Interfaces:  FL   CL   CPP   AD 

NAG AD Library Introduction
Example description
/* E02BB_T1W_F C++ Header Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 * Mark 27.3, 2021.
 */

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

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

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

  // Data points and values.
  double            xr[m], yr[m], dx[m], dy[m];
  nagad_t1w_w_rtype x[m], y[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;
  nag::ad::handle_t ad_handle;

  nagad_t1w_w_rtype xint, fit;
  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;
    }

    // Call the AD routine
    const Integer     lck = m + 4, lwrk = 6 * m + 16;
    nagad_t1w_w_rtype c[lck], lamda[lck], wrk[lwrk];
    ifail = 0;
    nag::ad::e01ba(ad_handle, m, x, y, lamda, c, lck, wrk, lwrk, ifail);

    // Evaluate computed spline using e02bb
    fit   = 0.0;
    ifail = 0;
    nag::ad::e02bb(ad_handle, lck, lamda, c, xint, fit, ifail);

    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;
  }

  return exit_status;
}