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

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

int main(void)
{
  /* Scalars */
  Integer exit_status = 0;
  double alpha, beta;
  Integer i, j, k, n, pda, pdc;
  /* Arrays */
  double *a = 0, *c = 0, *cr = 0;
  char nag_enum_arg[40];
  /* Nag Types */
  Nag_OrderType order;
  Nag_RFP_Store transr;
  Nag_UploType uplo;
  Nag_MatrixType matrix;
  Nag_TransType trans;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_dsfrk (f16yqc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &n, &k);
  pdc = n;

#ifdef NAG_COLUMN_MAJOR
  order = Nag_ColMajor;
  pda = n;
#define C(I, J) c[(J-1)*pdc + I-1]
#define A(I, J) a[(J-1)*pda + I-1]
#else
  order = Nag_RowMajor;
  pda = k;
#define C(I, J) c[(I-1)*pdc + J-1]
#define A(I, J) a[(I-1)*pda + J-1]
#endif

  if (!(c = NAG_ALLOC(pdc * n, double)) ||
      !(cr = NAG_ALLOC((n * (n + 1)) / 2, double)) ||
      !(a = NAG_ALLOC(n * k, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Nag_RFP_Store */
  scanf("%39s ", nag_enum_arg);
  transr = (Nag_RFP_Store) nag_enum_name_to_value(nag_enum_arg);
  /* Nag_UploType */
  scanf("%39s ", nag_enum_arg);
  uplo = (Nag_UploType) nag_enum_name_to_value(nag_enum_arg);
  /* Nag_TransType */
  scanf("%39s  %*[^\n] ", nag_enum_arg);
  trans = (Nag_TransType) nag_enum_name_to_value(nag_enum_arg);
  scanf("%lf%lf%*[^\n] ", &alpha, &beta);
  /* Read upper or lower triangle of matrix C from data file */
  if (uplo == Nag_Lower) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= i; j++) {
        scanf("%lf", &C(i, j));
      }
    }
  }
  else {
    for (i = 1; i <= n; i++) {
      for (j = i; j <= n; j++) {
        scanf("%lf", &C(i, j));
      }
    }
  }
  scanf("%*[^\n] ");

  /* Read matrix A from data file */
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= k; j++) {
      scanf("%lf", &A(i, j));
    }
  }

  /* Convert symmetric matrix C from full triangular storage to rectangular full
   * packed storage (in cr) using nag_dtrttf (f01vec).
   */
  nag_dtrttf(order, transr, uplo, n, c, pdc, cr, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dtrttf (f01vec).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  /* Perform the rank-k update of real symmetric matrix C by real matrix A
   * using nag_dsfrk (f16yqc).
   */
  nag_dsfrk(order, transr, uplo, trans, n, k, alpha, a, pda, beta, cr, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dsfrk (f16yqc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Convert C back from rectangular full packed (cr) to standard triangular
   * storage format (c) using nag_dtfttr (f01vgc).
   */
  nag_dtfttr(order, transr, uplo, n, cr, c, pdc, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dtfttr (f01vgc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  matrix = (uplo == Nag_Upper ? Nag_UpperMatrix : Nag_LowerMatrix);
  /* Print out the result, stored in the lower triangle of matrix C using
   * the easy-to-use print routine nag_gen_real_mat_print (x04cac).
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, matrix, Nag_NonUnitDiag, n, n, c, pdc,
                         "The Solution", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message);
    exit_status = 1;
  }

END:
  NAG_FREE(a);
  NAG_FREE(c);
  NAG_FREE(cr);
  return exit_status;
}