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

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

#include <dco.hpp>
#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_A1W_F C++ Header Example Program Results\n\n";

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

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

  // Create AD tape
  dco::ga1s<double>::global_tape = dco::ga1s<double>::tape_t::create();

  // Create AD configuration data object
  Integer           ifail = 0;
  nag::ad::handle_t ad_handle;

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

  /* Register variables to differentiate w.r.t. */
  dco::ga1s<double>::global_tape->register_variable(ruser[0]);
  dco::ga1s<double>::global_tape->register_variable(ruser[1]);
  dco::ga1s<double>::global_tape->register_variable(ruser[2]);

  // Call the AD routine
  ifail = 0;
  nagad_a1w_w_rtype finval, acc;
  
  auto f = [&](nag::ad::handle_t &     ad_handle,
            const Integer &         ndim,
            const nagad_a1w_w_rtype *z,
            nagad_a1w_w_rtype &     fz)
          {
            // dco/c++ overloading used here to perform AD
            nagad_a1w_w_rtype 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);

  double inc = 1.0;
  dco::derivative(finval) += inc;
  dco::ga1s<double>::global_tape->sparse_interpret() = true;
  dco::ga1s<double>::global_tape->interpret_adjoint();

  cout << "\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n";

  // Get derivatives
  double dr[3];
  dr[0] = dco::derivative(ruser[0]);
  dr[1] = dco::derivative(ruser[1]);
  dr[2] = dco::derivative(ruser[2]);

  cout.setf(ios::scientific, ios::floatfield);
  cout.setf(ios::right);
  cout.precision(4);
  cout << "\n Solution, x = ";
  double ans_value = dco::value(finval);
  cout.width(12);
  cout << ans_value << endl;
  cout << "\n Derivatives:\n";
  cout << " d/druser[0] = ";
  cout.width(12);
  cout << dr[0] << endl;
  cout << " d/druser[1] = ";
  cout.width(12);
  cout << dr[1] << endl;
  cout << " d/druser[2] = ";
  cout.width(12);
  cout << dr[2] << endl;

  dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);

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