Example description
/* E02DE_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 <nagx04.h>
#include <string>
#include <iostream>
using namespace std;

int main(void)
{
  // Scalars
  int               exit_status = 0;
  
  cout << "E02DE_A1W_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_a1w_w_rtype *x = 0,  *lamda = 0, *y = 0, *mu = 0, *f = 0, *c = 0,
                    *wrk = 0;
  double            *dx = 0, *dy = 0, *df = 0;
  Integer           *iwrk = 0;
  Integer           lwrk = (my+6)*(mx+6);
  if (!(x      = NAG_ALLOC(mx,    nagad_a1w_w_rtype)) ||
      !(y      = NAG_ALLOC(my,    nagad_a1w_w_rtype)) ||
      !(lamda  = NAG_ALLOC(mx+4,  nagad_a1w_w_rtype)) ||
      !(mu     = NAG_ALLOC(my+4,  nagad_a1w_w_rtype)) ||
      !(f      = NAG_ALLOC(my*mx, nagad_a1w_w_rtype)) ||
      !(c      = NAG_ALLOC(my*mx, nagad_a1w_w_rtype)) ||
      !(wrk    = NAG_ALLOC(lwrk,  nagad_a1w_w_rtype)) ||
      !(dx     = NAG_ALLOC(mx,    double)) ||
      !(dy     = NAG_ALLOC(my,    double)) ||
      !(df     = NAG_ALLOC(my*mx, double)) ||
      !(iwrk   = NAG_ALLOC(lwrk,  Integer))) {
    printf("Allocation failure\n");
    exit_status = -2;
  }
  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<mx; i++) {
      double xr;
      cin >> xr;
      x[i].value = xr;
      x[i].id = 0;
      nagad_a1w_ir_register_variable(&x[i]);
    }
    for (int i=0; i<my; i++) {
      double yr;
      cin >> yr;
      y[i].value = yr;
      y[i].id = 0;
      nagad_a1w_ir_register_variable(&y[i]);
    }
    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].value = fr;
        f[k].id = 0;
        nagad_a1w_ir_register_variable(&f[k]);
      }
    }
    
    // Call the AD routine
    Integer px, py;
    ifail = 0;
    e01da_a1w_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_a1w_w_rtype tx[m], ty[m], ff[m];
    tx[0].value = 1.4;
    tx[0].id = 0;
    ty[0].value = 0.5;
    ty[0].id = 0;

    ifail = 0;
    e02de_a1w_f_(ad_handle,m,px,py,tx,ty,lamda,mu,c,ff,wrk,iwrk,ifail);

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

    // Setup evaluation of derivatives via adjoints.
    double inc = 1.0;
    nagad_a1w_inc_derivative(&ff[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";
    for (int j=0; j < mx; j++) {
      dx[j] = nagad_a1w_get_derivative(x[j]);
    }
    for (int j=0; j < my; j++) {
      dy[j] = nagad_a1w_get_derivative(y[j]);
    }
    for (int j=0; j < my*mx; j++) {
      df[j] = nagad_a1w_get_derivative(f[j]);
    }
    cout << endl;
    NagError  fail;
    INIT_FAIL(fail);
    x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,1,mx,dx,1,
           "     d/dx",0,&fail);
    cout << endl;
    x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,1,my,dy,1,
           "     d/dy",0,&fail);
    cout << endl;
    x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,my,mx,df,my,
           "     d/df",0,&fail);

    // Remove computational data object and tape
    x10ab_a1w_f_(ad_handle,ifail);
    nagad_a1w_ir_remove();
  }
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(lamda);
  NAG_FREE(mu);
  NAG_FREE(f);
  NAG_FREE(c);
  NAG_FREE(wrk);
  NAG_FREE(dx);
  NAG_FREE(dy);
  NAG_FREE(df);
  NAG_FREE(iwrk);
  
  return exit_status;
}