/* nag_mv_promax (g03bdc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 9, 2009.
 */
/* Pre-processor includes */
#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg03.h>
#include <nagx04.h>

int main(void)
{
  /*Integer scalar and array declarations */
  Integer             exit_status = 0;
  Integer             i, iter, j, m, maxit, n;
  Integer             pdfp, pdfs, pdphi, pdr, pdro, pdx;
  /*Double scalar and array declarations */
  double              acc, g, power;
  double              *fp = 0, *fs = 0, *phi = 0, *r = 0, *ro = 0, *x = 0;
  /*Character scalar and array declarations */
  char                sstand[40];
  /*NAG types */
  Nag_OrderType       order;
  Nag_RotationLoading stand;
  NagError            fail;

  INIT_FAIL(fail);

  printf("%s\n", "nag_mv_promax (g03bdc) Example Program Results");
  /* Skip heading in data file*/
  scanf("%*[^\n] ");
  scanf("%39s %ld%ld%lf%*[^\n] ", sstand, &n, &m, &power);
  stand = (Nag_RotationLoading) nag_enum_name_to_value(sstand);

  pdfp = m;
  #define FP(I, J)  fp[(I-1)*pdfp + J-1]
  pdfs = m;
  #define FS(I, J)  fs[(I-1)*pdfs + J-1]
  pdphi = m;
  #define PHI(I, J) phi[(I-1)*pdphi + J-1]
  pdr = m;
  #define R(I, J)   r[(I-1)*pdr + J-1]
  pdro = m;
  #define RO(I, J)  ro[(I-1)*pdro + J-1]
  pdx = pdfp;
  #define X(I, J)   x[(I-1)*pdx + J-1]

  if (!(fp = NAG_ALLOC(pdfp*n, double)) ||
      !(fs = NAG_ALLOC(pdfs*n, double)) ||
      !(phi = NAG_ALLOC(pdphi*m, double)) ||
      !(r = NAG_ALLOC(pdr*m, double)) ||
      !(ro = NAG_ALLOC(pdro*m, double)) ||
      !(x = NAG_ALLOC(pdx*n, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  /* Read loadings matrix.*/
  for (i = 1; i <= n; i++)
    {
      for (j = 1; j <= m; j++)
        scanf("%lf ", &FP(i, j));
    }
  scanf("%*[^\n] ");
  /*
   * nag_mv_orthomax (g03bac)
   * Orthogonal rotations
   */
  g = 1.0e0;
  acc = 1.0e-5;
  maxit = 200;
  nag_mv_orthomax(stand, g, n, m, fp, pdx, x, ro, pdro, acc, maxit, &iter,
                  &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_mv_orthomax (g03bac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  /*
   * nag_mv_promax (g03bdc)
   * ProMax rotations
   */
  nag_mv_promax(stand, n, m, x, pdx, ro, pdro, power, fp, pdfp, r, pdr,
                phi, pdphi, fs, pdfs, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_mv_promax (g03bdc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
  printf("\n");
  /*
   * nag_gen_real_mat_print (x04cac)
   * Print real general matrix (easy-to-use)
   */
  order = Nag_RowMajor;
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m,
                         fp, pdfp, "Factor pattern", 0, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  printf("\n");
  /*
   * nag_gen_real_mat_print (x04cac)
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, m,
                         r, pdr, "ProMax rotation", 0, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  printf("\n");
  /*
   * nag_gen_real_mat_print (x04cac)
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, m,
                         phi, pdphi, "Inter-factor correlations", 0,
                         &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  printf("\n");
  /*
   * nag_gen_real_mat_print (x04cac)
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m,
                         fs, pdfs, "Factor structure", 0, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

 END:
  NAG_FREE(fp);
  NAG_FREE(fs);
  NAG_FREE(phi);
  NAG_FREE(r);
  NAG_FREE(ro);
  NAG_FREE(x);

  return exit_status;
}