/* S01BA_A1W_F C Header Example Program.
*
* Copyright 2017 Numerical Algorithms Group.
* Mark 26.2, 2017.
*/
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
#include <nag_stdlib.h>
int main(void)
{
/* Scalars */
int exit_status = 0;
nagad_a1w_w_rtype x, y;
Integer i, ifail, n;
double dydx, inc, xr;
/* Arrays */
nagad_a1w_w_rtype *xv = 0;
void *ad_handle = 0;
printf("S01BA_A1W_F C Header Example Program Results\n\n");
/* Skip heading in data file */
#ifdef _WIN32
scanf_s("%*[^\n]");
#else
scanf("%*[^\n]");
#endif
#ifdef _WIN32
scanf_s("%" NAG_IFMT "%*[^\n] ", &n);
#else
scanf("%" NAG_IFMT "%*[^\n] ", &n);
#endif
/* Allocate memory */
if (!(xv = NAG_ALLOC(n, nagad_a1w_w_rtype)))
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read x values from data file */
for (i = 0; i < n; ++i) {
#ifdef _WIN32
scanf_s("%lf", &xr);
#else
scanf("%lf", &xr);
#endif
xv[i].value = xr;
xv[i].id = 0;
}
#ifdef _WIN32
scanf_s("%*[^\n] ");
#else
scanf("%*[^\n] ");
#endif
printf("\n x y=ln(1+x) dy/dx\n");
/* Create AD tape */
nagad_a1w_ir_create();
/* Create AD configuration data object */
ifail = 0;
x10aa_a1w_f_(&ad_handle,&ifail);
for (i = 0; i < n; ++i) {
x = xv[i];
/* Register variables to differentiate w.r.t. */
nagad_a1w_ir_register_variable(&x);
ifail = -1;
s01ba_a1w_f_(&ad_handle,&x,&y,&ifail);
nagad_a1w_ir_zero_adjoints();
inc = 1.0;
nagad_a1w_inc_derivative(&y,inc);
ifail = 0;
nagad_a1w_ir_interpret_adjoint(&ifail);
/* Get derivatives */
dydx = nagad_a1w_get_derivative(x);
if (ifail<0) {
printf(" %12.4e %12s%" NAG_IFMT "\n", x.value, "fail = ", ifail);
} else {
printf(" %12.4e %12.4e %12.4e\n", x.value, y.value, dydx);
}
}
/* Remove computational data object and tape */
x10ab_a1w_f_(&ad_handle,&ifail);
nagad_a1w_ir_remove();
END:
NAG_FREE(xv);
return exit_status;
}