/* nag_smooth_spline_estim (g10acc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 6a revised, 2001.
 */

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

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

  INIT_FAIL(fail);

  printf("nag_smooth_spline_estim (g10acc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld", &n);
  if (!(x = NAG_ALLOC(n, double))
     || !(y = NAG_ALLOC(n, double))
     || !(weights = NAG_ALLOC(n, double))
     || !(yhat = NAG_ALLOC(n, double))
     || !(coeff = NAG_ALLOC((n-1)*3, double))
     || !(res = NAG_ALLOC(n, double))
     || !(h = NAG_ALLOC(n, double))
     || !(wwt = NAG_ALLOC(n, double))
     || !(yord = NAG_ALLOC(n, double))
     || !(xord = NAG_ALLOC(n, 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
   */
  method = (Nag_SmoothParamMethods) nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s", nag_enum_arg);
  weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg);

  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;
    }
  scanf("%lf %lf %ld %lf", &u, &tol, &maxcal, &crit);
  /* Sort data, removing ties and weighting accordingly */
  /* nag_order_data (g10zac).
   * Reorder data to give ordered distinct observations
   */
  nag_order_data(n, x, y, wtptr, &nord, xord, yord, wwt, &rss, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_order_data (g10zac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
  /* Fit cubic spline */
  /* nag_smooth_spline_estim (g10acc).
   * Fit cubic smoothing spline, smoothing parameter estimated
   */
  nag_smooth_spline_estim(method, nord, xord, yord, wwt, yhat, coeff, &rss,
                          &df, res, h, &crit, &rho, u, tol, maxcal, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_smooth_spline_estim (g10acc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* Print results */
  printf("\n");
  printf("%s%10.2f\n", " Residual sum of squares = ", rss);
  printf("%s%10.2f\n", " Degrees of freedom = ", df);
  printf("%s%10.2f\n", " rho = ", rho);
  printf("\n");
  printf("\n%s%s%s\n%s%s%s\n", "     Input data",
          "                ",
          "Output results",
          "   i    x        y   ",
          "          ",
          "yhat     h");

  for (i = 1; i <= nord; ++i)
    printf("%4ld %8.3f %8.3f      %8.3f %8.3f\n",
            i, xord[i - 1], yord[i - 1], yhat[i - 1], h[i - 1]);

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

  return exit_status;
}