/* nag_zggev (f08wnc) 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 <nagx02.h>
#include <nagx04.h>

int main(void)
{
  /* Scalars */
  Complex           z;
  double            small;
  Integer           i, j, n, pda, pdb, pdvl, pdvr;
  Integer           exit_status = 0;

  /* Arrays */
  Complex           *a = 0, *alpha = 0, *b = 0, *beta = 0, *vl = 0, *vr = 0;
  char              nag_enum_arg[40];
  
  /* Nag Types */
  NagError          fail;
  Nag_OrderType     order;
  Nag_LeftVecsType  jobvl;
  Nag_RightVecsType jobvr;


#ifdef NAG_COLUMN_MAJOR
#define A(I, J)  a[(J-1)*pda + I - 1]
#define B(I, J)  b[(J-1)*pdb + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J)  a[(I-1)*pda + J - 1]
#define B(I, J)  b[(I-1)*pdb + J - 1]
  order = Nag_RowMajor;
#endif
  
  INIT_FAIL(fail);
  
  
  printf("nag_zggev (f08wnc) Example Program Results\n");
  
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%ld%*[^\n]", &n);
  if (n < 0)
    {
      printf("Invalid n\n");
      exit_status = 1;
      goto END;
    }
  
  scanf(" %39s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  jobvl = (Nag_LeftVecsType) nag_enum_name_to_value(nag_enum_arg);
  scanf(" %39s%*[^\n]", nag_enum_arg);
  jobvr = (Nag_RightVecsType) nag_enum_name_to_value(nag_enum_arg);

  pda = n;
  pdb = n;
  pdvl = (jobvl==Nag_LeftVecs?n:1);
  pdvr = (jobvr==Nag_RightVecs?n:1);
  
  /* Allocate memory */
  if (!(a     = NAG_ALLOC(n*n, Complex)) ||
      !(alpha = NAG_ALLOC(n, Complex)) ||
      !(b     = NAG_ALLOC(n*n, Complex)) ||
      !(beta  = NAG_ALLOC(n, Complex)) ||
      !(vl    = NAG_ALLOC(pdvl*pdvl, Complex)) ||
      !(vr    = NAG_ALLOC(pdvr*pdvr, Complex)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  
  /* Read in the matrices A and B */
  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]");
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &B(i, j).re, &B(i, j).im);
  scanf("%*[^\n]");
  
  /* Solve the generalized eigenvalue problem using nag_zggev (f08wnc). */
  nag_zggev(order, jobvl, jobvr, n, a, pda, b, pdb, alpha, beta, vl, pdvl, vr,
            pdvr, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_zggev (f08wnc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_real_safe_small_number (x02amc). */
  small = nag_real_safe_small_number;
  printf("\n     Eigenvalues\n");
  for (j = 0; j < n; ++j)
    {
      if (nag_complex_abs(alpha[j]) * small >=  nag_complex_abs(beta[j]))
        {
          printf("%2ld numerically infinite or undetermined\n", j+1);
          printf("   alpha = (%9.4f, %9.4f), beta = (%9.4f, %9.4f)\n",
                 alpha[j].re, alpha[j].im, beta[j].re, beta[j].im);
        }
      else
        {
          z = nag_complex_divide(alpha[j], beta[j]);
          printf("%2ld  (%13.4e, %13.4e)\n", j+1, z.re, z.im);
        }
    }

  if (jobvl==Nag_LeftVecs) {
    printf("\n");
    /* Print left eigenvectors using nag_gen_complx_mat_print (x04dac). */
    fflush(stdout);
    nag_gen_complx_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                             n, vl, pdvl, "    Left eigenvectors (columns)",
                             0, &fail);
  }
  if ( jobvr==Nag_RightVecs && fail.code == NE_NOERROR) {
    printf("\n");
    /* Print rightt eigenvectors using nag_gen_complx_mat_print (x04dac). */
    fflush(stdout);
    nag_gen_complx_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                             n, vr, pdvr, "    Right eigenvectors (columns)", 
                             0, &fail);
  }
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_complx_mat_print (x04dac).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
    
 END:
  NAG_FREE(a);
  NAG_FREE(alpha);
  NAG_FREE(b);
  NAG_FREE(beta);
  NAG_FREE(vl);
  NAG_FREE(vr);
  
  return exit_status;
}