/* nag_mv_promax (g03bdc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */
/* 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 %" NAG_IFMT "%" NAG_IFMT "%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;
  pdphi = m;
  pdr = m;
  pdro = m;
  pdx = pdfp;

  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;
}