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

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

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

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

  cout << "D01RG_P0W_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 a, b, epsabs, epsrel;
  cin >> a;
  cin >> b;
  cin >> epsabs;
  cin >> epsrel;

  // Call the passive routine
  Integer ifail     = 0;
  void *  ad_handle = 0;
  double  dinest, errest, ruser[1];
  Integer nevals, iuser[1];
  ifail = -1;
  nag::ad::d01rg(ad_handle, a, b, f, epsabs, epsrel, dinest, errest, nevals, -1,
                 iuser, -1, ruser, ifail);
  if (ifail < 0)
    {
      cout << "\n ** nag::ad::d01rg failed error exit ifail = " << ifail
           << endl;
      goto END;
    }
  // 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  : " << dinest << endl;
      cout << " estimate of the absolute error : " << errest << endl;
      cout << " number of function evaluations : " << nevals << endl;
    }

END:

  // Restore the original halting mode
  nag_set_ieee_exception_mode(exmode_old);

  return exit_status;
}

static void NAG_CALL f(void *&        ad_handle,
                       const double   x[],
                       const Integer &nx,
                       double         fv[],
                       Integer &      iflag,
                       Integer        iuser[],
                       double         ruser[])
{
  double 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;
}