/* E01AA_T1W_F C++ Header Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
* Mark 30.2, 2024.
*/
#include "dco.hpp"
#include <iostream>
#include <math.h>
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
// Scalars
int exit_status = 0;
cout << "E01AA_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, *a = 0, *b = 0, *c = 0;
double xr, fx, *ar = 0, *br = 0, *dx = 0;
Integer n1, n2;
n1 = n + 1;
n2 = n * (n + 1) / 2;
ar = new double[n1];
br = new double[n1];
dx = new double[n1];
a = new nagad_t1w_w_rtype[n1];
b = new nagad_t1w_w_rtype[n1];
c = new nagad_t1w_w_rtype[n2];
cin >> xr;
for (int i = 0; i < n1; i++)
{
cin >> ar[i];
}
for (int i = 0; i < n1; i++)
{
cin >> br[i];
}
// Create AD configuration data object
Integer ifail = 0;
nag::ad::handle_t ad_handle;
for (int i = 0; i < n1; ++i)
{
x = xr;
for (int j = 0; j < n1; j++)
{
a[j] = ar[j];
b[j] = br[j];
}
double inc = 1.0;
dco::derivative(a[i]) = inc;
// Call the AD routine
ifail = 0;
nag::ad::e01aa(ad_handle, a, b, c, n, x, ifail);
if (i == 0)
{
fx = dco::value(c[n2 - 1]);
}
double zero = 0.0;
dco::derivative(a[i]) = zero;
dx[i] = dco::derivative(c[n2 - 1]);
}
cout.setf(ios::scientific, ios::floatfield);
cout.precision(5);
cout << "\n Interpolation point = ";
cout.width(12);
cout << xr << endl;
cout << "\n Function value at interpolation point = ";
cout.width(12);
cout << fx << endl;
cout << "\n Derivatives calculated: First order tangents\n";
cout << " Computational mode : algorithmic\n";
// Get derivatives
cout << "\n Derivatives of fitted value w.r.t. x values:\n";
cout << " j d/da(j)\n";
for (int j = 0; j < n1; j++)
{
cout.width(3);
cout << j + 1 << " ";
cout.width(12);
cout << dx[j] << endl;
}
delete[] ar;
delete[] br;
delete[] dx;
delete[] a;
delete[] b;
delete[] c;
return exit_status;
}