/* nag_normal_pdf_vector (g01kqc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg01.h>

int main(void)
{
  /* Integer scalar and array declarations */
  Integer lx, lxmu, lxstd, i, lout;
  Integer *ivalid = 0;
  Integer exit_status = 0;

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

  /* Double scalar and array declarations */
  double *x = 0, *xmu = 0, *xstd = 0, *pdf = 0;

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

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

  printf("nag_normal_pdf_vector (g01kqc) 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("%" NAG_IFMT "%*[^\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("%" NAG_IFMT "%*[^\n] ", &lxmu);
  if (!(xmu = NAG_ALLOC(lxmu, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lxmu; i++)
    scanf("%lf", &xmu[i]);
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &lxstd);
  if (!(xstd = NAG_ALLOC(lxstd, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lxstd; i++)
    scanf("%lf", &xstd[i]);
  scanf("%*[^\n] ");

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

  /* Calculate probability */
  nag_normal_pdf_vector(ilog, lx, x, lxmu, xmu, lxstd, xstd, pdf, ivalid,
                        &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_normal_pdf_vector (g01kqc).\n%s\n", fail.message);
    exit_status = 1;
    if (fail.code != NW_IVALID)
      goto END;
  }

  /* Display title */
  printf("   x        xmu       xstd       pdf      ivalid\n");
  printf(" ------------------------------------------------\n");

  /* Display results */
  for (i = 0; i < lout; i++)
    printf("%6.2f    %6.2f    %6.2f    %9.3e    %3" NAG_IFMT "\n",
           x[i % lx], xmu[i % lxmu], xstd[i % lxstd], pdf[i], ivalid[i]);

END:
  NAG_FREE(x);
  NAG_FREE(xmu);
  NAG_FREE(xstd);
  NAG_FREE(pdf);
  NAG_FREE(ivalid);

  return (exit_status);
}