/* nag_1d_pade_eval (e02rbc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2001.
 */

#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nage02.h>

int main(void)
{
  /* Scalars */
  double   ans, tval, x;
  Integer  exit_status, i, l, m, ia, ib, ic;
  NagError fail;

  /* Arrays */
  double   *aa = 0, *bb = 0, *cc = 0;

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_1d_pade_eval (e02rbc) Example Program Results\n");

  l = 4;
  m = 4;
  ia = l + 1;
  ib = m + 1;
  ic = ia + ib - 1;

  /* Allocate memory */
  if (!(aa = NAG_ALLOC(ia, double)) ||
      !(bb = NAG_ALLOC(ib, double)) ||
      !(cc = NAG_ALLOC(ic, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  cc[0] = 1.0;
  for (i = 1; i <= ic - 1; ++i)
    cc[i] = cc[i-1] / (double) i;

  /* nag_1d_pade (e02rac).
   * Pade-approximants
   */
  nag_1d_pade(ia, ib, cc, aa, bb, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_1d_pade (e02rac).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  printf("\n");
  printf("    x        Pade           True\n");

  for (i = 0; i < 10; ++i)
    {
      x = (double)(i + 1) / 10.0;
      /* nag_1d_pade_eval (e02rbc).
       * Evaluation of fitted rational function as computed by
       * nag_1d_pade (e02rac)
       */
      nag_1d_pade_eval(aa, ia, bb, ib, x, &ans, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_1d_pade_eval (e02rbc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      tval = exp(x);
      printf("%6.1f%15.5e%15.5e\n", x, ans, tval);
    }

 END:
  NAG_FREE(aa);
  NAG_FREE(bb);
  NAG_FREE(cc);

  return exit_status;
}