Example description
/* D01TB_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 <iostream>
using namespace std;

extern "C"
{
  static void NAG_CALL fun(void* &ad_handle,
                           const Integer& ndim,
                           const nagad_a1w_w_rtype x[],
                           nagad_a1w_w_rtype& ret,
                           Integer iuser[],
                           nagad_a1w_w_rtype ruser[]);
}

int main(void)
{
  // Scalars
  int               exit_status = 0;
  Integer           ndim = 4;

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

  // Allocate memory
  Integer           *nptvec = 0;
  nagad_a1w_w_rtype *abscis = 0, *weight = 0;
  Integer           lwa = 0;

  nptvec = new Integer [ndim];
  for (int i=0;i<ndim;i++) {
    nptvec[i] = 4;
    lwa = lwa + nptvec[i];
  }
  abscis = new nagad_a1w_w_rtype [lwa];
  weight = new nagad_a1w_w_rtype [lwa];

  // 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);

  // Evaluate primal weights and abscisae in each Dimension
  int j = 0;
  for (int i=0;i<ndim;i++) {

    Integer ifail = 0, quadtype = 0;
    nagad_a1w_w_rtype a, b;
    switch (i) {
    case 0:
      a = 1.0;
      b = 2.0;
      quadtype = 0;
      break;
    case 1:
        a = 0.0;
      b = 2.0;
      quadtype = -3;
      break;
    case 2:
      a = 0.0;
      b = 0.5;
      quadtype = -4;
      break;
    case 3:
      a = 1.0;
      b = 2.0;
      quadtype = -5;
      break;
    }
    d01tb_a1w_f_(ad_handle,quadtype,a,b,nptvec[i],&weight[j],&abscis[j],ifail);
    j = j + nptvec[i];
  }

  /* Register variables to differentiate w.r.t. */
  for (int i = 0; i < lwa; i++) {
    nagad_a1w_ir_register_variable(&weight[i]);
    nagad_a1w_ir_register_variable(&abscis[i]);
  }
    
  // Call the AD routine
  ifail = 0;
  nagad_a1w_w_rtype ans;
  nagad_a1w_w_rtype ruser[1];
  Integer           iuser[1];
  d01fb_a1w_f_(ad_handle,ndim,nptvec,lwa,weight,abscis,fun,ans,iuser,ruser,ifail);

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

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

  // Get derivatives
  cout.setf(ios::right);
  cout.precision(4);
  cout << "\n Solution, x = ";
  double ans_value = nagad_a1w_get_value(ans);
  cout.width(12); cout << ans_value << endl;
  cout << " Derivatives:\n";
  cout << " dim   j  d/dweight    d/dabscis\n";
  
  cout.setf(ios::scientific,ios::floatfield);

  j = -1;
  for (int i = 0; i< ndim; i++) {
    j = j + 1;
    double w = nagad_a1w_get_derivative(weight[j]);
    double a = nagad_a1w_get_derivative(abscis[j]);
      
    int k = 1;
    cout.width(4); cout << i;
    cout.width(4); cout << k;
    cout.width(12); cout << w;
    cout.width(12); cout << a << endl;
    for (k = 2; k <= nptvec[i]; k++) {
      j = j + 1;
      double w = nagad_a1w_get_derivative(weight[j]);
      double a = nagad_a1w_get_derivative(abscis[j]);
      cout.width(8); cout << k;
      cout.width(12); cout << w;
      cout.width(12); cout << a << endl;
    }
  }

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

  delete [] nptvec;
  delete [] abscis;
  delete [] weight;
  return exit_status;
}

static void NAG_CALL fun(void* &ad_handle,
                             const Integer& ndim,
                             const nagad_a1w_w_rtype x[],
                             nagad_a1w_w_rtype& ret,
                             Integer iuser[],
                             nagad_a1w_w_rtype ruser[])
{
  // dco/c++ overloading used here to perform AD
  double    p1 = 6.0, p2 = 8.0;
  nagad_a1w_w_rtype r1, r2;
  // Split the following function into manageable chunks
  // ret = (pow(x[0]*x[1]*x[2],p1)/pow(x[3]+2.0,p2))*
  //       exp(-2.0*x[1]-0.5*x[2]*x[2]);
  r1 = x[2]*x[2];
  r1 = 0.5*r1;
  r2 = -2.0*x[1];
  r1 = r2 - r1;
  ret = exp(r1);
  r1 = x[0]*x[1]*x[2];
  r1 = pow(r1,p1);
  r2 = x[3] + 2.0;
  r2 = pow(r2,p2);
  r2 = r1/r2;
  ret = ret*r2;
  return;
}