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

NAG AD Library Introduction
Example description
/* D01RM_P0W_F C++ Header Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 * Mark 28.7, 2022.
 */

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

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

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

  double bound, epsabs, epsrel;
  bound  = 0.0;
  epsabs = 0.0;
  epsrel = 1.0e-4;

  Integer  inf    = 1;
  Integer  maxsub = 20;
  Integer  lrinfo = 80;
  Integer  liinfo = 20;
  double * rinfo  = 0;
  Integer *iinfo  = 0;

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

  nag::ad::handle_t ad_handle;
  double            inc = 1.0;
  double            result, abserr, ruser[22];

  for (int i = 0; i < 20; ++i)
  {
    ruser[i] = 0.0;
  }
  ruser[20] = 1.0;
  ruser[21] = 1.0;

  auto f = [&](nag::ad::handle_t &     ad_handle,
            const double *x,
            const Integer &         nx,
            double *fv,
            Integer &               iflag)
          {
            // dco/c++ used here to perform AD of the following
            for (int i = 0; i < nx; i++)
            {
              fv[i] = 1.0 / ((x[i] + ruser[20]) * sqrt(ruser[21] * x[i]));
            }
          };

  // Call the passive routine
  Integer ifail = -1;
  nag::ad::d01rm(ad_handle, f, bound, inf, epsabs, epsrel, maxsub, result,
                 abserr, rinfo, iinfo, ifail);
  if (ifail < 0)
  {
    cout << "\n ** nag::ad::d01rm failed error exit ifail = " << ifail << endl;
    goto END;
  }
  // Print inputs and primal outputs.
  cout << "\n lower limit of integration (bound) = " << bound << endl;
  cout << " upper limit of integration (Inf)   = "
       << "Infinity" << 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;
}