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

NAG CL Interface Introduction
Example description
/* nag_fit_dim2_spline_sort (e02zac) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.5, 2022.
 */

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

int main(void) {
  /* Scalars */
  Integer exit_status, i, iadres, m, nadres, npoint, px, py;

  /* Arrays */
  double *lamda = 0, *mu = 0, *x = 0, *y = 0;
  Integer *point = 0;

  /* Nag types */
  NagError fail;

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_fit_dim2_spline_sort (e02zac) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  while (scanf("%" NAG_IFMT "%*[^\n] ", &m) != EOF)
  {
    if (m > 0) {
      /* Allocate memory */
      if (!(x = NAG_ALLOC(m, double)) || !(y = NAG_ALLOC(m, double))) {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }
    } else {
      printf("Invalid m.\n");
      exit_status = 1;
      return exit_status;
    }
    scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &px, &py);
    nadres = (px - 7) * (py - 7);
    npoint = m + nadres;
    if (px >= 8 && py >= 8) {
      /* Allocate memory */
      if (!(lamda = NAG_ALLOC(px, double)) || !(mu = NAG_ALLOC(py, double)) ||
          !(point = NAG_ALLOC(npoint, Integer))) {
        printf("Allocation failure\n");
        exit_status = -1;
        goto END;
      }
    } else {
      printf("Invalid px or py.\n");
      exit_status = 1;
      goto END;
    }

    /* Read data points and intercepts of panel sides */
    for (i = 1; i <= m; ++i) {
      scanf("%lf%lf", &x[i - 1], &y[i - 1]);
    }
    scanf("%*[^\n] ");

    if (px > 8) {
      for (i = 5; i <= px - 4; ++i) {
        scanf("%lf", &lamda[i - 1]);
      }
      scanf("%*[^\n] ");
    }
    if (py > 8) {
      for (i = 5; i <= py - 4; ++i) {
        scanf("%lf", &mu[i - 1]);
      }
      scanf("%*[^\n] ");
    }
    /* Sort points into panel order */

    /* nag_fit_dim2_spline_sort (e02zac).
     * Sort two-dimensional data into panels for fitting bicubic
     * splines
     */
    nag_fit_dim2_spline_sort(px, py, lamda, mu, m, x, y, point, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_fit_dim2_spline_sort (e02zac).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

    /* Output points in panel order */
    for (i = 1; i <= nadres; ++i) {
      printf("\n%s%4" NAG_IFMT "\n\n", "Panel", i);
      iadres = m + i;
      while ((iadres = point[iadres - 1]) > 0) {
        printf("%7.2f%7.2f\n", x[iadres - 1], y[iadres - 1]);
      }
    }
  END:
    NAG_FREE(lamda);
    NAG_FREE(mu);
    NAG_FREE(x);
    NAG_FREE(y);
    NAG_FREE(point);
  }
  return exit_status;
}