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

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

int main(void)
{
  // 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;
  void    *ad_handle = 0;
  x10aa_t1w_f_(ad_handle,ifail);

  // 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;
  e01da_t1w_f_(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;
  nagad_t1w_inc_derivative(&tx[0],inc);
  ifail = 0;
  e02de_t1w_f_(ad_handle,m,px,py,tx,ty,lamda,mu,c,ff,wrk,iwrk,ifail);
  double dx = nagad_t1w_get_derivative(ff[0]);
  nagad_t1w_set_derivative(&tx[0],zero);

  nagad_t1w_inc_derivative(&tx[0],inc);
  ifail = 0;
  e02de_t1w_f_(ad_handle,m,px,py,tx,ty,lamda,mu,c,ff,wrk,iwrk,ifail);
  double dy = nagad_t1w_get_derivative(ff[0]);
  
  cout << "\n Interpolant evaluated at x = " << nagad_t1w_get_value(tx[0]);
  cout << " and y = " << nagad_t1w_get_value(ty[0]);
  cout.precision(5);
  cout << "\n Value of interpolant       = ";
  cout << nagad_t1w_get_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;
  
  // Remove computational data object
  x10ab_t1w_f_(ad_handle,ifail);
  
  delete [] x;
  delete [] y;
  delete [] lamda;
  delete [] mu;
  delete [] f;
  delete [] c;
  delete [] wrk;
  delete [] iwrk;
  return exit_status;
}