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

NAG CL Interface Introduction
Example description
/* nag_fit_dim2_spline_evalm (e02dfc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.3, 2024.
 */

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

#define FF(I, J) ff[my * (I) + (J)]

int main(void) {
  Integer exit_status = 0, i, j, mx, my;
  NagError fail;
  Nag_2dSpline spline;
  double *ff = 0, *x = 0, *y = 0;

  INIT_FAIL(fail);

  /* Initialize spline */
  spline.lamda = 0;
  spline.mu = 0;
  spline.c = 0;

  printf("nag_fit_dim2_spline_evalm (e02dfc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  /* Read mx and my, the number of grid points in the x and y
   * directions respectively.
   */
  scanf("%" NAG_IFMT "%" NAG_IFMT "", &mx, &my);
  if (mx >= 1 && my >= 1) {
    if (!(x = NAG_ALLOC(mx, double)) || !(y = NAG_ALLOC(my, double)) ||
        !(ff = NAG_ALLOC(mx * my, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  } else {
    printf("Invalid mx or my.\n");
    exit_status = 1;
    return exit_status;
  }
  /* Read spline.nx and spline.ny, the number of knots
   * in the x and y directions.
   */
  scanf("%" NAG_IFMT "%" NAG_IFMT "", &(spline.nx), &(spline.ny));
  if (!(spline.c = NAG_ALLOC((spline.nx - 4) * (spline.ny - 4), double)) ||
      !(spline.lamda = NAG_ALLOC(spline.nx, double)) ||
      !(spline.mu = NAG_ALLOC(spline.ny, double))) {
    printf("Storage allocation failed.\n");
    exit_status = -1;
    goto END;
  }
  /* Read the knots spline.lamda[0]...spline.lamda[nx-1]
   * and spline.mu[0]...spline.mu[ny-1].
   */
  for (i = 0; i < spline.nx; i++)
    scanf("%lf", &(spline.lamda[i]));
  for (i = 0; i < spline.ny; i++)
    scanf("%lf", &(spline.mu[i]));
  /* Read spline.c, the bicubic spline coefficients. */
  for (i = 0; i < (spline.nx - 4) * (spline.ny - 4); i++)
    scanf("%lf", &(spline.c[i]));
  /* Read the x and y co-ordinates defining the evaluation grid. */
  for (i = 0; i < mx; i++)
    scanf("%lf", &x[i]);
  for (i = 0; i < my; i++)
    scanf("%lf", &y[i]);
  /* Evaluate the spline at the mx by my points. */
  /* nag_fit_dim2_spline_evalm (e02dfc).
   * Evaluation of bicubic spline, at a mesh of points
   */
  nag_fit_dim2_spline_evalm(mx, my, x, y, ff, &spline, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_fit_dim2_spline_evalm (e02dfc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the result array. */
  printf("Spline evaluated on x-y grid (x across, y down):\n      ");
  for (i = 0; i < mx; i++)
    printf("%2.1f      ", x[i]);
  printf("\n");
  for (j = 0; j < my; j++) {
    printf("%2.1f", y[j]);
    for (i = 0; i < mx; i++)
      printf("%8.3f%s", FF(i, j), (i % 7 == 6 || i == mx - 1) ? "\n" : " ");
  }
END:
  NAG_FREE(spline.c);
  NAG_FREE(spline.lamda);
  NAG_FREE(spline.mu);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(ff);
  return exit_status;
}