/* nag_zgebal (f08nvc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

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

int main(void)
{
  /* Scalars */
  Integer firstnz, i, ihi, ilo, j, m, n, pda, pdh, pdvr;
  Integer scale_len, tau_len, w_len;
  Integer exit_status = 0;
  NagError fail;
  Nag_OrderType order;
  /* Arrays */
  Complex *a = 0, *h = 0, *tau = 0, *vl = 0, *vr = 0, *w = 0;
  double *scale = 0;
  Nag_Boolean *select = 0;

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

  INIT_FAIL(fail);

  printf("nag_zgebal (f08nvc) Example Program Results\n\n");

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

  pda = n;
  pdh = n;
  pdvr = n;
  scale_len = n;
  tau_len = n;
  w_len = n;

  /* Allocate memory */
  if (!(a = NAG_ALLOC(n * n, Complex)) ||
      !(h = NAG_ALLOC(n * n, Complex)) ||
      !(scale = NAG_ALLOC(scale_len, double)) ||
      !(tau = NAG_ALLOC(tau_len, Complex)) ||
      !(vl = NAG_ALLOC(1 * 1, Complex)) ||
      !(vr = NAG_ALLOC(n * n, Complex)) ||
      !(w = NAG_ALLOC(w_len, Complex)) ||
      !(select = NAG_ALLOC(1, Nag_Boolean)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read 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] ");

  /* Balance A */
  /* nag_zgebal (f08nvc).
   * Balance complex general matrix
   */
  nag_zgebal(order, Nag_DoBoth, n, a, pda, &ilo, &ihi, scale, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zgebal (f08nvc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Reduce A to upper Hessenberg form H = (Q^H)*A*Q */
  /* nag_zgehrd (f08nsc).
   * Unitary reduction of complex general matrix to upper
   * Hessenberg form
   */
  nag_zgehrd(order, n, ilo, ihi, a, pda, tau, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zgehrd (f08nsc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Copy A to H and VR */
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) {
      H(i, j).re = A(i, j).re;
      H(i, j).im = A(i, j).im;
      VR(i, j).re = A(i, j).re;
      VR(i, j).im = A(i, j).im;
    }
  }

  /* Form Q explicitly, storing the result in VR */
  /* nag_zunghr (f08ntc).
   * Generate unitary transformation matrix from reduction to
   * Hessenberg form determined by nag_zgehrd (f08nsc)
   */
  nag_zunghr(order, n, 1, n, vr, pdvr, tau, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zunghr (f08ntc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Calculate the eigenvalues and Schur factorization of A */
  /* nag_zhseqr (f08psc).
   * Eigenvalues and Schur factorization of complex upper
   * Hessenberg matrix reduced from complex general matrix
   */
  nag_zhseqr(order, Nag_Schur, Nag_UpdateZ, n, ilo, ihi, h, pdh,
             w, vr, pdvr, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zhseqr (f08psc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf(" Eigenvalues\n");
  for (i = 0; i < n; ++i)
    printf(" (%7.4f,%7.4f)", w[i].re, w[i].im);
  printf("\n");
  /* Calculate the eigenvectors of A, storing the result in VR */
  /* nag_ztrevc (f08qxc).
   * Left and right eigenvectors of complex upper triangular
   * matrix
   */
  nag_ztrevc(order, Nag_RightSide, Nag_BackTransform, select, n,
             h, pdh, vl, 1, vr, pdvr, n, &m, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_ztrevc (f08qxc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* nag_zgebak (f08nwc).
   * Transform eigenvectors of complex balanced matrix to
   * those of original matrix supplied to nag_zgebal (f08nvc)
   */
  nag_zgebak(order, Nag_DoBoth, Nag_RightSide, n, ilo, ihi, scale,
             m, vr, pdvr, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zgebak (f08nwc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Normalize the eigenvectors */
  for (j = 1; j <= m; j++) {
    firstnz = n;
    for (i = n; i >= 1; i--) {
      if (VR(i, j).re != 0 || VR(i, j).im != 0) {
        firstnz = i;
      }
    }
    for (i = n; i >= 1; i--) {
      VR(i, j) = nag_complex_divide(VR(i, j), VR(firstnz, j));
    }
  }
  /* Print eigenvectors */
  printf("\n");
  /* nag_gen_complx_mat_print_comp (x04dbc).
   * Print complex general matrix (comprehensive)
   */
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                                m, vr, pdvr, Nag_BracketForm, "%7.4f",
                                "Contents of array VR", Nag_IntegerLabels, 0,
                                Nag_IntegerLabels, 0, 80, 0, 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
END:
  NAG_FREE(a);
  NAG_FREE(h);
  NAG_FREE(scale);
  NAG_FREE(tau);
  NAG_FREE(vl);
  NAG_FREE(vr);
  NAG_FREE(w);
  NAG_FREE(select);

  return exit_status;
}