/* E01BA_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 <math.h>
#include <nag_stdlib.h>
#include <iostream>
using namespace std;
int main(void)
{
// Scalars
int exit_status = 0;
const Integer m = 7;
cout << "E01BA_A1W_F C++ Header Example Program Results\n\n";
// Data points and values.
double xr[m], yr[m];
nagad_a1w_w_rtype x[m], y[m];
xr[0] = 0.0; xr[1] = 0.2; xr[2] = 0.4; xr[3] = 0.6;
xr[4] = 0.75; xr[5] = 0.9; xr[6] = 1.0;
for (int i= 0; i < m; i++) {
yr[i] = exp(xr[i]);
x[i].value = xr[i];
x[i].id = 0;
y[i].value = yr[i];
y[i].id = 0;
}
// Create AD tape
nagad_a1w_ir_create();
// Create AD configuration data object
Integer ifail = 0;
void *ad_handle = 0;
x10aa_a1w_f_(ad_handle,ifail);
// Register variables to differentiate w.r.t.
for (int i=0; i<m; i++) {
nagad_a1w_ir_register_variable(&x[i]);
nagad_a1w_ir_register_variable(&y[i]);
}
// Call the AD routine
const Integer lck = m + 4, lwrk = 6*m+16;
nagad_a1w_w_rtype c[lck], lamda[lck], wrk[lwrk];
ifail = 0;
e01ba_a1w_f_(ad_handle,m,x,y,lamda,c,lck,wrk,lwrk,ifail);
// Evaluate computed spline using e02bb
double xint_r = 0.5;
nagad_a1w_w_rtype xint, fit;
xint.value = xint_r;
xint.id = 0;
ifail = 0;
e02bb_a1w_f_(ad_handle,lck,lamda,c,xint,fit,ifail);
cout << "\n Value of fitted spline at x = " << xint_r;
cout.precision(5);
cout << " is: " << fit.value << endl;
// Setup evaluation of derivatives via adjoints.
double inc = 1.0;
nagad_a1w_inc_derivative(&fit,inc);
ifail = 0;
nagad_a1w_ir_interpret_adjoint(ifail);
cout << "\n Derivatives calculated: First order adjoints\n";
cout << " Computational mode : algorithmic\n";
// Get derivatives
cout << "\n Derivatives of fitted value w.r.t. data points:\n";
cout << " j d/dx(j) d/y(j)\n";
cout.setf(ios::scientific,ios::floatfield);
cout.precision(4);
for (int j=0; j < m; j++) {
double dx = nagad_a1w_get_derivative(x[j]);
double dy = nagad_a1w_get_derivative(y[j]);
cout.width(3); cout << j+1;
cout.width(12); cout << dx;
cout.width(12); cout << dy << endl;
}
// Remove computational data object and tape
x10ab_a1w_f_(ad_handle,ifail);
nagad_a1w_ir_remove();
return exit_status;
}