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

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

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

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

  cout << "E01EA_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, *y = 0, *f = 0;
  Integer *          triang = 0;
  if (!(x = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
      !(y = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
      !(f = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
      !(triang = NAG_ALLOC(7 * n, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
  }
  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;
    nag::ad::handle_t ad_handle;

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

    ifail = 0;
    nag::ad::e01ea(n, x, y, triang, ifail);
    // Evaluate interpolant and derivatives at a mid-point
    nagad_a1w_w_rtype px[1], py[1], pf[1];
    double            xint, yint;
    xint  = 0.5 * (dco::value(x[n / 2 - 1]) + dco::value(x[n / 2]));
    yint  = 0.5 * (dco::value(y[n / 2 - 1]) + dco::value(y[n / 2]));
    px[0] = xint;
    py[0] = yint;

    // Call the AD routine
    Integer m = 1;
    ifail     = 0;
    nag::ad::e01eb(ad_handle, m, n, x, y, f, triang, px, py, pf, ifail);

    cout << "\n Interpolant point: x = " << xint << " y = " << yint << endl;
    cout.precision(5);
    cout << " Interpolated value = " << 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/dy         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(y[j]);
      double df = dco::derivative(f[j]);
      cout.width(5);
      cout << j + 1;
      cout.width(12);
      cout << dx;
      cout.width(12);
      cout << dy;
      cout.width(12);
      cout << df << endl;
    }

    dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);
  }
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(f);
  NAG_FREE(triang);

  return exit_status;
}