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

NAG AD Library Introduction
Example description
/* D01FC_P0W_F C++ Header Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 * Mark 29.2, 2023.
 */

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

int main()
{
  // Scalars
  int     exit_status = 0;
  Integer ndim = 4, maxpts = 4000, lenwrk = 100;

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

  // Allocate memory
  double *a = 0, *b = 0, *work = 0;

  a    = new double[ndim];
  b    = new double[ndim];
  work = new double[lenwrk];

  // Initialize variables
  for (int i = 0; i < ndim; i++)
  {
    a[i] = 0.0;
    b[i] = 1.0;
  }
  Integer minpts = 0;
  double  eps    = 0.0001;
  double  ruser[3];
  ruser[0] = 4.0;
  ruser[1] = 2.0;
  ruser[2] = 1.0;

  // Call the passive routine
  nag::ad::handle_t ad_handle;
  Integer           ifail = 0;
  double            finval, acc;

  auto f = [&](nag::ad::handle_t &     ad_handle,
            const Integer &         ndim,
            const double *z,
            double &     fz)
          {
            // dco/c++ overloading used here to perform AD
            double f1, f2, f3;

            f1 = ruser[0] * z[0] * z[2] * z[2];
            f2 = ruser[1] * z[0] * z[2];
            f2 = exp(f2);
            f3 = ruser[2] + z[1] + z[3];
            f3 = f3 * f3;
            fz = f1 * f2 / f3;
          };

  nag::ad::d01fc(ad_handle, ndim, a, b, minpts, maxpts, f, eps, acc, lenwrk,
                 work, finval, ifail);

  cout.setf(ios::scientific, ios::floatfield);
  cout.setf(ios::right);
  cout.precision(4);
  cout << "\n Solution, x = ";
  cout.width(12);
  cout << finval << endl;

  delete[] a;
  delete[] b;
  delete[] work;
  return exit_status;
}