/* nag_multi_students_t (g01hdc) 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>

#define RC(I, J) rc[(I-1)*tdrc + J-1]

int main(void)
{
  /* Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer fmax, i, ierr, j, tdrc, n, nsampl, numsub;
  Nag_Boolean iscov;

  /* Double scalar and array declarations */
  double epsabs, epsrel, errest, nu, prob;
  double *a = 0, *b = 0, *delta = 0, *rc = 0;

  /* NAG structures */
  Nag_TailProbability *tail = 0;
  NagError fail;

  /* Character scalar and array declarations */
  char nag_enum_arg[30 + 1];

  printf("nag_multi_students_t (g01hdc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT, &n);
  scanf("%30s%*[^\n] ", nag_enum_arg);

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

  tdrc = n;
  numsub = 200;
  nsampl = 8;
  fmax = 25000;
  epsabs = 0.0;
  epsrel = 1.0e-3;

  if (!(tail = NAG_ALLOC(n, Nag_TailProbability)) ||
      !(a = NAG_ALLOC(n, double)) ||
      !(b = NAG_ALLOC(n, double)) ||
      !(delta = NAG_ALLOC(n, double)) || !(rc = NAG_ALLOC(tdrc * n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  while (1) {
    ierr = scanf("%*[^\n] ");

    if (ierr == EOF)
      break;
    scanf("%lf%*[^\n] ", &nu);

    /* read NAG enum member name as string and convert using
     * nag_enum_name_to_value (x04nac).
     */
    for (j = 0; j < n; j++) {
      scanf("%30s", nag_enum_arg);
      tail[j] = (Nag_TailProbability) nag_enum_name_to_value(nag_enum_arg);
    }
    scanf("%*[^\n]");

    for (j = 0; j < n; j++)
      scanf("%lf", &a[j]);
    scanf("%*[^\n] ");

    for (j = 0; j < n; j++)
      scanf("%lf", &b[j]);
    scanf("%*[^\n] ");

    for (j = 0; j < n; j++)
      scanf("%lf", &delta[j]);
    scanf("%*[^\n] ");

    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        scanf("%lf", &RC(i, j));
    scanf("%*[^\n] ");

    /* nag_multi_students_t (g01hdc). Multivariate Student's t probability */
    INIT_FAIL(fail);
    prob = nag_multi_students_t(n, tail, a, b, nu, delta, iscov, rc, tdrc,
                                epsabs, epsrel, numsub, nsampl, fmax, &errest,
                                &fail);

    if (fail.code == NE_NOERROR) {
      printf("%24s%24.7e\n", "Probability:   ", prob);
      printf("%24s%24.2e\n\n", "Error estimate:", errest);
    }
    else {
      printf("nag_multi_students_t (g01hdc) failed.\n%s\n", fail.message);
      exit_status = 1;
    }
  }

END:

  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(delta);
  NAG_FREE(rc);
  NAG_FREE(tail);

  return exit_status;
}