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

int main(void)
{
  /*Logical scalar and array declarations */
  Nag_Boolean exact;
  Nag_IncludeMean mean;
  Nag_Boolean *parhld = 0;
  /*Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer i, ip, iprint, iq, ishow, j, k, kmax, maxcal, n;
  Integer niter, npar, pdcm, pdqq, pdw;
  /*Double scalar and array declarations */
  double cgetol, rlogl;
  double *cm = 0, *g = 0, *par = 0, *qq = 0, *v = 0, *w = 0;
  /*Character scalar and array declarations */
  char smean[40];
  /*NAG Types */
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_tsa_varma_estimate (g13ddc) Example Program Results\n");
  /*     Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%39s%*[^\n] ",
        &k, &ip, &iq, &n, smean);
  npar = (ip + iq) * k * k;
  kmax = 3;
  /*
   * nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  mean = (Nag_IncludeMean) nag_enum_name_to_value(smean);
  if (mean == Nag_MeanInclude)
    npar = npar + k;
  printf("\n");
  pdcm = npar;
  pdqq = kmax;
#define QQ(I, J) qq[(J-1)*pdqq + I-1]
  pdw = kmax;
#define W(I, J)  w[(J-1)*pdw + I-1]
  if (!(cm = NAG_ALLOC(npar * npar, double)) ||
      !(g = NAG_ALLOC(npar, double)) ||
      !(par = NAG_ALLOC(npar, double)) ||
      !(qq = NAG_ALLOC(kmax * k, double)) ||
      !(v = NAG_ALLOC(kmax * n, double)) ||
      !(w = NAG_ALLOC(kmax * n, double)) ||
      !(parhld = NAG_ALLOC(npar, Nag_Boolean)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < npar; i++) {
    par[i] = 0.00e0;
    parhld[i] = Nag_FALSE;
  }
  /*     Set all elements of Q to zero to use the covariance matrix */
  /*     between the K time series as the initial estimate of the   */
  /*     covariance matrix                                          */
  for (j = 1; j <= k; j++) {
    for (i = j; i <= k; i++)
      QQ(i, j) = 0.00e0;
  }
  for (i = 1; i <= k; i++) {
    for (j = 1; j <= n; j++)
      scanf("%lf ", &W(i, j));
  }
  scanf("%*[^\n] ");
  parhld[2] = Nag_TRUE;
  exact = Nag_TRUE;
  iprint = (-(1));
  cgetol = 0.00010e0;
  maxcal = 40 * npar * (npar + 5);
  ishow = 2;
  /*
   * nag_tsa_varma_estimate (g13ddc)
   * Multivariate time series, estimation of varma model
   */
  fflush(stdout);
  nag_tsa_varma_estimate(k, n, ip, iq, mean, par, npar, qq, kmax, w, parhld,
                         exact, iprint, cgetol, maxcal, ishow, 0,
                         &niter, &rlogl, v, g, cm, pdcm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_tsa_varma_estimate (g13ddc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

END:
  NAG_FREE(cm);
  NAG_FREE(g);
  NAG_FREE(par);
  NAG_FREE(qq);
  NAG_FREE(v);
  NAG_FREE(w);
  NAG_FREE(parhld);

  return exit_status;
}