/* nag_mv_prin_comp (g03aac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 5, 1998.
 * Mark 8 revised, 2004.
 *
 */

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

#define X(I, J) x[(I) *tdx + J]
#define P(I, J) p[(I) *tdp + J]
#define E(I, J) e[(I) *tde + J]
#define V(I, J) v[(I) *tdv + J]

int main(void)
{
  Integer            exit_status = 0, i, *isx = 0, j, m, n, nvar, tde = 6, tdp,
                     tdv, tdx;
  Nag_PrinCompMat    matrix;
  Nag_PrinCompScores scores;
  Nag_Boolean        weight;
  char               nag_enum_arg[40];
  double             *e = 0, *p = 0, *s = 0, *v = 0, *wt = 0, *wtptr = 0;
  double             *x = 0;
  NagError           fail;

  INIT_FAIL(fail);

  printf("nag_mv_prin_comp (g03aac) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  matrix = (Nag_PrinCompMat) nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s", nag_enum_arg);
  scores = (Nag_PrinCompScores) nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s", nag_enum_arg);
  weight = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg);
  scanf("%ld", &n);
  scanf("%ld", &m);

  if (n >= 2 && m >= 1)
    {
      if (!(x = NAG_ALLOC((n)*(m), double)) ||
          !(wt = NAG_ALLOC(n, double)) ||
          !(s = NAG_ALLOC(m, double)) ||
          !(isx = NAG_ALLOC(m, Integer)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }
      tdx = m;
    }
  else
    {
      printf("Invalid n or m.\n");
      exit_status = 1;
      return exit_status;
    }
  if (!weight)
    {
      for (i = 0; i < n; ++i)
        {
          for (j = 0; j < m; ++j)
            scanf("%lf", &X(i, j));
        }
    }
  else
    {
      for (i = 0; i < n; ++i)
        {
          for (j = 0; j < m; ++j)
            scanf("%lf", &X(i, j));
          scanf("%lf", &wt[i]);
        }
      wtptr = wt;
    }
  for (j = 0; j < m; ++j)
    {
      scanf("%ld", &isx[j]);
    }
  scanf("%ld", &nvar);
  if (nvar >= 1 && nvar <= MIN(n-1, m))
    {
      if (!(p = NAG_ALLOC(nvar*nvar, double)) ||
          !(e = NAG_ALLOC(nvar*6, double)) ||
          !(v = NAG_ALLOC(n*nvar, double)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }
      tdp = nvar;
      tde = 6;
      tdv = nvar;
    }
  else
    {
      printf("Invalid nvar.\n");
      exit_status = 1;
      goto END;
    }

  if (matrix == Nag_MatStandardised)
    {
      for (j = 0; j < m; ++j)
        scanf("%lf", &s[j]);
    }

  /* nag_mv_prin_comp (g03aac).
   * Principal component analysis
   */
  nag_mv_prin_comp(matrix, scores, n, m, x, tdx, isx, s, wtptr, nvar,
                   e, tde, p, tdp, v, tdv, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_mv_prin_comp (g03aac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  printf(
          "Eigenvalues  Percentage  Cumulative     Chisq      DF     Sig\n");
  printf("               variation   variation\n\n");
  for (i = 0; i < nvar; ++i)
    {
      for (j = 0; j < 6; ++j)
        printf("%11.4f", E(i, j));
      printf("\n");
    }
  printf("\nPrincipal component loadings \n\n");
  for (i = 0; i < nvar; ++i)
    {
      for (j = 0; j < nvar; ++j)
        printf("%9.4f", P(i, j));
      printf("\n");
    }
  printf("\nPrincipal component scores \n\n");
  for (i = 0; i < n; ++i)
    {
      printf("%2ld", i+1);
      for (j = 0; j < nvar; ++j)
        printf("%9.3f", V(i, j));
      printf("\n");
    }

 END:
  NAG_FREE(x);
  NAG_FREE(wt);
  NAG_FREE(s);
  NAG_FREE(isx);
  NAG_FREE(p);
  NAG_FREE(e);
  NAG_FREE(v);

  return exit_status;
}