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

NAG AD Library Introduction
Example description
/* D01PA_P0W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.1, 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;
  Integer mxord = 5, ndim = 3, sdvert = 8, ldvert = 4;

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

  // Allocate memory
  double *finvls = 0, *vert = 0, *vert_in = 0;
  finvls = new double[mxord];
  vert   = new double[ldvert * sdvert];

  for (int i = 0; i < ndim * ldvert; i++)
  {
    vert[i] = 0.0;
  }
  for (int i = 1; i < ndim * ldvert; i = i + ldvert + 1)
  {
    vert[i] = 1.0;
  }

  nag::ad::handle_t ad_handle;

  cout << "\n Maxord   Estimated      Estimated         Integrand\n";
  cout << "           value         accuracy        evaluations\n";
  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(4);
  Integer minord = 0, nevals = 1;
  auto functn = [&](nag::ad::handle_t &     ad_handle,
                  const Integer &         ndim,
                  const double *x,
                  double &     ret)
                {
                  double tmp1;
                  // dco/c++ used here to perform AD of the following
                  tmp1 = x[0] + x[1] + x[2];
                  ret  = exp(tmp1) * cos(tmp1);
                };

  for (Integer maxord = 1; maxord <= mxord; maxord++)
  {

    // Call the passive routine
    double  esterr;
    Integer ifail = 0;

    nag::ad::d01pa(ad_handle, ndim, vert, ldvert, sdvert, functn, minord,
                   maxord, finvls, esterr, ifail);
    double finv_r = finvls[maxord - 1];
    cout.width(5);
    cout << maxord;
    cout.width(15);
    cout << finv_r;
    cout.width(15);
    cout << esterr;
    cout.width(12);
    cout << nevals << endl;

    nevals = (nevals * (maxord + ndim + 1)) / maxord;
  }

  cout.setf(ios::right);
  cout.precision(4);
  cout << "\n Solution, I = ";
  double ans_value = finvls[mxord - 1];
  cout.width(12);
  cout << ans_value << endl;

  delete[] finvls;
  delete[] vert;
  return exit_status;
}