/* C05AU_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>
extern "C"
{
static void NAG_CALL f(void * &ad_handle,
const nagad_a1w_w_rtype &x,
nagad_a1w_w_rtype &z,
Integer iuser[],
nagad_a1w_w_rtype ruser[]);
}
int main(void)
{
/* Scalars */
Integer exit_status = 0;
nagad_a1w_w_rtype a, b, eps, eta, h, x;
Integer ifail;
/* Arrays */
nagad_a1w_w_rtype ruser[1];
Integer iuser[1];
printf("C05AU_A1W_F C++ Header Example Program Results\n");
// Create AD tape
nagad_a1w_ir_create();
void *ad_handle = 0;
x10aa_a1w_f_(ad_handle,ifail);
ruser[0] = 1.0;
x = 1.0;
h = 0.1;
eps = 1e-05;
eta = 0.0;
nagad_a1w_ir_register_variable(&ruser[0]);
c05au_a1w_f_(ad_handle, x, h, eps, eta, f, a, b, iuser,ruser, ifail);
if (ifail == 0) {
printf("Root is %13.5f\n", nagad_a1w_get_value(x));
printf("Interval searched is [%8.5f,%8.5f]\n", nagad_a1w_get_value(a),
nagad_a1w_get_value(b));
} else {
printf("%" NAG_IFMT "\n", ifail);
if (ifail == 3 || ifail == 4) {
printf("Final value = %13.5f\n", nagad_a1w_get_value(x));
exit_status = 1;
} else {
exit_status = 2;
goto END;
}
}
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]));
END:
// Remove computational data object and tape
x10ab_a1w_f_(ad_handle,ifail);
nagad_a1w_ir_remove();
return exit_status;
}
static void (NAG_CALL f)(void * &ad_handle,
const nagad_a1w_w_rtype &x,
nagad_a1w_w_rtype &z,
Integer iuser[],
nagad_a1w_w_rtype ruser[])
{
z = x - exp(-ruser[0]*x);
}