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

NAG AD Library Introduction
Example description
/* D02BJ_T1W_F C++ Header Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.2, 2021.
 *
 */

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

#ifdef __cplusplus
extern "C"
{
#endif
  static void NAG_CALL fcn(void *&                  ad_handle,
                           const nagad_t1w_w_rtype &x,
                           const nagad_t1w_w_rtype  y[],
                           nagad_t1w_w_rtype        f[],
                           Integer                  iuser[],
                           nagad_t1w_w_rtype        ruser[]);
  static void NAG_CALL g(void *&                  ad_handle,
                         const nagad_t1w_w_rtype &x,
                         const nagad_t1w_w_rtype  y[],
                         nagad_t1w_w_rtype &      retval,
                         Integer                  iuser[],
                         nagad_t1w_w_rtype        ruser[]);
#ifdef __cplusplus
}
#endif

int main(void)
{
  int           exit_status = 0;
  const Integer n           = 3;

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

  Integer            iw, iuser[1];
  double             pi, dr[5];
  nagad_t1w_w_rtype  tol, x, xinit, xend, ruser[2];
  nagad_t1w_w_rtype *y = 0, *yinit = 0, *w = 0;

  iw    = 20 * n;
  w     = new nagad_t1w_w_rtype[iw];
  y     = new nagad_t1w_w_rtype[n];
  yinit = new nagad_t1w_w_rtype[n];

  xinit = 0.0;
  xend  = 10.0;
  pi    = nag_math_pi;

  cout << "\n\nCase: no intermediate output, root-finding\n";
  const double tolr = 1.0e-5;
  tol               = tolr;
  cout << "\n  Calculation with tol = " << tolr << endl;

  // Create AD configuration data object
  Integer ifail     = 0;
  void *  ad_handle = 0;
  nag::ad::x10aa(ad_handle, ifail);

  yinit[0] = 0.5;
  yinit[1] = 0.5;
  yinit[2] = 0.2 * pi;

  const double alpha = -0.032;
  const double beta  = -0.02;
  ruser[0]           = alpha;
  ruser[1]           = beta;

  for (int i = 0; i < 5; ++i)
    {
      double inc = 1.0;
      if (i < 2)
        {
          dco::derivative(ruser[i]) = inc;
        }
      else
        {
          dco::derivative(yinit[i - 2]) = inc;
        }
      y[0] = yinit[0];
      y[1] = yinit[1];
      y[2] = yinit[2];
      x    = xinit;

      ifail = 0;
      nag::ad::d02bj(ad_handle, x, xend, n, y, fcn, tol, "D", nullptr, g,
                     w, -1, iuser, -1, ruser, ifail);

      dr[i]       = dco::derivative(x);
      double zero = 0.0;
      if (i < 2)
        {
          dco::derivative(ruser[i]) = zero;
        }
      else
        {
          dco::derivative(yinit[i - 2]) = zero;
        }
    }

  cout.setf(ios::fixed);
  cout.setf(ios::right);
  cout.precision(3);
  cout << "\n  Root of Y(1) = 0.0 at ";
  cout.width(5);
  cout << x << endl;
  cout << "\n  Solution is  ";
  cout.precision(4);
  for (int i = 0; i < 3; ++i)
    {
      cout.width(10);
      cout << y[i];
    }
  cout << endl;

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

  // Get derivatives
  cout << "\n Derivatives: (hit point w.r.t. parameters)\n";
  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(5);
  cout << "     dx/dg      = ";
  cout.width(12);
  cout << -dr[0] << endl;
  cout << "     dx/ddrag   = ";
  cout.width(12);
  cout << -dr[1] << endl;
  cout << "     dx/dheight = ";
  cout.width(12);
  cout << dr[2] << endl;
  cout << "     dx/dvel    = ";
  cout.width(12);
  cout << dr[3] << endl;
  cout << "     dx/dangle  = ";
  cout.width(12);
  cout << dr[4] << endl;

  nag::ad::x10ab(ad_handle, ifail);

  delete[] w;
  delete[] y;
  delete[] yinit;
  return exit_status;
}

static void NAG_CALL fcn(void *&                  ad_handle,
                         const nagad_t1w_w_rtype &x,
                         const nagad_t1w_w_rtype  y[],
                         nagad_t1w_w_rtype        f[],
                         Integer                  iuser[],
                         nagad_t1w_w_rtype        ruser[])
{
  nagad_t1w_w_rtype alpha, beta;
  alpha = ruser[0];
  beta  = ruser[1];

  f[0] = tan(y[2]);
  f[1] = alpha * tan(y[2]) / y[1] + beta * y[1] / cos(y[2]);
  f[2] = alpha / (y[1] * y[1]);
}

static void NAG_CALL g(void *&                  ad_handle,
                       const nagad_t1w_w_rtype &x,
                       const nagad_t1w_w_rtype  y[],
                       nagad_t1w_w_rtype &      retval,
                       Integer                  iuser[],
                       nagad_t1w_w_rtype        ruser[])
{
  retval = y[0];
}