/* nag_ztgexc (f08ytc) 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             i, ifst, ilst, j, n, pdq, pds, pdt, pdz;
  Integer             exit_status = 0;

  /* Arrays */
  Complex             *q = 0, *s = 0, *t = 0, *z = 0;
  char                nag_enum_arg[40];

  NagError            fail;
  Nag_OrderType       order;
  Nag_Boolean         wantq, wantz;
  Nag_MatrixType      upmat = Nag_UpperMatrix;
  Nag_DiagType        diag   = Nag_NonUnitDiag;
  Nag_LabelType       intlab = Nag_IntegerLabels;
  Nag_ComplexFormType brac   = Nag_BracketForm;

#ifdef NAG_COLUMN_MAJOR
#define S(I, J) s[(J-1)*pds + I - 1]
#define T(I, J) t[(J-1)*pdt + I - 1]
  order = Nag_ColMajor;
#else
#define S(I, J) s[(I-1)*pds + J - 1]
#define T(I, J) t[(I-1)*pdt + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_ztgexc (f08ytc) Example Program Results\n\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
   */
  wantq = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg);
  scanf(" %39s%*[^\n]", nag_enum_arg);
  wantz = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg);

  pds = n;
  pdt = n;
  pdq = (wantq?n:1);
  pdz = (wantz?n:1);

  /* Allocate memory */
  if (
      !(s = NAG_ALLOC(n*n, Complex)) ||
      !(t = NAG_ALLOC(n*n, Complex)) ||
      !(q = NAG_ALLOC(pdq*pdq, Complex)) ||
      !(z = NAG_ALLOC(pdz*pdz, Complex)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

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

  /* Read the row indices */
  scanf("%ld%ld%*[^\n]", &ifst, &ilst);

  /* Reorder the S and T */
  nag_ztgexc(order, wantq, wantz, n, s, pds, t, pdt, q, pdq, z, pdz, ifst,
             &ilst, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_ztgexc (f08ytc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* Print reordered generalized Schur form */
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, upmat, diag, n, n, s, pds, brac,
                                "%7.4f", "Reordered Schur matrix S",
                                intlab, NULL, intlab, NULL, 80, 0, NULL,
                                &fail);
  if (fail.code != NE_NOERROR) goto PRERR;
  printf("\n");
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, upmat, diag, n, n, t, pdt, brac,
                                "%7.4f", "Reordered Schur matrix T",
                                intlab, NULL, intlab, NULL, 80, 0, NULL,
                                &fail);
 PRERR:
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
             fail.message);
      exit_status = 1;
    }

 END:
  NAG_FREE(q);
  NAG_FREE(s);
  NAG_FREE(t);
  NAG_FREE(z);

  return exit_status;
}