NAG Library Manual, Mark 30.3
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_correg_pls_pred (g02ldc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.3, 2024.
 */
/* Pre-processor includes */
#include <math.h>
#include <nag.h>
#include <stdio.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_correg_pls_pred (g02ldc) Example Program Results\n");
  /* Skip header in data file. */
  scanf("%*[^\n] ");
  /* Read data values. */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%39s %39s %" NAG_IFMT "%" NAG_IFMT
        "%*[^\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;
  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;
  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("%" NAG_IFMT " ", &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_correg_pls_pred (g02ldc)
   * Partial least squares
   */
  nag_correg_pls_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_correg_pls_pred (g02ldc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /*
   * nag_file_print_matrix_real_gen (x04cac)
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                                 my, yhat, pdyhat, "YHAT", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (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;
}