/* nag_tsa_multi_cross_corr (g13dmc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2002.
 */

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg13.h>

static void cprint(Integer, Integer, Integer, Integer,
                   double *, double *);

int main(void)
{
  /* Scalars */
  Integer       exit_status, i, j, k, m, n, kmax;
  NagError      fail;
  Nag_CovOrCorr matrix;

  /* Arrays */
  double        *r0 = 0, *r = 0, *w = 0, *wmean = 0;

#define W(I, J) w[(J-1)*kmax + I - 1]

  INIT_FAIL(fail);

  exit_status = 0;

  printf("nag_tsa_multi_cross_corr (g13dmc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%ld%ld%ld%*[^\n] ", &k, &n, &m);

  if (k > 0 && n >= 1 && m >= 1)
    {
      /* Allocate arrays */
      if (!(r0 = NAG_ALLOC(k * k, double)) ||
          !(r = NAG_ALLOC(k * k * m, double)) ||
          !(w = NAG_ALLOC(k * n, double)) ||
          !(wmean = NAG_ALLOC(k, double)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }

      kmax = k;

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

      matrix = Nag_AutoCorr;

      /* nag_tsa_multi_cross_corr (g13dmc).
       * Multivariate time series, sample cross-correlation or
       * cross-covariance matrices
       */
      nag_tsa_multi_cross_corr(matrix, k, n, m, w, wmean, r0, r, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_tsa_multi_cross_corr (g13dmc).\n%s\n",
                 fail.message);
          exit_status = 1;
          goto END;
        }
      cprint(k, n, k, m, wmean, r);
    }

 END:
  NAG_FREE(r0);
  NAG_FREE(r);
  NAG_FREE(w);
  NAG_FREE(wmean);

  return exit_status;
}

/* Print the correlation matrices and indicator symbols. */
static void cprint(Integer k, Integer n, Integer ik, Integer m,
                   double *wmean, double *r)
{
  /* Scalars */
  double  c1, c2, c3, c5, c6, c7, cnst, sum;
  Integer i2, i, j, lf, llf, ii;

  /* Arrays */
  char    rec[7][80];

#define R(I, J, K) r[((K-1)*ik + (J-1))*ik + I - 1]

  cnst = 1.0 / sqrt((double) n);

  printf("\n");
  printf(" THE MEANS\n");
  printf(" ---------\n");
  printf("  ");
  for (i = 1; i <= k; ++i)
    {
      printf("%10.3f", wmean[i-1]);
      if (i % 2 == 0 || i == k)
        printf("\n");
    }

  printf("\n");
  printf(" CROSS-CORRELATION MATRICES\n");
  printf(" --------------------------\n");
  for (lf = 1; lf <= m; ++lf)
    {
      printf("\n");
      printf(" Lag = %2ld\n", lf);
      for (i = 1; i <= k; i++)
        {
          for (j = 1; j <= k; j++)
            printf("%9.3f", R(i, j, lf));
          printf("\n");
        }
    }

  /* Print indicator symbols to indicate significant elements. */
  printf("\n");
  printf(" Standard error = 1 / SQRT(N) = %5.3f\n", cnst);
  printf("\n");
  printf(" TABLES OF INDICATOR SYMBOLS\n");
  printf(" ---------------------------\n");
  printf("\n");
  printf(" For Lags 1 to %2ld\n", m);
  printf("\n");

  /* Set up the critical values */
  c1 = cnst * 3.29;
  c2 = cnst * 2.58;
  c3 = cnst * 1.96;
  c5 = -c3;
  c6 = -c2;
  c7 = -c1;

  for (i = 1; i <= k; ++i)
    {
      for (j = 1; j <= k; ++j)
        {
          printf("\n");
          printf("\n");
          if (i == j)
            printf("Auto-correlation function for series %2ld\n",
                   i);
          else
            printf("Cross-correlation function for series %2ld"
                   " and series%2ld\n", i, j);
          printf("\n");

          /* Clear the last plot with blanks */
          sprintf(&rec[0][0], "              0.005  :");
          sprintf(&rec[1][0], "        +     0.01   :");
          sprintf(&rec[2][0], "              0.05   :");
          sprintf(&rec[3][0],
                  "   Sig. Level        : - - - - - - - - - -  Lags");
          sprintf(&rec[4][0], "              0.05   :");
          sprintf(&rec[5][0], "        -     0.01   :");
          sprintf(&rec[6][0], "              0.005  :");
          for (i2 = 0; i2 < 7; ++i2)
            {
              for (ii = strlen(&rec[i2][0]); ii < 80; ii++)
                rec[i2][ii] = ' ';
            }

          for (lf = 1; lf <= m; ++lf)
            {
              llf = lf * 2 + 21;
              sum = R(i, j, lf);

              /* Check for significance */
              if (sum > c1)
                rec[0][llf] = '*';
              if (sum > c2)
                rec[1][llf] = '*';
              if (sum > c3)
                rec[2][llf] = '*';
              if (sum < c5)
                rec[4][llf] = '*';
              if (sum < c6)
                rec[5][llf] = '*';
              if (sum < c7)
                rec[6][llf] = '*';
            }

          /* Print */
          for (i2 = 0; i2 < 7; ++i2)
            {
              /* Terminate the string */
              for (ii = 80; ii > 1 && rec[i2][ii-1] == ' '; ii--) ;
              rec[i2][ii] = '\0';
              /* Print the string */
              printf("%s\n", &rec[i2][0]);
            }
        }
    }

  return;
}