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

extern "C"
{
  static void NAG_CALL f_a1w(void* &ad_handle,
                             const nagad_a1w_w_rtype x[],
                             const Integer& nx,
                             nagad_a1w_w_rtype fv[],
                             Integer& iflag,
                             Integer iuser[],
                             nagad_a1w_w_rtype ruser[]);
}

int main(void)
{
  // Scalars
  int               exit_status = 0;
  Integer           n = 64;
  // Arrays
  nagad_a1w_w_rtype ruser[1];
  Integer           iuser[1];

  ruser[0] = 1.0;

  cout << "D01UA_A1W_F C++ Header Example Program Results\n\n";

  // 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.
  nagad_a1w_ir_register_variable(&ruser[0]);

  cout << "\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n";

  nagad_a1w_w_rtype a, b;
  Integer           key;
  for (int funid = 1; funid <= 6; funid++) {
    if (funid==1) {
      a = 0.0;
      b = 1.0;
      key = 0;
    }
    if (funid==2) {
      a = 2.0;
      b = 0.0;
      key = -5;
    }
    if (funid==3) {
      a = 2.0;
      b = 1.0;
      key = -3;
    }
    if (funid==4) {
      a = 2.0;
      b = 1.0;
      key = 3;
    }
    if (funid==5) {
      a = -1.0;
      b = 3.0;
      key = -4;
    }
    if (funid==6) {
      a = -1.0;
      b = 3.0;
      key = 4;
    }

    iuser[0] = funid;

    // Call the AD routine
    ifail = 0;
    nagad_a1w_w_rtype dinest;
    d01ua_a1w_f_(ad_handle,key,a,b,n,f_a1w,dinest,iuser,ruser,ifail);

    nagad_a1w_ir_zero_adjoints();
    double inc = 1.0;
    nagad_a1w_inc_derivative(&dinest,inc);
    nagad_a1w_ir_interpret_adjoint(ifail);

    // Get derivatives
    double dr;
    dr = nagad_a1w_get_derivative(ruser[0]);

    cout.setf(ios::scientific,ios::floatfield);
    cout.setf(ios::right);
    cout.precision(4);
    cout << "\n dinest = ";
    double dinest_value = nagad_a1w_get_value(dinest);
    cout.width(12); cout << dinest_value << "     ";
    cout <<  " d/druser[0] = ";
    cout.width(12); cout << dr << endl;
  }

  // Remove computational data object and tape
  x10ab_a1w_f_(ad_handle,ifail);
  nagad_a1w_ir_remove();

  return exit_status;
}

static void NAG_CALL f_a1w(void* &ad_handle,
                           const nagad_a1w_w_rtype x[],
                           const Integer& nx,
                           nagad_a1w_w_rtype fv[],
                           Integer& iflag,
                           Integer iuser[],
                           nagad_a1w_w_rtype ruser[])
{
  // dco/c++ overloading used here to perform AD
  Integer i;
  nagad_a1w_w_rtype xx, fx, rux;
  for (i=0; i<nx && iflag>=0; i++) {
    xx = x[i];
    rux = ruser[0]*xx;
    if (iuser[0]==1) {
      fx = 4.0/(1.0+ruser[0]*xx*xx);
    }
    if (iuser[0]==2) {
      fx = 1.0/(xx*xx*log(ruser[0]*xx));
    }
    if (iuser[0]==3) {
      fx = exp(-ruser[0]*xx)/xx;
    }
    if (iuser[0]==4) {
      fx = ruser[0]/xx;
    }
    if (iuser[0]==5) {
      fx = exp(-3.0*ruser[0]*xx*xx-4.0*xx-1.0);
    }
    if (iuser[0]==6) {
      fx = exp(2.0*ruser[0]*xx+2.0);
    }
    if (iuser[0]<0 || iuser[0]>6) {
      iflag = -1;
    }
    fv[i] = fx;
  }

  return;
}