Example description
/* E04AB_A1W_F C++ Header Example Program.
 *
 * Copyright 2019 Numerical Algorithms Group.
 * Mark 27, 2019.
 */

#include <nag.h>
#include <dco.hpp>
#include <nagad.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string>



static void NAG_CALL funct(void * &ad_handle,
                           const nagad_a1w_w_rtype &xc,
                           nagad_a1w_w_rtype &fc,
                           Integer iuser[],
                           nagad_a1w_w_rtype ruser[])
{
  fc = sin(ruser[0]* xc) / xc;
}

/* funct */

int main(void)
{
  Integer exit_status = 0, max_fun;
  Integer ifail;
  nagad_a1w_w_rtype a, b, e1, e2, f, x;
  nagad_a1w_w_rtype ruser[1];
  Integer iuser[1];

  printf("E04AB_A1W_F C++ Header Example Program Results\n\n");

  // Create AD tape
  nagad_a1w_ir_create();
  void    *ad_handle = 0;
  x10aa_a1w_f_(ad_handle,ifail);


  /* For communication with user-supplied functions: */
  ruser[0] = 1.;
  /* e1 and e2 are set to zero so that nag_opt_one_var_func (e04abc) will
   *  reset them to their default values.
   */
  e1 = 0.0;
  e2 = 0.0;
  /* The minimum is known to lie in the range (3.5, 5.0) */
  a = 3.5;
  b = 5.0;
  /* Allow 30 calls of funct */
  max_fun = 30;
  /* nag_opt_one_var_func (e04abc).
   * Minimizes a function of one variable, using function values only.
   */
  nagad_a1w_ir_register_variable(&ruser[0]);

  e04ab_a1w_f_(ad_handle, funct, e1, e2, a, b, max_fun, x, f, iuser, ruser,
               ifail);

  if (ifail != 0) {
    printf("Error from nagad_opt_one_var_func_a1w (e04ab_a1w_f_)."
           "  %" NAG_IFMT "\n", ifail);
    exit_status = 1;
  }  else {
    printf("The minimum lies in the interval %7.5f to %7.5f.\n",
           nagad_a1w_get_value(a), nagad_a1w_get_value(b));
    printf("Its estimated position is %7.5f,\n", nagad_a1w_get_value(x));
    printf("where the function value is %13.4e.\n", nagad_a1w_get_value(f));
    printf("%1" NAG_IFMT " function evaluations were required.\n", max_fun);

    nagad_a1w_set_derivative(&x,1.0);
    nagad_a1w_ir_interpret_adjoint(ifail);
    printf("dx/druser %26.16f\n",nagad_a1w_get_derivative(ruser[0]));
  }
  // Remove computational data object and tape
  x10ab_a1w_f_(ad_handle,ifail);
  nagad_a1w_ir_remove();

  return exit_status;
}