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

NAG AD Library Introduction
Example description
/* nag::ad::c05ay Passive Example Program.
 */

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

// Function which calls NAG AD Library routines.
// Finds the zero point f(xv) = 0 for the function
// f(x) =  exp(-x) - r*x.
template <typename T> void func(T r, T &x);

// The function f
template <typename T>
void NAG_CALL f(void *&ad_handle, const T &x, T &z, Integer iuser[], T ruser[]);

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

  // Set problem parameters
  double rv = 2.0;
  // Solution x
  double xv;

  // Call the NAG AD Lib functions
  func(rv, xv);

  // Print outputs
  std::cout.setf(std::ios::scientific, std::ios::floatfield);
  std::cout.precision(12);
  std::cout << "\n Solution:\n";
  std::cout << " x = " << xv << std::endl;

  return 0;
}

// Function which calls NAG AD Library routines.
template <typename T> void func(T r, T &x)
{
  // Active variables
  T a = 0.0, b = 1.0;
  T eps = 1.0e-5, eta = 0.0;
  // Create AD configuration data object
  Integer ifail     = 0;
  void *  ad_handle = 0;
  nag::ad::x10aa(ad_handle, ifail);
  // Call routine for computing an approximation to a simple zero of the
  // function f
  ifail = 0;
  nag::ad::c05ay(ad_handle, a, b, eps, eta, f<T>, x, 0, nullptr, 1, &r, ifail);
  // Remove computational data object
  ifail = 0;
  nag::ad::x10ab(ad_handle, ifail);
}

// The function f
template <typename T>
void NAG_CALL f(void *&ad_handle, const T &x, T &z, Integer iuser[], T ruser[])
{
  T r = ruser[0];
  // Evaluate nonlinear function
  z = exp(-x) - x * r;
}