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

NAG AD Library Introduction
Example description
/* D01RL_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 f(void *&        ad_handle,
                         const double   x[],
                         const Integer &nx,
                         double         fv[],
                         Integer &      iflag,
                         Integer        iuser[],
                         double         ruser[],
                         const void *&  cpuser);
}

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

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

  double a, b, epsabs, epsrel;
  a      = 0.0;
  b      = 1.0;
  epsabs = 0.0;
  epsrel = 1.0e-4;

  Integer  npts   = 1;
  Integer  maxsub = 20;
  Integer  lrinfo = 80 + npts + 6;
  Integer  liinfo = 40 + npts + 4;
  double * rinfo  = 0;
  Integer *iinfo  = 0;

  rinfo = new double[lrinfo];
  iinfo = new Integer[liinfo];

  void *      ad_handle = 0;
  double      result, abserr, ruser[20], points[1];
  Integer     iuser[1];
  const void *cpuser = 0;
  points[0]          = 1.0 / 7.0;
  iuser[0]           = 0;
  for (int i = 0; i < 20; ++i)
    {
      ruser[i] = 0.0;
    }

  // Call the passive routine

  Integer ifail = -1;
  nag::ad::d01rl(ad_handle, f, a, b, npts, points, epsabs, epsrel, maxsub,
                 result, abserr, rinfo, iinfo, -1, iuser, -1, ruser, cpuser,
                 ifail);
  if (ifail < 0)
    {
      cout << "\n ** nag::ad::d01rl 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 << " given break point (points[0])  = " << points[0] << endl;
  cout << " absolute accuracy requested    = " << epsabs << endl;
  cout << " relative accuracy requested    = " << epsrel << endl;
  cout << " maximum number of subintervals = " << maxsub << 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 : " << iinfo[0] << endl;
    }

END:

  delete[] rinfo;
  delete[] iinfo;
  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[],
                       const void *&  cpuser)
{
  iflag = 0;
  for (int i = 0; i < nx; i++)
    {
      fv[i] = x[i] - 1.0 / 7.0;
      if (fv[i] == 0.0)
        {
          ruser[iflag] = x[i];
          iflag++;
        }
      else if (fv[i] < 0.0)
        {
          fv[i] = -fv[i];
        }
    }
  iuser[0] = iflag;
  if (iflag == 0)
    {
      for (int i = 0; i < nx; i++)
        {
          fv[i] = 1.0 / sqrt(fv[i]);
        }
    }
  else
    {
      iflag = -iflag;
    }
  return;
}