/* nag_sum_cheby_series (c06dcc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 24, 2013.
 */

#include <nag.h>
#include <nag_stdlib.h>
#include <nagc06.h>

int main(void)
{
  /* Scalars */
  Integer    exit_status = 0, i, lx, n;
  double     xmax, xmin;
  /* Arrays */
  char       sstr[30];
  double     *c = 0, *res = 0, *x = 0;
  /* Nag Types */
  Nag_Series s;
  NagError   fail;

  INIT_FAIL(fail);

  printf("nag_sum_cheby_series (c06dcc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld%*[^\n]%ld%*[^\n]", &n, &lx);

  if (!(c = NAG_ALLOC(n, double)) ||
      !(res = NAG_ALLOC(lx, double)) ||
      !(x = NAG_ALLOC(lx, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  for (i = 0; i < lx; i++) scanf("%lf", &x[i]);
  scanf("%*[^\n]");
  scanf("%lf%lf%*[^\n]", &xmin, &xmax);
  scanf("%29s%*[^\n]", sstr);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  s = (Nag_Series) nag_enum_name_to_value(sstr);
  for (i = 0; i < n; i++) scanf("%lf", &c[i]);

  /* nag_sum_cheby_series (c06dcc).
   * Evaluates a polynomial from its Chebyshev series representation.
   */
  nag_sum_cheby_series(x,lx,xmin,xmax,c,n,s,res,&fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_sum_cheby_series (c06dcc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  printf("\n%8s%16s\n","x","sum at x");
  for (i = 0; i < lx; i++) printf("%11.4f%13.4f\n", x[i], res[i]);

 END:
  NAG_FREE(c);
  NAG_FREE(res);
  NAG_FREE(x);

  return exit_status;
}