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

NAG CL Interface Introduction
Example description
/* nag_interp_dim5_scat_shep (e01tmc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

#include <nag.h>
#include <stdio.h>

#define X(I, J) x[I * 5 + J]
#define XE(I, J) xe[I * 5 + J]

int main(void) {
  /* Scalars */
  Integer exit_status, i, j, m, n, nq, nw, liq, lrq;
  NagError fail;

  /* Arrays */
  double *f = 0, *q = 0, *qx = 0, *rq = 0, *xe = 0, *x = 0;
  Integer *iq = 0;

  exit_status = 0;

  INIT_FAIL(fail);

  printf("nag_interp_dim5_scat_shep (e01tmc) Example Program Results\n");

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

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

  /* Allocate memory */
  lrq = 21 * m + 11;
  liq = 2 * m + 1;
  if (!(f = NAG_ALLOC(m, double)) || !(x = NAG_ALLOC(m * 5, double)) ||
      !(rq = NAG_ALLOC(lrq, double)) || !(iq = NAG_ALLOC(liq, Integer))) {
    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 < 5; ++j) {
      scanf("%lf", &X(i, j));
    }
    scanf("%lf%*[^\n] ", &f[i]);
  }

  /* Generate the interpolant. */
  nq = 0;
  nw = 0;

  /* nag_interp_dim5_scat_shep (e01tmc).
   * Interpolating functions, modified Shepard's method, five
   * variables
   */
  nag_interp_dim5_scat_shep(m, x, f, nw, nq, iq, rq, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_interp_dim5_scat_shep (e01tmc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

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

  /* Allocate memory for nag_interp_dim5_scat_shep_eval (e01tnc) */
  if (!(q = NAG_ALLOC(n, double)) || !(qx = NAG_ALLOC(n * 5, double)) ||
      !(xe = NAG_ALLOC(n * 5, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Input the evaluation points. */
  for (i = 0; i < n; ++i) {
    for (j = 0; j < 5; ++j) {
      scanf("%lf", &XE(i, j));
    }
    scanf("%*[^\n] ");
  }

  /* nag_interp_dim5_scat_shep_eval (e01tnc).
   * Evaluate interpolant and first derivatives computed by
   * nag_interp_dim5_scat_shep (e01tmc).
   */
  fail.print = Nag_TRUE;
  nag_interp_dim5_scat_shep_eval(m, x, f, iq, rq, n, xe, q, qx, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_interp_dim5_scat_shep_eval (e01tnc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n Evaluation of interpolant at various (5D) points\n");
  printf("\n%6s%30s%17s\n", " pt.no.", "point coordinates", "value");
  for (i = 0; i < n; ++i)
    printf("%5" NAG_IFMT "%8.2f%8.2f%8.2f%8.2f%8.2f%10.4f\n", i, XE(i, 0),
           XE(i, 1), XE(i, 2), XE(i, 3), XE(i, 4), q[i]);

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

  return exit_status;
}