/* nag_rand_matrix_multi_normal (g05rzc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
/* Pre-processor includes */
#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg05.h>

#define X(I, J) x[(order == Nag_ColMajor)?(J*pdx + I):(I*pdx + J)]

int main(void)
{
  /* Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer i, j, lstate, lr, x_size;
  Integer *state = 0;
  Integer pdx;

  /* NAG structures */
  NagError fail;
  Nag_ModeRNG mode;

  /* Double scalar and array declarations */
  double *r = 0, *x = 0;

  /* Use column major order */
  Nag_OrderType order = Nag_RowMajor;

  /* Set the number of variables and variates */
  Integer m = 4;
  Integer n = 10;

  /* Input the covariance matrix */
  double c[] = { 1.69e0, 0.39e0, -1.86e0, 0.07e0,
    0.39e0, 98.01e0, -7.07e0, -0.71e0,
    -1.86e0, -7.07e0, 11.56e0, 0.03e0,
    0.07e0, -0.71e0, 0.03e0, 0.01e0
  };
  Integer pdc = 4;

  /* Input the means */
  double xmu[] = { 1.0e0, 2.0e0, -3.0e0, 0.0e0 };

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

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

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

  printf("nag_rand_matrix_multi_normal (g05rzc)"
         " Example Program Results\n\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;
  }

  pdx = (order == Nag_ColMajor) ? n : m;
  x_size = (order == Nag_ColMajor) ? pdx * m : pdx * n;

  /* Calculate the size of the reference vector */
  lr = m * m + m + 1;

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

  /* Initialize 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;
  }

  /* Set up reference vector and generate n numbers */
  mode = Nag_InitializeAndGenerate;
  nag_rand_matrix_multi_normal(order, mode, n, m, xmu,
                               c, pdc, r, lr, state, x, pdx, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_rand_matrix_multi_normal (g05rzc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Display the variates */
  for (i = 0; i < n; i++) {
    printf("  ");
    for (j = 0; j < m; j++)
      printf("%9.4f%s", X(i, j), (j + 1) % 10 ? " " : "\n");
    if (m % 10)
      printf("\n");
  }

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

  return exit_status;
}