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

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

int main(void)
{
  // Scalars
  int               exit_status = 0;
  
  cout << "E01BF_T1W_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_t1w_w_rtype *x = 0, *f = 0, *d = 0;
  double            *dx = 0, *df = 0;
  x = new nagad_t1w_w_rtype [n];
  f = new nagad_t1w_w_rtype [n];
  d = new nagad_t1w_w_rtype [n];
  dx = new double [n];
  df = new double [n];

  // Create AD configuration data object
  Integer ifail = 0;
  void    *ad_handle = 0;
  x10aa_t1w_f_(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;
  }

  for (int i=0; i<2*n; ++i) {
    // Call the AD routine
    double inc = 1.0;
    if (i<n) {
      nagad_t1w_inc_derivative(&x[i],inc);
    } else {
      nagad_t1w_inc_derivative(&f[i-n],inc);
    }
    ifail = 0;
    e01be_t1w_f_(ad_handle,n,x,f,d,ifail);

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

    ifail = 0;
    e01bf_t1w_f_(ad_handle,n,x,f,d,m,px,pf,ifail);

    double zero = 0.0;
    if (i<n) {
      dx[i] = nagad_t1w_get_derivative(pf[0]);
      nagad_t1w_set_derivative(&x[i],zero);
    } else {
      df[i-n] = nagad_t1w_get_derivative(pf[0]);
      nagad_t1w_set_derivative(&f[i-n],zero);
    }

    if (i==0) {
      cout << "\n Value of interpolant at x = " << xint;
      cout.precision(5);
      cout << " is: " << nagad_t1w_get_value(pf[0]) << 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\n";
  cout << "    i     d/dx         d/df\n";
  cout.setf(ios::scientific,ios::floatfield);
  cout.precision(4);
  for (int j=0; j < n; j++) {
    cout.width(5); cout << j+1;
    cout.width(12); cout << dx[j];
    cout.width(12); cout << df[j] << endl;
  }

  // Remove computational data object and tape
  x10ab_t1w_f_(ad_handle,ifail);
  
  delete [] x;
  delete [] f;
  delete [] d;
  delete [] dx;
  delete [] df;
  return exit_status;
}