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

NAG AD Library Introduction
Example description
/* E01DA_T1W_F C++ Header Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 * Mark 29.1, 2023.
 */

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

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

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

  // Skip first line of data file
  string mystr;
  getline(cin, mystr);
  // Read number of x and y data points
  Integer mx, my;
  cin >> mx;
  cin >> my;

  // Allocate arrays for data and interpolant
  nagad_t1w_w_rtype *x = 0, *lamda = 0, *y = 0, *mu = 0, *f = 0, *c = 0,
                    *wrk = 0;
  Integer *iwrk          = 0;
  Integer  lwrk          = (my + 6) * (mx + 6);
  x                      = new nagad_t1w_w_rtype[mx];
  y                      = new nagad_t1w_w_rtype[my];
  lamda                  = new nagad_t1w_w_rtype[mx + 4];
  mu                     = new nagad_t1w_w_rtype[my + 4];
  f                      = new nagad_t1w_w_rtype[my * mx];
  c                      = new nagad_t1w_w_rtype[my * mx];
  wrk                    = new nagad_t1w_w_rtype[lwrk];
  iwrk                   = new Integer[lwrk];

  // Create AD configuration data object
  Integer           ifail = 0;
  nag::ad::handle_t ad_handle;

  // Read data and register variables
  for (int i = 0; i < mx; i++)
  {
    double xr;
    cin >> xr;
    x[i] = xr;
  }
  for (int i = 0; i < my; i++)
  {
    double yr;
    cin >> yr;
    y[i] = yr;
  }
  for (int i = 0; i < my; i++)
  {
    double fr;
    for (int j = 0; j < mx; j++)
    {
      Integer k = j * my + i;
      cin >> fr;
      f[k] = fr;
    }
  }

  // Call the AD routine
  Integer px, py;
  ifail = 0;
  nag::ad::e01da(ad_handle, mx, my, x, y, f, px, py, lamda, mu, c, wrk, ifail);

  // Evaluate interpolant and derivatives at an internal point
  const Integer     m = 1;
  nagad_t1w_w_rtype tx[m], ty[m], ff[m];
  tx[0] = 1.4;
  ty[0] = 0.5;

  double inc = 1.0, zero = 0.0;
  dco::derivative(tx[0]) = inc;
  ifail                  = 0;
  nag::ad::e02de(ad_handle, m, px, py, tx, ty, lamda, mu, c, ff, wrk, iwrk,
                 ifail);
  double dx              = dco::derivative(ff[0]);
  dco::derivative(tx[0]) = zero;

  dco::derivative(tx[0]) = inc;
  ifail                  = 0;
  nag::ad::e02de(ad_handle, m, px, py, tx, ty, lamda, mu, c, ff, wrk, iwrk,
                 ifail);
  double dy = dco::derivative(ff[0]);

  cout << "\n Interpolant evaluated at x = " << dco::value(tx[0]);
  cout << " and y = " << dco::value(ty[0]);
  cout.precision(5);
  cout << "\n Value of interpolant       = ";
  cout << dco::value(ff[0]) << endl;

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

  cout << "\n Derivatives of fitted value w.r.t. fit point:\n\n";
  cout << "   dfit/dx = " << dx << endl;
  cout << "   dfit/dy = " << dy << endl;

  delete[] x;
  delete[] y;
  delete[] lamda;
  delete[] mu;
  delete[] f;
  delete[] c;
  delete[] wrk;
  delete[] iwrk;
  return exit_status;
}