/* nag_prob_poisson_vector (g01skc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */
#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg01.h>

int main(void)
{
  /* Integer scalar and array declarations */
  Integer lk, ll, i, lout;
  Integer *ivalid = 0, *k = 0;
  Integer exit_status = 0;

  /* NAG structures */
  NagError fail;

  /* Double scalar and array declarations */
  double *peqk = 0, *pgtk = 0, *plek = 0, *l = 0;

  /* Initialise the error structure to print out any error messages */
  INIT_FAIL(fail);

  printf("nag_prob_poisson_vector (g01skc) Example Program Results\n\n");

  /* Skip heading in data file*/
  scanf("%*[^\n] ");
  scanf("%ld%*[^\n] ", &ll);
  if (!(l = NAG_ALLOC(ll, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < ll; i++)
    scanf("%lf", &l[i]);
  scanf("%*[^\n] ");
  scanf("%ld%*[^\n] ", &lk);
  if (!(k = NAG_ALLOC(lk, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lk; i++)
    scanf("%ld", &k[i]);
  scanf("%*[^\n] ");

  /* Allocate memory for output */
  lout = MAX(ll,lk);
  if (!(peqk = NAG_ALLOC(lout, double)) ||
      !(pgtk = NAG_ALLOC(lout, double)) ||
      !(plek = NAG_ALLOC(lout, double)) ||
      !(ivalid = NAG_ALLOC(lout, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Calculate probability */
  nag_prob_poisson_vector(ll, l, lk, k, plek, pgtk, peqk, ivalid, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_prob_poisson_vector (g01skc).\n%s\n",
            fail.message);
    exit_status = 1;
    if (fail.code != NW_IVALID) goto END;
  }

  /* Display title */
  printf("     l         k       plek      pgtk      peqk  ivalid\n");
  printf(" --------------------------------------------------------\n");

  /* Display results */
  for (i = 0; i < lout; i++)
    printf(" %6.2f    %6ld    %6.3f    %6.3f    %6.3f   %3ld\n",
           l[i%ll], k[i%lk], plek[i], pgtk[i], peqk[i], ivalid[i]);

 END:
  NAG_FREE(l);
  NAG_FREE(k);
  NAG_FREE(plek);
  NAG_FREE(pgtk);
  NAG_FREE(peqk);
  NAG_FREE(ivalid);

  return(exit_status);
}