/* nag_2_sample_ks_test (g08cdc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 6, 2000.
 *
 * Mark 8 revised, 2004
 *
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg05.h>
#include <nagg08.h>

int main(void)
{
  /* Integer scalar and array declarations */
  Integer            exit_status = 0;
  Integer            m, n, lstate;
  Integer            *state = 0;

  /* Double scalar and array declarations */
  double             d, p, z;
  double             *x = 0, *y = 0;

  /* Character array declarations */
  char               nag_enum_arg[40];

  /* NAG structures and data types */
  Nag_TestStatistics ntype;
  NagError           fail;

  /* Choose the base generator */
  Nag_BaseRNG        genid = Nag_Basic;
  Integer            subid = 0;

  /* Set the seed */
  Integer            seed[] = { 423523 };
  Integer            lseed = 1;

  /* Initialise the error structure */
  INIT_FAIL(fail);

  printf("nag_2_sample_ks_test (g08cdc) Example Program Results\n");

  /* Get the length of the state array */
  lstate = -1;
  nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_rand_init_repeatable (g05kfc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

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

  /* Get the problem size */
  scanf("%ld %ld", &n, &m);

  /* Allocate the arrays */
  if (!(x = NAG_ALLOC(n, double)) ||
      !(state = NAG_ALLOC(lstate, Integer)) ||
      !(y = NAG_ALLOC(m, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  printf("\n");

  /* Initialise the generator to a repeatable sequence */
  nag_rand_init_repeatable(genid, subid, seed, lseed, state, &lstate, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_rand_init_repeatable (g05kfc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* Generate vector of n uniform variates between 0 and 2 */
  nag_rand_uniform(n, 0.0, 2.0, state, x, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_rand_uniform (g05sqc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* Generate vector of m uniform variates between 0.25 and 2.25 */
  nag_rand_uniform(m, 0.25, 2.25, state, y, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_rand_uniform (g05sqc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  scanf("%39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  ntype = (Nag_TestStatistics) nag_enum_name_to_value(nag_enum_arg);

  /* nag_2_sample_ks_test (g08cdc).
   * Performs the two-sample Kolmogorov-Smirnov test
   */
  nag_2_sample_ks_test(n, x, m, y, ntype, &d, &z, &p, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_2_sample_ks_test (g08cdc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  printf("Test statistic D = %8.4f\n", d);
  printf("Z statistic      = %8.4f\n", z);
  printf("Tail probability = %8.4f\n", p);

 END:
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(state);

  return exit_status;
}