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

NAG AD Library Introduction
Example description
#include "dco.hpp"
/* E01BE_A1W_F C++ Header Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 * Mark 27.2, 2021.
 */

#include <iostream>
#include <math.h>
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
#include <string>
using namespace std;

int main(void)
{
  // Scalars
  int exit_status = 0;

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

  // Skip first line of data file
  string mystr;
  getline(cin, mystr);
  // Read number of data points
  Integer n;
  cin >> n;

  // Allocate arrays for data and interpolant
  nagad_a1w_w_rtype *x = 0, *f = 0, *d = 0;
  if (!(x = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
      !(f = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
      !(d = NAG_ALLOC(n, nagad_a1w_w_rtype)))
    {
      printf("Allocation failure\n");
      exit_status = -2;
    }
  if (exit_status == 0)
    {
      // Create AD tape
      dco::ga1s<double>::global_tape = dco::ga1s<double>::tape_t::create();

      // Create AD configuration data object
      Integer ifail     = 0;
      void *  ad_handle = 0;
      nag::ad::x10aa(ad_handle, ifail);

      // Read data and register variables
      for (int i = 0; i < n; i++)
        {
          double xr, fr;
          cin >> xr >> fr;
          x[i] = xr;
          f[i] = fr;
          dco::ga1s<double>::global_tape->register_variable(x[i]);
          dco::ga1s<double>::global_tape->register_variable(f[i]);
        }

      // Call the AD routine
      ifail = 0;
      nag::ad::e01be(ad_handle, n, x, f, d, ifail);

      // Evaluate interpolant and derivatives at a mid-point
      const Integer     m = 1;
      nagad_a1w_w_rtype px[m], pf[m], pd[m];
      double            xint;
      xint  = 0.5 * (dco::value(x[n / 2 - 1]) + dco::value(x[n / 2]));
      px[0] = xint;

      ifail = 0;
      nag::ad::e01bg(ad_handle, n, x, f, d, m, px, pf, pd, ifail);

      cout << "\n Value of interpolant at x = " << xint;
      cout.precision(5);
      cout << " is: " << dco::value(pf[0]) << endl;

      // Setup evaluation of derivatives via adjoints.
      double inc = 1.0;
      dco::derivative(pf[0]) += inc;

      ifail                                              = 0;
      dco::ga1s<double>::global_tape->sparse_interpret() = true;
      dco::ga1s<double>::global_tape->interpret_adjoint();

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

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

      // Remove computational data object and tape
      nag::ad::x10ab(ad_handle, ifail);
      dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);
    }
  NAG_FREE(x);
  NAG_FREE(f);
  NAG_FREE(d);

  return exit_status;
}