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

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

int main(void)
{
  /* Scalars */
  double dif, scale;
  Integer i, ijob, j, m, n, pda, pdb, pdc, pdd, pde, pdf;
  Integer exit_status = 0;

  /* Arrays */
  Complex *a = 0, *b = 0, *c = 0, *d = 0, *e = 0, *f = 0;
  char nag_enum_arg[40];

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

  /* K(I,J) maps matrix element (I,J) to array storage element k */
#ifdef NAG_COLUMN_MAJOR
#define K(I, J, PD) (J-1)*PD + I - 1
  order = Nag_ColMajor;
#else
#define K(I, J, PD) (I-1)*PD + J - 1
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_ztgsyl (f08yvc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n);
  scanf("%" NAG_IFMT "%*[^\n]", &ijob);
  if (m < 0 || n < 0 || ijob < 0 || ijob > 4) {
    printf("Invalid m, n or ijob\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
   */
  trans = (Nag_TransType) nag_enum_name_to_value(nag_enum_arg);

  pda = m;
  pdb = n;
  pdd = m;
  pde = n;
#ifdef NAG_COLUMN_MAJOR
  pdc = m;
  pdf = m;
#else
  pdc = n;
  pdf = n;
#endif

  /* Allocate memory */
  if (!(a = NAG_ALLOC(m * m, Complex)) ||
      !(b = NAG_ALLOC(n * n, Complex)) ||
      !(c = NAG_ALLOC(m * n, Complex)) ||
      !(d = NAG_ALLOC(m * m, Complex)) ||
      !(e = NAG_ALLOC(n * n, Complex)) || !(f = NAG_ALLOC(m * n, Complex)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read A, B, D, E, C and F from data file */
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= m; ++j)
      scanf(" ( %lf , %lf )", &a[K(i, j, pda)].re, &a[K(i, j, pda)].im);
  scanf("%*[^\n]");
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &b[K(i, j, pdb)].re, &b[K(i, j, pdb)].im);
  scanf("%*[^\n]");
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= m; ++j)
      scanf(" ( %lf , %lf )", &d[K(i, j, pdd)].re, &d[K(i, j, pdd)].im);
  scanf("%*[^\n]");
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &e[K(i, j, pde)].re, &e[K(i, j, pde)].im);
  scanf("%*[^\n]");
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &c[K(i, j, pdc)].re, &c[K(i, j, pdc)].im);
  scanf("%*[^\n]");
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &f[K(i, j, pdf)].re, &f[K(i, j, pdf)].im);
  scanf("%*[^\n]");

  /* Solve the Sylvester equations:
   * A*R - L*B = scale*C 
   * D*R - L*E = scale*F
   *                     for R and L using  nag_ztgsyl (f08yvc).
   */
  nag_ztgsyl(order, trans, ijob, m, n, a, pda, b, pdb, c, pdc, d, pdd, e, pde,
             f, pdf, &scale, &dif, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_ztgsyl (f08yvc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the solution matrices R and L */
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m,
                                n, c, pdc, Nag_BracketForm, "%7.4f",
                                "Solution matrix R", Nag_IntegerLabels, NULL,
                                Nag_IntegerLabels, NULL, 80, 0, NULL, &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;
  }
  printf("\n");
  fflush(stdout);
  nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m,
                                n, f, pdf, Nag_BracketForm, "%7.4f",
                                "Solution matrix L", Nag_IntegerLabels, NULL,
                                Nag_IntegerLabels, NULL, 80, 0, NULL, &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;
  }
  printf("\nscale = %11.2e\n", scale);

  if (ijob > 0 && scale > 0.0) {
    printf("\ndif   = %11.2e\n\n", dif);
    printf("This estimate of Dif((A,D),(B,E)) was computed based on the ");
    if (ijob == 1 || ijob == 3) {
      printf("Frobenius norm.\n");
    }
    else {
      printf("one norm.\n");
    }
  }

END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(c);
  NAG_FREE(d);
  NAG_FREE(e);
  NAG_FREE(f);

  return exit_status;
}