NAG Library Manual, Mark 29
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_stat_pdf_gamma_vector (g01kkc) Example Program.
 *
 * Copyright 2023 Numerical Algorithms Group.
 *
 * Mark 29.0, 2023.
 */
#include <nag.h>
#include <stdio.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];

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

  printf("nag_stat_pdf_gamma_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("%" 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] ", &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("%" NAG_IFMT "%*[^\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_stat_pdf_gamma_vector(ilog, lx, x, la, a, lb, b, pdf, ivalid, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_stat_pdf_gamma_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    %3" NAG_IFMT "\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);
}