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

NAG CL Interface Introduction
Example description
/* nag_interp_dimn_scat_shep (e01zmc) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.3, 2023.
 */
#include <math.h>
#include <nag.h>

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer d, i, j, liq, lrq, m, n, nq, nw;
  /* Arrays */
  double *f = 0, *q = 0, *qx = 0, *rq = 0, *x = 0, *xe = 0;
  Integer *iq = 0;
  /* NAG types */
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_interp_dimn_scat_shep (e01zmc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Input the number of nodes. */
  scanf("%" NAG_IFMT " %" NAG_IFMT "%*[^\n] ", &d, &m);

  liq = 2 * m + 1;
  lrq = (d + 1) * (d + 2) / 2 * m + 2 * d + 1;
  if (!(x = NAG_ALLOC(d * m, double)) || !(f = NAG_ALLOC(m, double)) ||
      !(iq = NAG_ALLOC(liq, Integer)) || !(rq = NAG_ALLOC(lrq, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Input the data points X and F. */
  for (i = 0; i < m; i++) {
    for (j = 0; j < d; j++)
      scanf("%lf", &x[i * d + j]);
    scanf("%lf%*[^\n] ", &f[i]);
  }

  /* Generate the interpolant using nag_interp_dimn_scat_shep (e01zmc):
     Interpolating functions, modified Shepard's method, d variables. */
  nq = 0;
  nw = 0;
  nag_interp_dimn_scat_shep(d, m, x, f, nw, nq, iq, rq, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_interp_dimn_scat_shep (e01zmc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Input the number of evaluation points and allocate arrays with lengths
     based on this. */
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  if (!(xe = NAG_ALLOC((d) * (n), double)) || !(q = NAG_ALLOC((n), double)) ||
      !(qx = NAG_ALLOC((d) * (n), double))) {
    printf("Allocation failure\n");
    exit_status = -2;
    goto END;
  }

  /* Input the evaluation points. */
  for (i = 0; i < n; i++) {
    for (j = 0; j < d; j++)
      scanf("%lf", &xe[i * d + j]);
    scanf("%*[^\n] ");
  }
  /* Evaluate the interpolant using nag_interp_dimn_scat_shep_eval (e01znc), at
   * given interpolated values, where interpolant previously computed by
   * nag_interp_dimn_scat_shep (e01zmc). */
  nag_interp_dimn_scat_shep_eval(d, m, x, f, iq, rq, n, xe, q, qx, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_interp_dimn_scat_shep_eval (e01znc).\n%s\n",
           fail.message);
    exit_status = 2;
    goto END;
  }

  /* Print evaluation points and interpolated function values at those points */

  /* Header */
  printf("\n%4s|%39s%23s|%8s", "", "Interpolated Evaluation Points", "",
         "Values\n");
  printf(" ---|--------------------------------------------------------------");
  printf("+-------\n");
  printf(" i  |  ");
  for (i = 0; i < d; i++)
    printf("XE(%1" NAG_IFMT ",i)   ", i);
  printf("|  q[i]\n");
  printf(" ---|--------------------------------------------------------------");
  printf("--------\n");

  /* Results */
  for (i = 0; i < n; i++) {
    printf(" %1" NAG_IFMT " ", i);
    for (j = 0; j < d; j++)
      printf("%10.4f", xe[i * d + j]);
    printf("  %10.4f\n", q[i]);
  }

END:
  NAG_FREE(f);
  NAG_FREE(q);
  NAG_FREE(qx);
  NAG_FREE(rq);
  NAG_FREE(x);
  NAG_FREE(xe);
  NAG_FREE(iq);

  return exit_status;
}