/* nag_zheevx (f08fpc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <naga02.h>
#include <nagf08.h>
#include <nagx04.h>

int main(void)
{  
  /* Scalars */
  double        abstol, vl, vu;
  Integer       exit_status = 0, i, il = 0, iu = 0, j, m, n, pda, pdz;
  /* Arrays */
  Complex       *a = 0, *z = 0;
  double        *w = 0;
  Integer       *index = 0;
  /* Nag Types */
  Nag_OrderType order;
  NagError      fail, fail_print;
  
#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
#define Z(I, J) z[(J - 1) * pdz + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
#define Z(I, J) z[(I - 1) * pdz + J - 1]
  order = Nag_RowMajor;
#endif
  
  INIT_FAIL(fail);
  
  printf("nag_zheevx (f08fpc) Example Program Results\n\n");
  
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld%*[^\n]", &n);

  m = n;
  
#ifdef NAG_COLUMN_MAJOR
  pda = n;
  pdz = n;
#else
  pda = n;
  pdz = m;
#endif
  
  /* Allocate memory */
  if (!(a = NAG_ALLOC(n*n, Complex)) ||
      !(z = NAG_ALLOC(n*m, Complex)) ||
      !(w = NAG_ALLOC(n, double)) ||
      !(index = NAG_ALLOC(n, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  
  pda = n;
#ifdef NAG_COLUMN_MAJOR
  pdz = n;
#else
  pdz = m;
#endif
  
  /* Read the lower and upper bounds of the interval to be searched,
   * and read the upper triangular part of the matrix A from data file.
   */
  scanf("%lf%lf%*[^\n]", &vl, &vu);
  for (i = 1; i <= n; ++i)
    for (j = i; j <= n; ++j)
      scanf(" ( %lf , %lf )", &A(i, j).re, &A(i, j).im);
  scanf("%*[^\n]");
  
  /* Set the absolute error tolerance for eigenvalues.
   * With abstol set to zero, the default value is used instead.
   */
  abstol = 0.0;
  
  /* nag_zheevx (f08fpc).
   * Solve the Hermitian eigenvalue problem.
   */
  nag_zheevx(order, Nag_DoBoth, Nag_Interval, Nag_Upper, n, a, pda, vl,
             vu, il, iu, abstol, &m, w, z, pdz, index, &fail);
  if (fail.code != NE_NOERROR && fail.code != NE_CONVERGENCE)
    {
      printf("Error from nag_zheevx (f08fpc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
  
  /* nag_complex_divide (a02cdc).
   * Normalize the eigenvectors.
   */
  for(j=1; j<=m; j++)
    for(i=n; i>=1; i--)
      Z(i, j) = nag_complex_divide(Z(i, j),Z(1, j));
  
  /* Print solution */
  printf("Number of eigenvalues found =%5ld\n", m);
  
  printf("\nEigenvalues\n");
  for (j = 0; j < m; ++j)
    printf("%8.4f%s", w[j], (j+1)%8 == 0?"\n":" ");
  printf("\n\n");

  /* nag_gen_complx_mat_print (x04dac).
   * Print selected eigenvectors.
   */
  INIT_FAIL(fail_print);
  fflush(stdout);
  nag_gen_complx_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m, z,
                           pdz, "Selected eigenvectors", 0, &fail_print);
  if (fail_print.code != NE_NOERROR)
    {
      printf("Error from nag_gen_complx_mat_print (x04dac).\n%s\n",
             fail_print.message);
      exit_status = 1;
      goto END;
    }
  if (fail.code == NE_CONVERGENCE)
    {
      printf("eigenvectors failed to converge\n");
      printf("Indices of eigenvectors that did not converge\n");
      for (j = 0; j < m; ++j)
        printf("%8ld%s", index[j], (j+1)%8 == 0?"\n":" ");
    }
  
 END:
  NAG_FREE(a);
  NAG_FREE(z);
  NAG_FREE(w);
  NAG_FREE(index);
  
  return exit_status;
}

#undef A
#undef Z