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

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

#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;

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

  // The example function can raise various exceptions - it contains
  // a division by zero and a log singularity - although its integral
  // is well behaved.

  Integer exmode[3], exmode_old[3];
  nag_get_ieee_exception_mode(exmode_old);
  // Save the original halting mode.

  // Turn exception halting mode off for the three common exceptions.
  for (int i = 0; i < 3; i++)
  {
    exmode[i] = 0;
  }
  nag_set_ieee_exception_mode(exmode);

  double            pi = X01AAC;
  nagad_t1w_w_rtype 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;
  nagad_t1w_w_rtype *rinfo  = 0;
  Integer *          iinfo  = 0;

  rinfo = new nagad_t1w_w_rtype[lrinfo];
  iinfo = new Integer[liinfo];

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

  double            inc = 1.0, zero = 0.0;
  nagad_t1w_w_rtype result, abserr, ruser[2];
  Integer           iuser[1];
  iuser[0]                 = 0;
  ruser[0]                 = 4.0 * pi * pi;
  ruser[1]                 = 30.0;

  auto f = [&](nag::ad::handle_t &     ad_handle,
            const nagad_t1w_w_rtype *x,
            const Integer &         nx,
            nagad_t1w_w_rtype *fv,
            Integer &               iflag)
          {
            // dco/c++ used here to perform AD of the following
            nagad_t1w_w_rtype tmp1, tmp2;
            for (int i = 0; i < nx; i++)
            {
              if (x[i] == 1.0)
              {
                iflag    = -1;
                iuser[0] = iflag;
              }
              else
              {
                tmp1  = sqrt(1.0 - x[i] * x[i] / ruser[0]);
                tmp2  = x[i] * sin(ruser[1] * x[i]);
                fv[i] = tmp2 / tmp1;
              }
            }
          };

  // Call the AD routine with first active input derivative incremented
  dco::derivative(ruser[0]) = inc;
  ifail                     = -1;
  nag::ad::d01rj(ad_handle, f, a, b, epsabs, epsrel, maxsub, result, abserr,
                 rinfo, iinfo, ifail);
  dco::derivative(ruser[0]) = zero;
  if (ifail < 0)
  {
    cout << "\n ** nag::ad::d01rj failed error exit ifail = " << ifail << endl;
    goto END;
  }
  // Print inputs and primal outputs.
  cout << "\n lower limit of integration (a) = " << dco::value(a) << endl;
  cout << " upper limit of integration (b) = " << dco::value(b) << endl;
  cout << " absolute accuracy requested    = " << dco::value(epsabs) << endl;
  cout << " relative accuracy requested    = " << dco::value(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  : " << dco::value(result)
         << endl;
    cout << " estimate of the absolute error : " << dco::value(abserr) << endl;
    cout << " number of function evaluations : " << iinfo[0] << endl;
  }

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

  // Get derivatives
  double dr;
  dr = dco::derivative(result);
  cout << "\n Derivative of solution w.r.t to parameter in ruser:\n";
  cout << " dI/druser[0] = " << dr << endl;

  dco::derivative(ruser[1]) = inc;
  ifail                     = -1;
  nag::ad::d01rj(ad_handle, f, a, b, epsabs, epsrel, maxsub, result, abserr,
                 rinfo, iinfo, ifail);
  dr = dco::derivative(result);
  cout << " dI/druser[1] = " << dr << endl;

END:

  // Restore the original halting mode
  nag_set_ieee_exception_mode(exmode_old);

  delete[] rinfo;
  delete[] iinfo;
  return exit_status;
}