NAG Library Manual, Mark 30.2
Interfaces:  FL   CL   CPP   AD 

NAG AD Library Introduction
Example description
/* E01EA_P0W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.2, 2024.
 */

#include <iostream>
#include <dco.hpp>
#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 << "E01EA_P0W_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
  double * x = 0, *y = 0, *f = 0;
  Integer *triang = 0;
  x               = new double[n];
  y               = new double[n];
  f               = new double[n];
  triang          = new Integer[7 * n];

  // Read data 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;
  }

  Integer           ifail = 0;
  nag::ad::handle_t ad_handle;
  nag::ad::e01ea(n, x, y, triang, ifail);

  double px[1], py[1], pf[1];
  double xint, yint;
  xint  = 0.5 * (x[n / 2 - 1] + x[n / 2]);
  yint  = 0.5 * (y[n / 2 - 1] + y[n / 2]);
  px[0] = xint;
  py[0] = yint;

  // Call the passive routine
  Integer m = 1;
  ifail     = 0;
  nag::ad::e01eb(ad_handle, m, n, x, y, f, triang, px, py, pf, ifail);

  cout << "\n Interpolant point: x = " << xint << " y = " << yint << endl;
  cout.precision(5);
  cout << " Interpolated value = " << pf[0] << endl;

  delete[] x;
  delete[] y;
  delete[] f;
  delete[] triang;
  return exit_status;
}