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

extern "C"
{
  static void NAG_CALL f(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;

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

  // The example function can raise various exceptions - it contains
  // a division by zero and a log singularity - although its integral
  // is well behaved.

  Integer exmode[3], exmode_old[3];
  nag_get_ieee_exception_mode(exmode_old);
  // Save the original halting mode.

  // Turn exception halting mode off for the three common exceptions.
  for (int i=0; i<3; i++) {
    exmode[i] = 0;
  }
  nag_set_ieee_exception_mode(exmode);
  
  // Skip first line of data file
  string mystr;
  getline (cin, mystr);

  // Read problem parameters
  double            ar, br, epsabsr, epsrelr;
  cin >> ar;
  cin >> br;
  cin >> epsabsr;
  cin >> epsrelr;

  nagad_a1w_w_rtype a, b, epsabs, epsrel;
  a = ar; b = br; epsabs = epsabsr; epsrel = epsrelr;

  // 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(&a);
  
  // Call the AD routine
  nagad_a1w_w_rtype dinest, errest, ruser[1];
  Integer           nevals, iuser[1];
  double            inc = 1.0;
  ifail = -1;
  d01rg_a1w_f_(ad_handle,a,b,f,epsabs,epsrel,dinest,errest,nevals,
               iuser,ruser,ifail);
  if (ifail<0) {
    cout << "\n ** d01rg_a1w_f_ failed error exit ifail = " << ifail << endl;
    goto END;
  }
  // Print inputs and primal outputs.
  cout << "\n lower limit of integration (a) = " << ar << endl;
  cout << " upper limit of integration (b) = " << br << endl;
  cout << " absolute accuracy requested    = " << epsabsr << endl;
  cout << " relative accuracy requested    = " << epsrelr << endl;
  cout.setf(ios::scientific,ios::floatfield);
  cout.precision(4);
  if (ifail >= 0) {
    cout << "\n approximation to the integral  : " << nagad_a1w_get_value(dinest) << endl;
    cout << " estimate of the absolute error : " << nagad_a1w_get_value(errest) << endl;
    cout << " number of function evaluations : " << nevals << endl;
  }

  // Setup evaluation of derivatives via adjoints.
  nagad_a1w_inc_derivative(&dinest,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 Derivative of solution w.r.t to lower limit:\n";
  cout << " d/da(x) = " << nagad_a1w_get_derivative(a) << endl;

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

  // Restore the original halting mode
  nag_set_ieee_exception_mode(exmode_old);
  
  return exit_status;
}

static void NAG_CALL f(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++ used here to perform AD of the following
  nagad_a1w_w_rtype tmp1, tmp2;
  for (int i=0; i<nx; i++) {
    tmp1 = 10.0*(1.0-x[i]);
    tmp2 = sin(x[i])/x[i];
    fv[i] = tmp2*log(tmp1);
  }
  return;
}