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

NAG AD Library Introduction
Example description
/* D01BD_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 <nagx01.h>
#include <stdio.h>
#include <string>
using namespace std;

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

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

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

  // Skip first line of data file
  string mystr;
  getline(cin, mystr);

  // Read problem parameters
  double a, b, epsabs, epsrel;
  cin >> a;
  cin >> b;
  cin >> epsabs;
  cin >> epsrel;

  void *ad_handle = 0;

  double  result, abserr, ruser[1];
  Integer iuser[1];

  ruser[0] = 10.0;
  iuser[0] = 0;

  // Call the passive routine
  Integer ifail = 0;
  nag::ad::d01bd(ad_handle, f, a, b, epsabs, epsrel, result, abserr, -1, iuser,
                 -1, ruser, ifail);

  // Print inputs and primal outputs.
  cout << "\n lower limit of integration (a) = " << a << endl;
  cout << " upper limit of integration (b) = " << b << endl;
  cout << " absolute accuracy requested    = " << epsabs << endl;
  cout << " relative accuracy requested    = " << epsrel << endl;
  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(4);
  if (ifail >= 0)
    {
      cout << "\n approximation to the integral  : " << result << endl;
      cout << " estimate of the absolute error : " << abserr << endl;
      cout << " number of function evaluations : " << iuser[0] << endl;
    }

  return exit_status;
}

static void NAG_CALL f(void *&       ad_handle,
                       const double &x,
                       double &      ret,
                       Integer       iuser[],
                       double        ruser[])
{
  double pi = X01AAC;
  double prx, x2, s;
  x2  = x * x;
  prx = pi * ruser[0];
  prx = prx * x;
  s   = sin(prx);
  ret = x2 * s;
  iuser[0]++;

  return;
}