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

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

int main(void)
{
  /* Scalars */
  double abnrm, eps, rcnd, tol;
  Integer i, ihi, ilo, j, n, pda, pdvl, pdvr;
  Integer exit_status = 0;
  /* Arrays */
  Complex *a = 0, *vl = 0, *vr = 0, *w = 0;
  double *rconde = 0, *rcondv = 0, *scale = 0;

  /* Nag Types */
  NagError fail;
  Nag_OrderType order;

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

  INIT_FAIL(fail);

  printf("nag_zgeevx (f08npc) Example Program Results\n");

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

  pda = n;
  pdvl = n;
  pdvr = n;
  /* Allocate memory */
  if (!(a = NAG_ALLOC(n * n, Complex)) ||
      !(vl = NAG_ALLOC(n * n, Complex)) ||
      !(vr = NAG_ALLOC(n * n, Complex)) ||
      !(w = NAG_ALLOC(n, Complex)) ||
      !(rconde = NAG_ALLOC(n, double)) ||
      !(rcondv = NAG_ALLOC(n, double)) || !(scale = NAG_ALLOC(n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the matrix A from data file */
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &A(i, j).re, &A(i, j).im);
  scanf("%*[^\n]");

  /* Solve the eigenvalue problem using nag_zgeevx (f08npc). */
  nag_zgeevx(order, Nag_BalanceBoth, Nag_LeftVecs, Nag_RightVecs,
             Nag_RCondBoth, n, a, pda, w, vl, pdvl, vr, pdvr, &ilo, &ihi,
             scale, &abnrm, rconde, rcondv, &fail);

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

  /* Compute the machine precision */
  eps = nag_machine_precision;
  tol = eps * abnrm;

  /* Normalize the eigenvectors */
  for (j = 0; j < n; j++)
    for (i = n - 1; i >= 0; i--)
      VR(i, j) = nag_complex_divide(VR(i, j), VR(0, j));

  /* Print the eigenvalues/vectors, associated condition number and bounds. */
  for (j = 0; j < n; ++j) {
    /* Print information on j-th eigenvalue */
    printf("\n\nEigenvalue %3" NAG_IFMT "%14s= ", j + 1, "");
    if (w[j].im == 0.)
      printf("%12.4e\n", w[j].re);
    else
      printf("(%13.4e, %13.4e)\n", w[j].re, w[j].im);

    rcnd = rconde[j];
    printf("\nReciprocal condition number = %9.1e\n", rcnd);
    if (rcnd > 0.0)
      printf("Error bound                 = %9.1e\n", tol / rcnd);
    else
      printf("Error bound is infinite\n");

    /* Print information on j-th eigenvector */
    printf("\nEigenvector %2" NAG_IFMT "\n", j + 1);
    for (i = 0; i < n; ++i)
      printf("%30s(%13.4e, %13.4e)\n", "", VR(i, j).re, VR(i, j).im);

    rcnd = rcondv[j];
    printf("\nReciprocal condition number = %9.1e\n", rcnd);
    if (rcnd > 0.0)
      printf("Error bound                 = %9.1e\n", tol / rcnd);
    else
      printf("Error bound is infinite\n");
  }

END:
  NAG_FREE(a);
  NAG_FREE(vl);
  NAG_FREE(vr);
  NAG_FREE(w);
  NAG_FREE(rconde);
  NAG_FREE(rcondv);
  NAG_FREE(scale);

  return exit_status;
}

#undef A
#undef VR