/* E01EB_T1W_F C++ Header Example Program.
*
* Copyright 2019 Numerical Algorithms Group.
* Mark 27, 2019.
*/
#include <dco_light.hpp>
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
// Scalars
int exit_status = 0;
cout << "E01EB_T1W_F C++ Header Example Program Results\n\n";
// Skip first line of data file
string mystr;
getline (cin, mystr);
// Read number of data points
Integer n;
cin >> n;
// Allocate arrays for data and interpolant
nagad_t1w_w_rtype *x = 0, *y = 0, *f = 0;
Integer *triang = 0;
x = new nagad_t1w_w_rtype [n];
y = new nagad_t1w_w_rtype [n];
f = new nagad_t1w_w_rtype [n];
triang = new Integer [7*n];
// Create AD configuration data object
Integer ifail = 0;
void *ad_handle = 0;
x10aa_t1w_f_(ad_handle,ifail);
// Read data and register variables
for (int i=0; i<n; i++) {
double xr, yr, fr;
cin >> xr >> yr >> fr;
x[i] = xr;
y[i] = yr;
f[i] = fr;
}
ifail = 0;
e01ea_t1w_f_(n,x,y,triang,ifail);
// Evaluate interpolant and derivatives at a mid-point
nagad_t1w_w_rtype px[1], py[1], pf[1];
double xint, yint;
xint = 0.5*(nagad_t1w_get_value(x[n/2-1]) + nagad_t1w_get_value(x[n/2]));
yint = 0.5*(nagad_t1w_get_value(y[n/2-1]) + nagad_t1w_get_value(y[n/2]));
px[0] = xint;
py[0] = yint;
// Call the AD routine
double inc = 1.0, zero = 0.0;
Integer m = 1;
nagad_t1w_inc_derivative(&px[0],inc);
ifail = 0;
e01eb_t1w_f_(ad_handle,m,n,x,y,f,triang,px,py,pf,ifail);
double dx = nagad_t1w_get_derivative(pf[0]);
nagad_t1w_set_derivative(&px[0],zero);
nagad_t1w_inc_derivative(&py[0],inc);
ifail = 0;
e01eb_t1w_f_(ad_handle,m,n,x,y,f,triang,px,py,pf,ifail);
double dy = nagad_t1w_get_derivative(pf[0]);
cout << "\n Interpolant point: x = " << xint << " y = " << yint << endl;
cout.precision(5);
cout << " Interpolated value = " << nagad_t1w_get_value(pf[0]) << endl;
cout << "\n Derivatives calculated: First order tangents\n";
cout << " Computational mode : algorithmic\n";
cout.setf(ios::scientific,ios::floatfield);
cout.precision(4);
cout << "\n Derivatives of fitted value w.r.t. fit point:\n\n";
cout << " d/dx = " << dx << endl;
cout << " d/dy = " << dy << endl;
// Remove computational data object
x10ab_t1w_f_(ad_handle,ifail);
delete [] x;
delete [] y;
delete [] f;
delete [] triang;
return exit_status;
}