/* nag_sum_sqs_update (g02btc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

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

int main(void)
{
  /* Arrays */
  char nag_enum_arg[40];
  double *c = 0, *v = 0, *x = 0, *xbar = 0;
  /* Scalars */
  double alpha, sw, wt;
  Integer exit_status, i, j, m, mm, n, nprint, incx;
  Nag_SumSquare mean;
  NagError fail;

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_sum_sqs_update (g02btc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  incx = 1;
  while (scanf("%39s %" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n]",
               nag_enum_arg, &m, &n, &nprint) != EOF) {
    /* nag_enum_name_to_value (x04nac).
     * Converts NAG enum member name to value
     */
    mean = (Nag_SumSquare) nag_enum_name_to_value(nag_enum_arg);
    /* Allocate memory */
    if (!(c = NAG_ALLOC((m * m + m) / 2, double)) ||
        !(v = NAG_ALLOC((m * m + m) / 2, double)) ||
        !(x = NAG_ALLOC(m * incx, double)) || !(xbar = NAG_ALLOC(m, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    sw = 0.0;
    for (i = 1; i <= n; ++i) {
      scanf("%lf", &wt);
      for (j = 1; j <= m; ++j)
        scanf("%lf", &x[j - 1]);
      scanf("%*[^\n] ");

      /* Calculate the sums of squares and cross-products matrix */
      /* nag_sum_sqs_update (g02btc).
       * Update a weighted sum of squares matrix with a new
       * observation
       */
      nag_sum_sqs_update(mean, m, wt, x, incx, &sw, xbar, c, &fail);

      if (fail.code != NE_NOERROR) {
        printf("Error from nag_sum_sqs_update (g02btc).\n%s\n", fail.message);
        exit_status = 1;
        goto END;
      }

      if (i % nprint == 0 || i == n) {
        printf("\n");
        printf("---------------------------------------------\n");
        printf("Observation: %4" NAG_IFMT "      Weight = %13.4f\n", i, wt);
        printf("\n");
        printf("---------------------------------------------\n");
        printf("\n");

        printf("Means\n");
        for (j = 1; j <= m; ++j)
          printf("%14.4f%s", xbar[j - 1], j % 4 == 0 || j == m ? "\n" : " ");
        printf("\n");

        /* Print the sums of squares and cross products matrix */
        /* nag_pack_real_mat_print (x04ccc).
         * Print real packed triangular matrix (easy-to-use)
         */
        fflush(stdout);
        nag_pack_real_mat_print(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_pack_real_mat_print (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_daxpby (f16ecc)
           * Multiply real vector by scalar, preserving input vector
           */
          nag_daxpby(mm, alpha, c, 1, 0.0, v, 1, &fail);

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

    NAG_FREE(c);
    NAG_FREE(v);
    NAG_FREE(x);
    NAG_FREE(xbar);
  }

END:
  NAG_FREE(c);
  NAG_FREE(v);
  NAG_FREE(x);
  NAG_FREE(xbar);

  return exit_status;
}