Example description
/* E01EB_A1W_F C++ Header Example Program.
 *
 * Copyright 2019 Numerical Algorithms Group.
 * Mark 27, 2019.
 */

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

int main(void)
{
  // Scalars
  int               exit_status = 0;
  
  cout << "E01EB_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
    nagad_a1w_ir_create();

    // Create AD configuration data object
    Integer ifail = 0;
    void    *ad_handle = 0;
    x10aa_a1w_f_(ad_handle,ifail);

    // Read data and register variables
    for (int i=0; i<n; i++) {
      double xr, yr, fr;
      cin >> xr >> yr >> fr;
      x[i].value = xr;
      x[i].id = 0;
      y[i].value = yr;
      y[i].id = 0;
      f[i].value = fr;
      f[i].id = 0;
      nagad_a1w_ir_register_variable(&x[i]);
      nagad_a1w_ir_register_variable(&y[i]);
      nagad_a1w_ir_register_variable(&f[i]);
    }
    
    // Call the AD routine
    ifail = 0;
    e01ea_a1w_f_(ad_handle,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*(nagad_a1w_get_value(x[n/2-1]) + nagad_a1w_get_value(x[n/2]));
    yint = 0.5*(nagad_a1w_get_value(y[n/2-1]) + nagad_a1w_get_value(y[n/2]));
    px[0].value = xint;
    px[0].id = 0;
    py[0].value = yint;
    py[0].id = 0;
    
    Integer     m = 1;
    ifail = 0;
    e01eb_a1w_f_(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 = " << nagad_a1w_get_value(pf[0]) << endl;

    // Setup evaluation of derivatives via adjoints.
    double inc = 1.0;
    nagad_a1w_inc_derivative(&pf[0],inc);

    ifail = 0;
    nagad_a1w_ir_interpret_adjoint(ifail);

    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 = nagad_a1w_get_derivative(x[j]);
      double dy = nagad_a1w_get_derivative(y[j]);
      double df = nagad_a1w_get_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;
    }

    // Remove computational data object and tape
    x10ab_a1w_f_(ad_handle,ifail);
    nagad_a1w_ir_remove();
  }
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(f);
  NAG_FREE(triang);
  
  return exit_status;
}