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

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

#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 n           = 21;
  double  ar          = 1.0;

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

  // Allocate memory
  nagad_t1w_w_rtype *x = 0, *y = 0;

  x = new nagad_t1w_w_rtype[n];
  y = new nagad_t1w_w_rtype[n];

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

  nagad_t1w_w_rtype a;
  a = ar;

  double inc         = 1.0;
  dco::derivative(a) = inc;

  // Initialize variables
  for (int i = 0; i < n; i++)
  {
    double            xr = (1.0 * i) / (1.0 * (n - 1));
    nagad_t1w_w_rtype tmp;
    x[i] = xr;
    tmp  = a * x[i];
    y[i] = 4.0 / (1.0 + tmp * tmp);
  }

  // Call the AD routine
  ifail = 0;
  nagad_t1w_w_rtype ans, er;

  nag::ad::d01ga(ad_handle, x, y, n, ans, er, ifail);

  double da;
  da = dco::derivative(ans);

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

  cout.setf(ios::scientific, ios::floatfield);
  cout.setf(ios::right);
  cout.precision(4);
  cout << "\n Solution, ans = ";
  double ans_value = dco::value(ans);
  cout.width(12);
  cout << ans_value << endl;
  cout << "\n Derivative:\n";
  cout << " d(ans)/da = ";
  cout.width(12);
  cout << da << endl;

  delete[] x;
  delete[] y;
  return exit_status;
}