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

NAG CL Interface Introduction
Example description
/* nag_correg_ssqmat (g02buc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */

#include <nag.h>
#include <stdio.h>
#include <string.h>

int main(void) {
  /* Arrays */
  char nag_enum_mean[40], nag_enum_weight[40];
  double *c = 0, *v = 0, *wmean = 0, *wt = 0, *x = 0;
  double *wtptr = 0;
  /* Scalars */
  double alpha, sw;
  Integer exit_status, j, k, m, mm, n, pdx;
  Nag_SumSquare mean;
  Nag_Boolean weight;
  Nag_OrderType order;
  NagError fail;

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

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_correg_ssqmat (g02buc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  while (scanf("%39s %39s %" NAG_IFMT "%" NAG_IFMT "%*[^\n]", nag_enum_mean,
               nag_enum_weight, &m, &n) != EOF) {
    /* nag_enum_name_to_value (x04nac).
     * Converts NAG enum member name to value
     */
    mean = (Nag_SumSquare)nag_enum_name_to_value(nag_enum_mean);
    weight = (Nag_Boolean)nag_enum_name_to_value(nag_enum_weight);
    /* Allocate memory */
    if (!(c = NAG_ALLOC((m * m + m) / 2, double)) ||
        !(v = NAG_ALLOC((m * m + m) / 2, double)) ||
        !(wmean = NAG_ALLOC(m, double)) || !(wt = NAG_ALLOC(n, double)) ||
        !(x = NAG_ALLOC(n * m, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

#ifdef NAG_COLUMN_MAJOR
    pdx = n;
#else
    pdx = m;
#endif

    for (j = 1; j <= n; ++j)
      scanf("%lf", &wt[j - 1]);
    scanf("%*[^\n] ");
    for (j = 1; j <= n; ++j) {
      for (k = 1; k <= m; ++k)
        scanf("%lf", &X(j, k));
    }
    scanf("%*[^\n] ");

    if (weight)
      wtptr = wt;

    /* Calculate sums of squares and cross-products matrix */
    /* nag_correg_ssqmat (g02buc).
     * Computes a weighted sum of squares matrix
     */
    nag_correg_ssqmat(order, mean, n, m, x, pdx, wtptr, &sw, wmean, c, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_correg_ssqmat (g02buc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

    printf("\n");
    printf("Means\n");
    for (j = 1; j <= m; ++j)
      printf("%14.4f%s", wmean[j - 1], j % 6 == 0 || j == m ? "\n" : " ");
    if (wtptr) {
      printf("\n");
      printf("Weights\n");
      for (j = 1; j <= n; ++j)
        printf("%14.4f%s", wt[j - 1], j % 6 == 0 || j == n ? "\n" : " ");
      printf("\n");
    }

    /* Print the sums of squares and cross products matrix */
    /* nag_file_print_matrix_real_packed (x04ccc).
     * Print real packed triangular matrix (easy-to-use)
     */
    fflush(stdout);
    nag_file_print_matrix_real_packed(
        Nag_ColMajor, Nag_Upper, Nag_NonUnitDiag, m, c,
        "Sums of squares and cross-products", 0, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_file_print_matrix_real_packed (x04ccc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
    if (sw > 1.0) {
      /* Calculate the variance matrix */
      alpha = 1.0 / (sw - 1.0);
      mm = m * (m + 1) / 2;
      /* v[] = alpha*c[] using
       * nag_blast_daxpby (f16ecc)
       * Multiply real vector by scalar, preserving input vector
       */
      nag_blast_daxpby(mm, alpha, c, 1, 0.0, v, 1, &fail);

      /* Print the variance matrix */
      printf("\n");
      /* nag_file_print_matrix_real_packed (x04ccc), see above. */
      fflush(stdout);
      nag_file_print_matrix_real_packed(Nag_ColMajor, Nag_Upper,
                                        Nag_NonUnitDiag, m, v,
                                        "Variance matrix", 0, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_file_print_matrix_real_packed (x04ccc).\n%s\n",
               fail.message);
        exit_status = 1;
        goto END;
      }
    }

    NAG_FREE(c);
    NAG_FREE(v);
    NAG_FREE(wmean);
    NAG_FREE(wt);
    NAG_FREE(x);
  }

END:
  NAG_FREE(c);
  NAG_FREE(v);
  NAG_FREE(wmean);
  NAG_FREE(wt);
  NAG_FREE(x);

  return exit_status;
}