NAG Library Manual, Mark 27.2
Interfaces:  FL   CL   CPP   AD 

NAG AD Library Introduction
Example description
/* D01FB_P0W_F C++ Header Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 * Mark 27.2, 2021.
 */

#include <iostream>
#include <math.h>
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
using namespace std;

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

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

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

  // Allocate memory
  Integer *nptvec = 0;
  double * 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 double[lwa];
  weight = new double[lwa];

  void *ad_handle = 0;

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

      Integer ifail = 0, quadtype = 0;
      double  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;
        }
      nag::ad::d01tb(ad_handle, quadtype, a, b, nptvec[i], &weight[j],
                     &abscis[j], ifail);
      j = j + nptvec[i];
    }

  // Call the passive routine
  Integer ifail = 0;
  double  ans;
  double  ruser[1];
  Integer iuser[1];
  nag::ad::d01fb(ad_handle, ndim, nptvec, lwa, weight, abscis, fun, ans, -1,
                 iuser, -1, ruser, ifail);

  cout.setf(ios::right);
  cout.precision(4);
  cout << "\n Solution, x = ";
  cout.width(12);
  cout << ans << endl;

  delete[] nptvec;
  delete[] abscis;
  delete[] weight;
  return exit_status;
}
static void NAG_CALL fun(void *&        ad_handle,
                         const Integer &ndim,
                         const double   x[],
                         double &       ret,
                         Integer        iuser[],
                         double         ruser[])
{
  double p1 = 6.0, p2 = 8.0;
  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]);
  return;
}