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

NAG AD Library Introduction
Example description
/* nag::ad::e01be Adjoint Example Program.
 */

#include <dco.hpp>
#include <iostream>
#include <nagad.h>

// Function which calls NAG AD routines.
template <typename T> void func(const T &xint, T &fint, T &dint);

// Driver with the adjoint calls.
//  Evaluates a piecewise cubic Hermite interpolant and computes function value
//  and derivative at point xint.
// Also computes the sum of all Jacobian elements d(fint, dint)/d(xint).
void driver(const double &xint, double &fint, double &dint, double &d);

int main()
{
  std::cout << "nag::ad::e01be Adjoint Example Program Results\n\n";

  // Point to interpolate at
  double xint = 8.4;
  // Interpolated function and derivative value
  double fint, dint;

  // Sum of Jacobian elements
  double d;

  // Call driver
  driver(xint, fint, dint, d);

  // Print outputs
  std::cout << " Derivatives calculated: First order adjoints\n";
  std::cout << " Computational mode    : algorithmic\n";

  std::cout.setf(std::ios::scientific, std::ios::floatfield);
  std::cout.precision(6);
  std::cout << "\n fint = " << fint << std::endl;
  std::cout << " dint = " << dint << std::endl;

  // Print derivative
  std::cout
      << "\n Sum of all Jacobian elements of (fint, dint) w.r.t. interpolation point xint and data x and f :\n";
  std::cout << " sum_i [d(fint, dint) / d(xint)]_i = " << d << std::endl;

  return 0;
}

// Driver with the adjoint calls.
//  Evaluates a piecewise cubic Hermite interpolant and computes function value
//  and derivative at point xint.
// Also computes the sum of all Jacobian elements d(fint, dint)/d(xint).
void driver(const double &xintv, double &fintv, double &dintv, double &d)
{
  using mode = dco::ga1s<double>;
  using T    = mode::type;

  // Create the AD tape
  mode::global_tape = mode::tape_t::create();

  // Variable to differentiate w.r.t.
  T xint = xintv;

  // Register variable to differentiate w.r.t.
  mode::global_tape->register_variable(xint);

  // Interpolated function and derivative value
  T fint, dint;

  // Call the NAG AD Lib functions
  func(xint, fint, dint);

  // Extract the computed solutions
  fintv = dco::passive_value(fint);
  dintv = dco::passive_value(dint);
  // Initialize adjoints of y
  mode::global_tape->register_output_variable(fint);
  mode::global_tape->register_output_variable(dint);
  dco::derivative(fint) = 1.0;
  dco::derivative(dint) = 1.0;

  // Interpret the tape
  mode::global_tape->interpret_adjoint();

  // Adjoint of xint
  d = dco::derivative(xint);

  // Remove tape
  mode::tape_t::remove(mode::global_tape);
}

// function which calls NAG AD Library routines
template <typename T> void func(const T &xint, T &fint, T &dint)
{

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

  // Initialize abscissa and ordinate data
  std::vector<T> x{7.99, 8.09, 8.19, 8.70, 9.20, 10.00, 12.00, 15.00, 20.00};
  std::vector<T> f{0.00000E+0, 0.27643E-4, 0.43750E-1, 0.16918E+0, 0.46943E+0,
                   0.94374E+0, 0.99864E+0, 0.99992E+0, 0.99999E+0};

  // Local output of e01be; approximation of derivatives at points x
  std::vector<T> d(x.size());
  ifail = 0;
  nag::ad::e01be(ad_handle, x.size(), x.data(), f.data(), d.data(), ifail);

  // Interpolate at point x
  ifail = 0;
  nag::ad::e01bg(ad_handle, x.size(), x.data(), f.data(), d.data(), 1, &xint,
                 &fint, &dint, ifail);

  ifail = 0;
}