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

NAG AD Library Introduction
Example description
/* D01RK_P0W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.2, 2024.
 */

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

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

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

  Integer key = 6;
  double  pi  = X01AAC;
  double  a, b, epsabs, epsrel;
  a      = 0.0;
  b      = 2.0 * pi;
  epsabs = 0.0;
  epsrel = 1.0e-4;

  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            result, abserr, ruser[2];
  Integer           iuser[1];
  iuser[0]                 = 0;
  ruser[0]                 = 30.0;
  ruser[1]                 = 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] = x[i] * sin(ruser[0] * x[i]) * cos(ruser[1] * x[i]);
            }
          };
  // Call the passive routine
  Integer ifail = -1;
  nag::ad::d01rk(ad_handle, f, a, b, key, epsabs, epsrel, maxsub, result,
                 abserr, rinfo, iinfo, ifail);
  if (ifail < 0)
  {
    cout << "\n ** nag::ad::d01rk 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 << " choice of Gaussian rule (key)  = " << key << 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;
}