/* nag_zhf_norm (f16ukc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

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

int main(void)
{
  /* Scalars */
  Integer exit_status = 0;
  double r_fro, r_inf, r_max, r_one;
  Integer i, j, n, pda;
  /* Arrays */
  Complex *a = 0, *ar = 0;
  char nag_enum_arg[40];
  /* Nag Types */
  Nag_OrderType order;
  Nag_RFP_Store transr;
  Nag_UploType uplo;
  NagError fail;

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

  INIT_FAIL(fail);

  printf("nag_zhf_norm (f16ukc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
  pda = n;
  if (!(a = NAG_ALLOC(pda * n, Complex)) ||
      !(ar = NAG_ALLOC((n * (n + 1)) / 2, Complex)))
  {
    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  %*[^\n] ", nag_enum_arg);
  uplo = (Nag_UploType) nag_enum_name_to_value(nag_enum_arg);
  /* Read upper or lower triangle of matrix A from data file */
  if (uplo == Nag_Lower) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= i; j++) {
        scanf(" ( %lf , %lf ) ", &A(i, j).re, &A(i, j).im);
      }
    }
  }
  else {
    for (i = 1; i <= n; i++) {
      for (j = i; j <= n; j++) {
        scanf(" ( %lf , %lf ) ", &A(i, j).re, &A(i, j).im);
      }
    }
  }
  /* Convert complex Hermitian matrix A from full to rectangular full packed
   * storage format (stored in ar) using nag_ztrttf (f01vfc).
   */
  nag_ztrttf(order, transr, uplo, n, a, pda, ar, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_ztrttf (f01vfc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\nNorms of Hermitian matrix stored in RFP format in ar:\n\n");

  /* Get, in turn, the 1-norm, infinity norm, Frobenius norm, and
   * largest absolute element of the Hermitian matrix A stored
   * in rectangular full packed format in ar using nag_zhf_norm (f16ukc).
   */
  nag_zhf_norm(order, Nag_OneNorm, transr, uplo, n, ar, &r_one, &fail);

  if (fail.code == NE_NOERROR) {
    printf("One norm           = %9.4f\n", r_one);
    nag_zhf_norm(order, Nag_InfNorm, transr, uplo, n, ar, &r_inf, &fail);
  }
  if (fail.code == NE_NOERROR) {
    printf("Infinity norm      = %9.4f\n", r_inf);
    nag_zhf_norm(order, Nag_FrobeniusNorm, transr, uplo, n, ar, &r_fro,
                 &fail);
  }
  if (fail.code == NE_NOERROR) {
    printf("Frobenius norm     = %9.4f\n", r_fro);
    nag_zhf_norm(order, Nag_MaxNorm, transr, uplo, n, ar, &r_max, &fail);
  }
  if (fail.code == NE_NOERROR) {
    printf("Maximum norm       = %9.4f\n", r_max);
  }
  else {
    printf("Error from nag_zhf_norm (f16ukc).\n%s\n", fail.message);
    exit_status = 1;
  }

END:
  NAG_FREE(a);
  NAG_FREE(ar);
  return exit_status;
}