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

NAG CL Interface Introduction
Example description
/* nag_smooth_fit_spline (g10abc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */

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

int main(void) {
  Integer exit_status = 0, i, n, nord;
  double df, rho, rss;
  double *coeff = 0, *comm_ar = 0, *h = 0, *res = 0, *weights = 0;
  double *wtptr, *wwt = 0, *x = 0, *xord = 0, *y = 0, *yhat = 0;
  double *yord = 0;
  char nag_enum_arg[40];
  Nag_SmoothFitType mode;
  Nag_Boolean weight;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_smooth_fit_spline (g10abc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "", &n);
  if (!(coeff = NAG_ALLOC((n - 1) * 3, double)) ||
      !(h = NAG_ALLOC(n, double)) || !(res = NAG_ALLOC(n, double)) ||
      !(x = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double)) ||
      !(weights = NAG_ALLOC(n, double)) || !(xord = NAG_ALLOC(n, double)) ||
      !(yord = NAG_ALLOC(n, double)) || !(wwt = NAG_ALLOC(n, double)) ||
      !(yhat = NAG_ALLOC(n, double)) ||
      !(comm_ar = NAG_ALLOC(9 * n + 14, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  scanf("%39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  mode = (Nag_SmoothFitType)nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s", nag_enum_arg);
  weight = (Nag_Boolean)nag_enum_name_to_value(nag_enum_arg);
  scanf("%lf", &rho);
  if (!weight) {
    for (i = 1; i <= n; ++i)
      scanf("%lf %lf ", &x[i - 1], &y[i - 1]);
    wtptr = 0;
  } else {
    for (i = 1; i <= n; ++i)
      scanf("%lf %lf %lf", &x[i - 1], &y[i - 1], &weights[i - 1]);
    wtptr = weights;
  }
  /* Sort data into increasing X and */
  /* remove tied observations and weight accordingly */
  /* nag_smooth_data_order (g10zac).
   * Reorder data to give ordered distinct observations
   */
  nag_smooth_data_order(n, x, y, wtptr, &nord, xord, yord, wwt, &rss, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_smooth_data_order (g10zac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Fit cubic spline */
  /* nag_smooth_fit_spline (g10abc).
   * Fit cubic smoothing spline, smoothing parameter given
   */
  nag_smooth_fit_spline(mode, nord, xord, yord, wwt, rho, yhat, coeff, &rss,
                        &df, res, h, comm_ar, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_smooth_fit_spline (g10abc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print results */
  printf("\n");
  printf("%s%10.3f\n", " rho  = ", rho);
  printf("\n");
  printf("%s%10.3f\n", " Residual sum of squares  = ", rss);
  printf("%s%10.3f\n", " Degrees of freedom       = ", df);
  printf("\n");
  printf("%s\n", " Ordered input data     Output results");
  printf("\n");
  printf("%s\n", "    x       y           Fitted Values");
  printf("\n");
  for (i = 1; i <= nord; ++i) {
    printf("%8.4f %8.4f      %8.4f\n", xord[i - 1], yord[i - 1], yhat[i - 1]);
  }

END:
  NAG_FREE(coeff);
  NAG_FREE(h);
  NAG_FREE(res);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(weights);
  NAG_FREE(xord);
  NAG_FREE(yord);
  NAG_FREE(wwt);
  NAG_FREE(yhat);
  NAG_FREE(comm_ar);

  return exit_status;
}