Example description
/* E01BA_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 <iostream>
using namespace std;

int main(void)
{
  // Scalars
  int               exit_status = 0;
  const Integer     m = 7;
  
  cout << "E01BA_T1W_F C++ Header Example Program Results\n\n";

  // Data points and values.
  double            xr[m], yr[m], dx[m], dy[m];
  nagad_t1w_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] = xr[i];
    y[i] = yr[i];
  }
  
  // Create AD configuration data object
  Integer ifail = 0;
  void    *ad_handle = 0;
  x10aa_t1w_f_(ad_handle,ifail);

  nagad_t1w_w_rtype xint, fit;
  xint = 0.5;

  for (int i = 0; i < 2*m; ++i) {
    double inc = 1.0;
    if (i<m) {
      nagad_t1w_inc_derivative(&x[i],inc);
    } else {
      nagad_t1w_inc_derivative(&y[i-m],inc);
    }

    // Call the AD routine
    const Integer     lck = m + 4, lwrk = 6*m+16;
    nagad_t1w_w_rtype c[lck], lamda[lck], wrk[lwrk];
    ifail = 0;
    e01ba_t1w_f_(ad_handle,m,x,y,lamda,c,lck,wrk,lwrk,ifail);

    // Evaluate computed spline using e02bb
    fit = 0.0;
    ifail = 0;
    e02bb_t1w_f_(ad_handle,lck,lamda,c,xint,fit,ifail);

    double zero = 0.0;
    if (i<m) {
      dx[i] = nagad_t1w_get_derivative(fit);
      nagad_t1w_set_derivative(&x[i],zero);
    } else {
      dy[i-m] = nagad_t1w_get_derivative(fit);
      nagad_t1w_set_derivative(&y[i-m],zero);
    }
  }
  
  cout << "\n Value of fitted spline at x = " << nagad_t1w_get_value(xint);
  cout.precision(5);
  cout << " is: " << nagad_t1w_get_value(fit) << 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. 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++) {
    cout.width(3); cout << j+1;
    cout.width(12); cout << dx[j];
    cout.width(12); cout << dy[j] << endl;
  }

  // Remove computational data object
  x10ab_t1w_f_(ad_handle,ifail);

  return exit_status;
}