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

// #define USE_DCO

#ifdef USE_DCO
#include "dco_light.hpp"
#endif

#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 << "E01AA_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,  *a = 0, *b = 0, *c = 0;
  double            xr, ar;
  Integer           n1, n2;
  n1 = n + 1;
  n2 = n*(n+1)/2;
  if (!(a  = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
      !(b  = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
      !(c  = NAG_ALLOC(n2, nagad_a1w_w_rtype)))
 {
    printf("Allocation failure\n");
    exit_status = -1;
  }
  if (exit_status==0) {
    // Create AD tape
    nagad_a1w_ir_create();

    cin >> xr;
#ifdef USE_DCO
    x = xr;
#else
    x.value = xr;
    x.id = 0;
# endif

    for (int i= 0; i < n1; i++) {
      cin >> ar;
#ifdef USE_DCO
      a[i] = ar;
#else
      a[i].value = ar;
      a[i].id = 0;
#endif
    }
    for (int i= 0; i < n1; i++) {
      cin >> ar;
#ifdef USE_DCO
      b[i] = ar;
#else
      b[i].value = ar;
      b[i].id = 0;
#endif
    }
  
    // 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);

    // Register variables to differentiate w.r.t.
    for (int i=0; i<n1; i++) {
      nagad_a1w_ir_register_variable(&a[i]);
    }
  
    // Call the AD routine
    ifail = 0;
    e01aa_a1w_f_(ad_handle,a,b,c,n1,n2,n,x,ifail);
      
      cout.setf(ios::scientific,ios::floatfield);
    cout.precision(5);
    cout << "\n Interpolation point = ";
    cout.width(12);cout << xr << endl;
    cout << "\n Function value at interpolation point = ";
    cout.width(12);cout << nagad_a1w_get_value(c[n2-1]) << endl;

    // Setup evaluation of derivatives via adjoints.
    double inc = 1.0;
    nagad_a1w_inc_derivative(&c[n2-1],inc);
    
    ifail = 0;
    nagad_a1w_ir_interpret_adjoint_sparse(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. x values:\n";
    cout << "  j         d/da(j)\n";
    for (int j=0; j < n1; j++) {
      double dx = nagad_a1w_get_derivative(a[j]);
      cout.width(3); cout << j+1 << "     ";
      cout.width(12); cout << dx << endl;
    }
    
    // Remove computational data object and tape
    x10ab_a1w_f_(ad_handle,ifail);
    nagad_a1w_ir_remove();
  }
  return exit_status;
}