/* nag_mv_canon_var (g03acc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 *
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nagg03.h>

#define X(I, J)   x[(I) *tdx + J]
#define E(I, J)   e[(I) *tde + J]
#define CVM(I, J) cvm[(I) *tdcvm + J]
#define CVX(I, J) cvx[(I) *tdcvx + J]
int main(void)
{

  Integer exit_status = 0, i, irx, j, m, n, ncv, ng;
  Integer nx, tdcvm, tdcvx, tde, tdx;
  Integer *ing = 0, *isx = 0, *nig = 0;
  double *cvm = 0, *cvx = 0, *e = 0, tol, *wt = 0, *x = 0;
  char nag_enum_arg[40];
  Nag_Weightstype weight;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_mv_canon_var (g03acc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "", &n);
  scanf("%" NAG_IFMT "", &m);
  scanf("%" NAG_IFMT "", &nx);
  scanf("%" NAG_IFMT "", &ng);
  scanf("%39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  weight = (Nag_Weightstype) nag_enum_name_to_value(nag_enum_arg);
  if (n >= nx + ng && m >= nx) {
    if (!(x = NAG_ALLOC(n * m, double)) ||
        !(wt = NAG_ALLOC(n, double)) ||
        !(ing = NAG_ALLOC(n, Integer)) ||
        !(e = NAG_ALLOC((MIN(nx, ng - 1)) * 6, double)) ||
        !(cvm = NAG_ALLOC(ng * nx, double)) ||
        !(cvx = NAG_ALLOC(nx * (ng - 1), double)) ||
        !(nig = NAG_ALLOC(ng, Integer)) || !(isx = NAG_ALLOC(m, Integer))

           )
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
    tdx = m;
    tde = 6;
    tdcvm = nx;
    tdcvx = ng - 1;
  }
  else {
    printf("Invalid n or m.\n");
    exit_status = 1;
    return exit_status;
  }
  if (weight == Nag_Weightsfreq || weight == Nag_Weightsvar) {
    for (i = 0; i < n; ++i) {
      for (j = 0; j < m; ++j)
        scanf("%lf", &X(i, j));
      scanf("%lf", &wt[i]);
      scanf("%" NAG_IFMT "", &ing[i]);
    }
  }
  else {
    for (i = 0; i < n; ++i) {
      for (j = 0; j < m; ++j)
        scanf("%lf", &X(i, j));
      scanf("%" NAG_IFMT "", &ing[i]);
    }
  }
  for (j = 0; j < m; ++j)
    scanf("%" NAG_IFMT "", &isx[j]);

  tol = 1e-6;
  /* nag_mv_canon_var (g03acc).
   * Canonical variate analysis
   */
  nag_mv_canon_var(weight, n, m, x, tdx, isx, nx, ing, ng, wt, nig,
                   cvm, tdcvm, e, tde, &ncv, cvx, tdcvx, tol, &irx, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_mv_canon_var (g03acc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("%s%2" NAG_IFMT "\n\n", "Rank of x = ", irx);
  printf("Canonical    Eigenvalues     Percentage   CHISQ"
         "        DF          SIG \n");
  printf("Correlations                 Variation\n");
  for (i = 0; i < ncv; ++i) {
    for (j = 0; j < 6; ++j)
      printf("%12.4f", E(i, j));
    printf("\n");
  }
  printf("\nCanonical Coefficients for X\n");
  for (i = 0; i < nx; ++i) {
    for (j = 0; j < ncv; ++j)
      printf("%9.4f", CVX(i, j));
    printf("\n");
  }
  printf("\nCanonical variate means\n");
  for (i = 0; i < ng; ++i) {
    for (j = 0; j < ncv; ++j)
      printf("%9.4f", CVM(i, j));
    printf("\n");
  }

END:
  NAG_FREE(x);
  NAG_FREE(wt);
  NAG_FREE(ing);
  NAG_FREE(e);
  NAG_FREE(cvm);
  NAG_FREE(cvx);
  NAG_FREE(nig);
  NAG_FREE(isx);

  return exit_status;
}