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

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

static double funct(double *x);

int main(void) {
  /* Scalars */
  Integer d = 6, exit_status = 0, lseed = 1, subid = 0;
  Integer i, j, liq, lrq, lstate, m, n, nq, nw, tmpdsm;
  double fun;
  /* Arrays */
  double *f = 0, *q = 0, *qx = 0, *rq = 0, *x = 0, *xe = 0;
  Integer *iq = 0, *state = 0;
  Integer seed[] = {1762543};
  /* Nag Types */
  Nag_BaseRNG genid = Nag_Basic;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_interp_dimn_scat_shep_eval (e01znc) Example Program Results\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Set up state array for generating a random sample of data locations
   * using nag_rand_init_repeat (g05kfc).
   *
   * First get the length of the state array by setting lstate = -1.
   */
  lstate = -1;
  nag_rand_init_repeat(genid, subid, seed, lseed, state, &lstate, &fail);
  if (fail.code == NE_NOERROR) {
    /* Allocate arrays */
    if (!(state = NAG_ALLOC(lstate, Integer))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    /* Then initialize the generator to a repeatable sequence */
    nag_rand_init_repeat(genid, subid, seed, lseed, state, &lstate, &fail);
  }
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_rand_init_repeat (g05kfc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Input the number of nodes. */
  scanf("%" NAG_IFMT "%*[^\n] ", &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 = -2;
    goto END;
  }
  /* Generate d*m  pseudorandom numbers in U(0,1) using
   * nag_rand_dist_uniform01 (g05sac).
   */
  tmpdsm = d * m;
  nag_rand_dist_uniform01(tmpdsm, state, x, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_rand_dist_uniform01 (g05sac).\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  /* Evaluate f at x */
  for (i = 0; i < m; i++)
    f[i] = funct(&x[i * d]);

  /* 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 = 3;
    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 = -3;
    goto END;
  }

  /* Generate a set of evaluation points lying on diagonal line
   * xe(1:d,i) = xe(1,i) = i/(n+1).
   */
  for (i = 0; i < n; i++)
    for (j = 0; j < d; j++)
      xe[i * d + j] = (double)(i + 1) / (double)(n + 1);

  /* Evaluate the interpolant using nag_interp_dimn_scat_shep_eval (e01znc), at
     given interpolated values, where interpolant previously computed by 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 = 4;
    goto END;
  }

  /* Print interpolated function values against actual function values
   * at the points on the diagonal line. */
  /* Header */
  printf("    i  |  f(i)      q(i)    | |f(i)-q(i)|\n");
  printf("    ---|--------------------+---------------\n");
  /* Results */
  for (i = 0; i < n; i++) {
    fun = funct(&xe[i * d]);
    printf("%5" NAG_IFMT " %10.4f%10.4f%10.4f\n", i, fun, q[i],
           fabs(fun - q[i]));
  }

END:

  NAG_FREE(f);
  NAG_FREE(q);
  NAG_FREE(qx);
  NAG_FREE(rq);
  NAG_FREE(x);
  NAG_FREE(xe);
  NAG_FREE(iq);
  NAG_FREE(state);

  return exit_status;
}

static double funct(double *x) {
  double funct_return;
  funct_return = x[0] * x[1] * x[2] / (1.0 + 2.0 * x[3] * x[4] * x[5]);
  return funct_return;
}