/* nag_nearest_correlation_k_factor (g02aec) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */

#include <nag.h>
#include <nag_stdlib.h>
#include <nagf16.h>
#include <nagg02.h>
#include <nagx04.h>

int main(void)
{

  /* Scalars */
  Integer               exit_status = 0;
  double                errtol, nrmpgd;
  Integer               feval, i, iter, j, k, pda, pdg, pdx, maxit, n;

  /* Arrays */
  double                *a = 0, *g = 0, *x = 0;

  /* Nag Types */
  Nag_OrderType         order;
  NagError              fail;

  INIT_FAIL(fail);

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J-1)*pda + I-1]
#define G(I, J) g[(J-1)*pdg + I-1]
#define X(I, J) x[(J-1)*pdx + I-1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I-1)*pda + J-1]
#define G(I, J) g[(I-1)*pdg + J-1]
#define X(I, J) x[(I-1)*pdx + J-1]
  order = Nag_RowMajor;
#endif

  /* Output preamble */
  printf("nag_nearest_correlation_k_factor (g02aec) ");
  printf("Example Program Results\n\n");
  fflush(stdout);

  /* Skip heading in data file */
  scanf("%*[^\n]");
  /* Read in the problem size */
  scanf("%ld%*[^\n]", &n);

  pda = n;
  pdg = n;
  pdx = n;
  if (
      !(a = NAG_ALLOC((pda)*(n), double)) ||
      !(g = NAG_ALLOC((pdg)*(n), double)) ||
      !(x = NAG_ALLOC((pdx)*(n), double))
      )
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  /* Read in the matrix g */
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      scanf("%lf", &G(i, j));
  scanf("%*[^\n] ");

  /* Use the defaults for errtol and maxit */
  errtol = 0.0;
  maxit = 0;
  /* Set k value*/
  k = 2;

  /* nag_nearest_correlation_k_factor (g02aec).
   * Computes the nearest correlation matrix with k-factor structure
   * to a real square matrix
   */
   nag_nearest_correlation_k_factor(order, g, pdg, n, k, errtol, maxit, x,
                                    pdx, &iter, &feval, &nrmpgd, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_gen_real_mat_print (x04cac).
   * Print real general matrix (easy-to-use)
   */
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, k, x,
                         pdx, "Factor Loading Matrix X", NULL, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  printf("\nNumber of steps taken: %ld\n", iter);
  printf("Number of function evaluations: %ld\n\n", feval);
  fflush(stdout);

  /* Generate Nearest k factor correlation matrix
   * nag_dgemm (f16yac) performs matrix-matrix multiplication for a
   * real general matrix
   */
  nag_dgemm(order, Nag_NoTrans, Nag_Trans, n, n, k, 1.0, x, pdx, x, pdx,
            0.0, a, pda, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
  for (i = 1; i <= n; i++)
    A(i, i) = 1.0;

  /* nag_gen_real_mat_print (x04cac).
   * Print real general matrix (easy-to-use)
   */
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, a,
                         pda, "Nearest Correlation Matrix", NULL, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("%s\n", fail.message);
      exit_status = 1;
    }

 END:
  NAG_FREE(a);
  NAG_FREE(g);
  NAG_FREE(x);
  return exit_status;
}