/* nag_legendre_p (s22aac) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * NAG C Library
 *
 * Mark 26, 2016.
 *
 */

#include <stdio.h>
#include <string.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nags.h>

int main(void)
{
  Integer exit_status = 0, m, mode, n, nl;
  NagError fail;
  char *str = 0;
  double *p = 0, x;

  INIT_FAIL(fail);

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  printf("nag_legendre_p (s22aac) Example Program Results\n");
  scanf("%" NAG_IFMT " %lf %" NAG_IFMT " %" NAG_IFMT "", &mode, &x, &m, &nl);
  if (!(p = NAG_ALLOC(nl + 1, double)) || !(str = NAG_ALLOC(80, char)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  if (mode == 1) {
    if (m == 0)
      strcpy(str, "Unnormalized Legendre function values\n");
    else
      strcpy(str, "Unnormalized associated Legendre function values\n");
  }
  else if (mode == 2) {
    if (m == 0)
      strcpy(str, "Normalized Legendre function values\n");
    else
      strcpy(str, "Normalized associated Legendre function values\n");
  }

  /* nag_legendre_p (s22aac).
   * Legendre and associated Legendre functions of the first
   * kind with real arguments
   */
  nag_legendre_p(mode, x, m, nl, p, &fail);
  printf("mode      x      m    nl\n");
  printf("%3" NAG_IFMT "    %5.1f%6" NAG_IFMT "%6" NAG_IFMT "\n\n", mode, x,
         m, nl);

  if (fail.code == NE_NOERROR) {
    printf("%s\n", str);
    printf(" n      P(n)\n");
    for (n = 0; n <= nl; ++n)
      printf("%2" NAG_IFMT " %13.4e\n", n, p[n]);
  }
  else {
    printf("Error from nag_legendre_p (s22aac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
END:
  NAG_FREE(p);
  NAG_FREE(str);
  return exit_status;
}