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

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

int main(void)
{
  /* Scalars */
  Integer       exit_status = 0, i, j, n, pda, pdvr;

  /* Arrays */
  Complex       *a = 0, *vr = 0, *w = 0;
  Complex       dummy[1];

  /* 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_zgeev (f08nnc) Example Program Results\n\n");

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

  pda = n;
  pdvr = n;
  /* Allocate memory */
  if (!(a  = NAG_ALLOC(n * n, Complex)) ||
      !(vr = NAG_ALLOC(n * n, Complex)) ||
      !(w  = NAG_ALLOC(n, Complex)))
    {
      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]");

  /* Compute the eigenvalues and right eigenvectors of A 
     using nag_zgeev (f08nnc). */
  nag_zgeev(order, Nag_NotLeftVecs, Nag_RightVecs, n, a, pda, w, dummy, 1,
            vr, pdvr, &fail);

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

  /* Print eigenvalues and right eigenvectors. */
  for (j = 0; j < n; ++j)
    {
      printf("\nEigenvalue %3ld = ", j+1);
      if (w[j].im == 0.0)
        printf("%13.4e\n", w[j].re);
      else
        printf(" (%13.4e, %13.4e)\n", w[j].re, w[j].im);

      printf("\nEigenvector %2ld\n", j+1);
      for (i = 0; i < n; ++i) 
        printf("%18s(%13.4e, %13.4e)\n", "", VR(i, j).re, VR(i, j).im);
      printf("\n");
    }

 END:
  NAG_FREE(a);
  NAG_FREE(vr);
  NAG_FREE(w);

  return exit_status;
}
#undef A