/* nag_gamma_pdf_vector (g01kkc) 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 lx, la, lb, i, lout;
  Integer *ivalid = 0;
  Integer exit_status = 0;

  /* NAG structures */
  NagError fail;
  Nag_Boolean ilog;

  /* Double scalar and array declarations */
  double *x = 0, *a = 0, *b = 0, *pdf = 0;

  /* Character scalar and array declarations */
  char cilog[40];

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

  printf("nag_gamma_pdf_vector (g01kkc) Example Program Results\n\n");

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

  /* Read in the flag indicating whether logs are required */
  scanf("%39s%*[^\n] ", cilog);
  ilog = (Nag_Boolean) nag_enum_name_to_value(cilog);

  /* Read in the input vectors */
  scanf("%ld%*[^\n] ", &lx);
  if (!(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("%ld%*[^\n] ", &la);
  if (!(a = NAG_ALLOC(la, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < la; i++)
    scanf("%lf", &a[i]);
  scanf("%*[^\n] ");
  scanf("%ld%*[^\n] ", &lb);
  if (!(b = NAG_ALLOC(lb, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lb; i++)
    scanf("%lf", &b[i]);
  scanf("%*[^\n] ");


  /* Allocate memory for output */
  lout = MAX(lx,MAX(la,lb));
  if (!(pdf = NAG_ALLOC(lout, double)) ||
      !(ivalid = NAG_ALLOC(lout, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Calculate probability */
  nag_gamma_pdf_vector(ilog,lx,x,la,a,lb,b,pdf,ivalid,&fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gamma_pdf_vector (g01kkc).\n%s\n",
            fail.message);
    exit_status = 1;
    if (fail.code != NW_IVALID) goto END;
  }

  /* Display title */
  printf("   x          a         b        pdf      ivalid\n");
  printf(" ------------------------------------------------\n");

  /* Display results */
  for (i = 0; i < lout; i++)
    printf("%6.2f    %6.2f    %6.2f    %9.3e    %3ld\n",
           x[i%lx], a[i%la], b[i%lb], pdf[i], ivalid[i]);

 END:
  NAG_FREE(x);
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(pdf);
  NAG_FREE(ivalid);

  return(exit_status);
}