/* nag_3d_shep_interp (e01tgc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2001.
 */

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

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

  /* Arrays */
  double   *f = 0, *q = 0, *qx = 0, *qy = 0, *qz = 0, *rq = 0,
  *u = 0, *v = 0, *w = 0, *x = 0, *y = 0, *z = 0;
  Integer  *iq = 0;

  exit_status = 0;

  INIT_FAIL(fail);

  printf("nag_3d_shep_interp (e01tgc) Example Program Results\n");

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

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

  if (m > 0)
    {
      /* Allocate memory */
      lrq = 10 * m + 7;
      liq = 2 * m + 1;
      if (!(f = NAG_ALLOC(m, double)) ||
          !(x = NAG_ALLOC(m, double)) ||
          !(y = NAG_ALLOC(m, double)) ||
          !(z = NAG_ALLOC(m, 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,Y,Z and F. */
      for (i = 0; i < m; ++i)
        scanf("%lf%lf%lf%lf%*[^\n] ", &x[i], &y[i], &z[i], &f[i]);

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

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

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

      /* Allocate memory for nag_3d_shep_eval (e01thc) */
      if (!(q = NAG_ALLOC(n, double)) ||
          !(qx = NAG_ALLOC(n, double)) ||
          !(qy = NAG_ALLOC(n, double)) ||
          !(qz = NAG_ALLOC(n, double)) ||
          !(u = NAG_ALLOC(n, double)) ||
          !(v = NAG_ALLOC(n, double)) ||
          !(w = NAG_ALLOC(n, double)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }

      /* Input the evaluation points. */
      for (i = 0; i < n; ++i)
        scanf("%lf%lf%lf%*[^\n] ", &u[i], &v[i], &w[i]);

      /* Evaluate the interpolant using nag_3d_shep_eval (e01thc). */
      fail.print = Nag_TRUE;
      /* nag_3d_shep_eval (e01thc).
       * Interpolated values, evaluate interpolant computed by
       * nag_3d_shep_interp (e01tgc), function and first
       * derivatives, three variables
       */
      nag_3d_shep_eval(m, x, y, z, f, iq, rq, n, u, v, w, q, qx, qy, qz, &fail);

      printf("\n");
      printf("     i     u(i)      v(i)      w(i)      Q(i)\n");
      for (i = 0; i < n; ++i)
        printf("%6ld%10.4f%10.4f%10.4f%10.4f\n", i, u[i], v[i], w[i],
                q[i]);
    }

 END:
  NAG_FREE(f);
  NAG_FREE(q);
  NAG_FREE(qx);
  NAG_FREE(qy);
  NAG_FREE(qz);
  NAG_FREE(rq);
  NAG_FREE(u);
  NAG_FREE(v);
  NAG_FREE(w);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(z);
  NAG_FREE(iq);

  return exit_status;
}