/* nag_bivariate_students_t (g01hcc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg01.h>

int main(void)
{
  /* Scalars */
  Integer df, exit_status = 0, ierr;
  double prob, rho;
  /* Arrays */
  char nag_enum_arg[14];
  double a[2], b[2];
  /* NAG types */
  Nag_TailProbability tail;
  NagError fail;

  printf("%s\n\n",
         "nag_bivariate_students_t (g01hcc) Example Program Results");

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

  /* Display headers */
  printf("%-8s%2s%-8s%2s%-8s%2s%-8s%2s%-4s%2s%-8s%2s%-14s%2s%-8s\n\n",
         "a1", " ", "b1", " ", "a2", " ", "b2", " ", "df", " ", "rho", " ",
         "Tail", " ", "p");

  while (1) {
    ierr = scanf("%13s", nag_enum_arg);
    if (ierr == EOF || ierr < 1) {
      break;
    }

    /* Initialize limits */
    a[0] = a[1] = b[0] = b[1] = 0.0;

    /* nag_enum_name_to_value (x04nac).
     * Converts NAG enum member name to value
     */
    tail = (Nag_TailProbability) nag_enum_name_to_value(nag_enum_arg);

    /* Read parameter values */
    switch (tail) {
    case Nag_LowerTail:
      scanf("%" NAG_IFMT "%lf%lf%lf", &df, &rho, b, b + 1);
      break;
    case Nag_Central:
      scanf("%" NAG_IFMT "%lf%lf%lf%lf%lf", &df, &rho, a, b, a + 1, b + 1);
      break;
    case Nag_UpperTail:
      scanf("%" NAG_IFMT "%lf%lf%lf", &df, &rho, a, a + 1);
      break;
    default:
      printf(" %s\n", "Invalid tail specification in data file");
      exit_status = -1;
      goto END;
    }
    scanf("%*[^\n]");

    /* Calculate probability for the bivariate Student's t-distribution */
    INIT_FAIL(fail);
    /* nag_bivariate_students_t (g01hcc) */
    prob = nag_bivariate_students_t(tail, a, b, df, rho, &fail);

    /* Display results */
    switch (tail) {
    case Nag_LowerTail:
      printf("%-8s%2s%-8g%2s%-8s%2s%-8g",
             "-Inf", " ", b[0], " ", "-Inf", " ", b[1]);
      break;
    case Nag_Central:
      printf("%-8g%2s%-8g%2s%-8g%2s%-8g",
             a[0], " ", b[0], " ", a[1], " ", b[1]);
      break;
    case Nag_UpperTail:
      printf("%-8g%2s%-8s%2s%-8g%2s%-8s",
             a[0], " ", "Inf", " ", a[1], " ", "Inf");
      break;
    default:
      {
        printf("Invalid tail specification.\n");
        exit_status = -1;
        goto END;
      }
    }

    printf("%2s%-4" NAG_IFMT "%2s%-8g%2s%-14s%2s%-8.4f\n",
           " ", df, " ", rho, " ", nag_enum_arg, " ", prob);
  }

END:
  return exit_status;
}