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

NAG AD Library Introduction
Example description
/* G01GC_T1W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.3, 2024.
 */

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

int main()
{
  int exit_status = 0;

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

  // Skip heading in data file
  string mystr;
  getline(cin, mystr);

  // Read number of x values
  Integer n;
  cin >> n;

  cout << "    x       df      rlamda     p";
  cout << "          p'         df'     rlamda'\n";
  cout.setf(ios::scientific, ios::floatfield);
  cout.setf(ios::right);
  cout.precision(2);

  Integer           maxit = 100;
  nagad_t1w_w_rtype p, x, df, rlamda, tol;
  tol = 0.0;

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

  // Loop over x values
  for (Integer i = 0; i < n; ++i)
  {
    // Read next x, df, rlamda
    double xr, dfr, rr;
    cin >> xr >> dfr >> rr;

    x                  = xr;
    df                 = dfr;
    rlamda             = rr;
    double inc         = 1.0;
    dco::derivative(x) = inc;
    // Call NAG AD Routine
    p     = 0.0;
    ifail = 0;
    nag::ad::g01gc(ad_handle, x, df, rlamda, tol, maxit, p, ifail);

    // Get derivatives
    double dpdx;
    dpdx = dco::derivative(p);

    x                   = xr;
    df                  = dfr;
    rlamda              = rr;
    dco::derivative(df) = inc;
    // Call NAG AD Routine
    p     = 0.0;
    ifail = 0;
    nag::ad::g01gc(ad_handle, x, df, rlamda, tol, maxit, p, ifail);
    double dpdd;
    dpdd = dco::derivative(p);

    x                       = xr;
    df                      = dfr;
    rlamda                  = rr;
    dco::derivative(rlamda) = inc;
    // Call NAG AD Routine
    p     = 0.0;
    ifail = 0;
    nag::ad::g01gc(ad_handle, x, df, rlamda, tol, maxit, p, ifail);
    double dpdr;
    dpdr = dco::derivative(p);

    // Output results
    cout.width(9);
    cout << dco::value(x);
    cout.width(9);
    cout << dco::value(df);
    cout.width(9);
    cout << dco::value(rlamda);
    cout.width(9);
    cout << dco::value(p);
    cout.width(11);
    cout << dpdx;
    cout.width(11);
    cout << dpdd;
    cout.width(11);
    cout << dpdr << endl;
  }

  return exit_status;
}