/* nag_pls_orth_scores_pred (g02ldc) 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 <nagg02.h>
#include <nagx04.h>

int main(void)
{
  /*Integer scalar and array declarations */
  Integer             exit_status = 0;
  Integer             i, ip, j, l, my, mz, n;
  Integer             pdb, pdyhat, pdz;
  Integer             *isz = 0;
  /*Double scalar and array declarations */
  double              *b = 0, *xbar = 0, *xstd = 0, *ybar = 0, *yhat = 0;
  double              *ystd = 0, *z = 0;
  /*Character scalar and array declarations */
  char                siscale[40], sorig[40];
  /*NAG Types */
  Nag_OrderType       order;
  Nag_ScalePredictor  iscale;
  Nag_EstimatesOption orig;
  NagError            fail;

  INIT_FAIL(fail);

  printf("nag_pls_orth_scores_pred (g02ldc) Example Program Results\n");
  /* Skip header in data file.*/
  scanf("%*[^\n] ");
  /* Read data values.*/
  scanf("%ld%ld%39s %39s %ld%ld%*[^\n] ",
        &ip, &my, sorig, siscale, &n, &mz);
  orig = (Nag_EstimatesOption) nag_enum_name_to_value(sorig);
  iscale = (Nag_ScalePredictor) nag_enum_name_to_value(siscale);

    #ifdef NAG_COLUMN_MAJOR
  pdb = ((orig == Nag_EstimatesStand)?ip:1+ip);
    #define B(I, J)    b[(J-1)*pdb + I-1]
  pdyhat = n;
    #define YHAT(I, J) yhat[(J-1)*pdyhat + I-1]
  pdz = n;
    #define Z(I, J)    z[(J-1)*pdz + I-1]
  order = Nag_ColMajor;
    #else
  pdb = my;
    #define B(I, J)    b[(I-1)*pdb + J-1]
  pdyhat = my;
    #define YHAT(I, J) yhat[(I-1)*pdyhat + J-1]
  pdz = mz;
    #define Z(I, J)    z[(I-1)*pdz + J-1]
  order = Nag_RowMajor;
    #endif
  if (!(b = NAG_ALLOC(pdb*(order == Nag_RowMajor?(1+ip):my), double)) ||
      !(xbar = NAG_ALLOC(ip, double)) ||
      !(xstd = NAG_ALLOC(ip, double)) ||
      !(ybar = NAG_ALLOC(my, double)) ||
      !(yhat = NAG_ALLOC(pdyhat*(order == Nag_RowMajor?n:my), double)) ||
      !(ystd = NAG_ALLOC(my, double)) ||
      !(z = NAG_ALLOC(pdz*(order == Nag_RowMajor?n:mz), double)) ||
      !(isz = NAG_ALLOC(mz, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  /* Read prediction x-data*/
  for (i = 1; i <= n; i++)
    {
      for (j = 1; j <= mz; j++)
        scanf("%lf ", &Z(i, j));
    }
  scanf("%*[^\n] ");
  /* Read elements of isz*/
  for (j = 0; j < mz; j++)
    scanf("%ld ", &isz[j]);
  scanf("%*[^\n] ");
  /* Read parameter estimates*/
  l = ip;
  if (orig != Nag_EstimatesStand)
    {
      l = l+1;
    }
  for (j = 1; j <= l; j++)
    {
      for (i = 1; i <= my; i++)
        scanf("%lf ", &B(j, i));
    }
  scanf("%*[^\n] ");
  /* Read means*/
  if (orig == Nag_EstimatesStand)
    {
      for (j = 0; j < ip; j++)
        scanf("%lf ", &xbar[j]);
      scanf("%*[^\n] ");
      for (l = 0; l < my; l++)
        scanf("%lf ", &ybar[l]);
      scanf("%*[^\n] ");
    }
  /* Read scalings*/
  if ((orig == Nag_EstimatesStand) && (iscale != Nag_PredNoScale))
    {
      for (j = 0; j < ip; j++)
        scanf("%lf ", &xstd[j]);
      scanf("%*[^\n] ");
      for (l = 0; l < my; l++)
        scanf("%lf ", &ystd[l]);
      scanf("%*[^\n] ");
    }
  /* Calculate predictions*/
  /*
   * nag_pls_orth_scores_pred (g02ldc)
   * Partial least-squares
   */
  nag_pls_orth_scores_pred(order, ip, my, orig, xbar, ybar, iscale, xstd,
                           ystd, b, pdb, n, mz, isz, z, pdz, yhat, pdyhat,
                           &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_pls_orth_scores_pred (g02ldc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  /*
   * 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, my,
                         yhat, pdyhat, "YHAT", 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(b);
  NAG_FREE(xbar);
  NAG_FREE(xstd);
  NAG_FREE(ybar);
  NAG_FREE(yhat);
  NAG_FREE(ystd);
  NAG_FREE(z);
  NAG_FREE(isz);

  return exit_status;
}