/* nag_1d_ratnl_interp (e01rac) 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;
  NagError fail;

  /* Arrays */
  double   *a = 0, *f = 0, *u = 0, *x = 0;

  exit_status = 0;
  INIT_FAIL(fail);

  printf("nag_1d_ratnl_interp (e01rac) Example Program Results\n");

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

  /* Allocate memory */
  if (!(a = NAG_ALLOC(n, double)) ||
      !(f = NAG_ALLOC(n, double)) ||
      !(u = NAG_ALLOC(n, double)) ||
      !(x = NAG_ALLOC(n, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  for (i = 1; i <= n; ++i)
    scanf("%lf", &x[i-1]);
  scanf("%*[^\n] ");

  for (i = 1; i <= n; ++i)
    scanf("%lf", &f[i-1]);
  scanf("%*[^\n] ");

  /* nag_1d_ratnl_interp (e01rac).
   * Interpolating functions, rational interpolant, one
   * variable
   */
  nag_1d_ratnl_interp(n, x, f, &m, a, u, &fail);
  if (fail.code != NE_NOERROR)
    {
      exit_status = 1;
      printf("Error from nag_1d_ratnl_interp (e01rac).\n%s\n",
              fail.message);
      goto END;
    }

  printf("\n");
  printf("The values of u[j] are\n");
  for (i = 1; i <= m - 1; ++i)
    {
      printf("%13.4e", u[i-1]);
      printf(i%4 == 0 || i == m - 1?"\n":" ");
    }
  printf("\n");

  printf("The Thiele coefficients a[j] are\n");
  for (i = 1; i <= m; ++i)
    {
      printf("%13.4e", a[i-1]);
      printf(i%4 == 0 || i == m?"\n":" ");
    }

 END:
  NAG_FREE(a);
  NAG_FREE(f);
  NAG_FREE(u);
  NAG_FREE(x);

  return exit_status;
}