Example description
/* nag_zhpev (f08gnc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.2, 2017.
 */

#include <math.h>
#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf08.h>
#include <nagx02.h>
#include <nagx04.h>

int main(void)
{
  /* Scalars */
  double eerrbd, eps;
  Integer exit_status = 0, i, j, n;
  /* Arrays */
  char nag_enum_arg[40];
  Complex *ap = 0, *dummy = 0;
  double *w = 0;
  /* Nag Types */
  Nag_OrderType order;
  Nag_UploType uplo;
  NagError fail;

#ifdef NAG_COLUMN_MAJOR
#define AP_UPPER(I, J) ap[J * (J - 1) / 2 + I - 1]
#define AP_LOWER(I, J) ap[(2 * n - J) * (J - 1) / 2 + I - 1]
  order = Nag_ColMajor;
#else
#define AP_LOWER(I, J) ap[I * (I - 1) / 2 + J - 1]
#define AP_UPPER(I, J) ap[(2 * n - I) * (I - 1) / 2 + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_zhpev (f08gnc) Example Program Results\n\n");

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

  /* Read uplo */
  scanf("%39s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  uplo = (Nag_UploType) nag_enum_name_to_value(nag_enum_arg);

  /* Allocate memory */
  if (!(ap = NAG_ALLOC(n * (n + 1) / 2, Complex)) ||
      !(dummy = NAG_ALLOC(1, Complex)) || !(w = NAG_ALLOC(n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the upper or lower triangular part of the matrix A from data file */
  if (uplo == Nag_Upper) {
    for (i = 1; i <= n; ++i)
      for (j = i; j <= n; ++j)
        scanf(" ( %lf , %lf )", &AP_UPPER(i, j).re, &AP_UPPER(i, j).im);
    scanf("%*[^\n]");
  }
  else if (uplo == Nag_Lower) {
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= i; ++j)
        scanf(" ( %lf , %lf )", &AP_LOWER(i, j).re, &AP_LOWER(i, j).im);
    scanf("%*[^\n]");
  }

  /* nag_zhpev (f08gnc).
   * Solve the Hermitian eigenvalue problem.
   */
  nag_zhpev(order, Nag_EigVals, uplo, n, ap, w, dummy, 1, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zhpev (f08gnc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print solution */
  printf("Eigenvalues\n");
  for (j = 0; j < n; ++j)
    printf("%8.4f%s", w[j], (j + 1) % 8 == 0 ? "\n" : " ");
  printf("\n");

  /* Get the machine precision, eps, using nag_machine_precision (X02AJC)
   * and compute the approximate error bound for the computed eigenvalues.
   * Note that for the 2-norm, ||A|| = max{|w[i]|,i=0..n-1}), and since 
   * the eigenvalues are in ascending order: ||A|| = max(|w[0]|,|w[n-1]|).
   */
  eps = nag_machine_precision;
  eerrbd = eps * MAX(fabs(w[0]), fabs(w[n - 1]));

  /* Print the approximate error bound for the eigenvalues */
  printf("\nError estimate for the eigenvalues\n");
  printf("%11.1e\n", eerrbd);

END:
  NAG_FREE(ap);
  NAG_FREE(dummy);
  NAG_FREE(w);

  return exit_status;
}

#undef AP_UPPER
#undef AP_LOWER