/* nag_dtgexc (f08yfc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf08.h>
#include <nagf16.h>
#include <nagx02.h>
#include <nagx04.h>

int main(void)
{
  /* Scalars */
  double alpha, beta;
  Integer i, ifst, ilst, j, n, pdq, pds;
  Integer pdt, pdz, exit_status = 0;
  /* Arrays */
  double *q = 0, *s = 0, *t = 0, *z = 0;
  char nag_enum_arg[40];

  /* Nag Types */
  NagError fail;
  Nag_OrderType order;
  Nag_Boolean wantq, wantz;

#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_dtgexc (f08yfc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%*[^\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, double)) ||
      !(t = NAG_ALLOC(n * n, double)) ||
      !(q = NAG_ALLOC(pdq * pdq, double)) ||
      !(z = NAG_ALLOC(pdz * pdz, double)))
  {
    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", &S(i, j));
  scanf("%*[^\n]");
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &T(i, j));
  scanf("%*[^\n]");

  /* Initialize Q an Z to identity matrices using nag_dge_load (f16qhc). */
  alpha = 0.0;
  beta = 1.0;
  if (wantq)
    nag_dge_load(order, n, n, alpha, beta, q, pdq, &fail);
  if (wantz)
    nag_dge_load(order, n, n, alpha, beta, z, pdz, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dge_load (f16qhc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Read the row indices of diagonal elements or blocks to be swapped. */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &ifst, &ilst);

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

  /* normalize signs so that last column and diagonal of T is positive. */
  for (i = 1; i<=n; i++) {
    if (T(i,n)<0.0) {
      for (j = i; j<=n; j++) {
        S(i,j) = -S(i,j);
        T(i,j) = -T(i,j);
      }
      if (i>1 && fabs(S(i,i-1))>0.0) {
        S(i,i-1) = -S(i,i-1);
      }
    }
  }
  for (j = 1; j<=n-1; j++) {
    if (T(j,j)<0.0) {
      for (i = 1; i<=j; i++) {
        S(i,j) = -S(i,j);
        T(i,j) = -T(i,j);
      }
      if (fabs(S(j+1,j))>0.0) { 
        S(j+1,j) = -S(j+1,j);
      }
    }
  }

  /* nag_gen_real_mat_print (x04cac): Print reordered S and T. */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n,
                         s, pds, "Reordered matrix S", 0, &fail);
  printf("\n");
  if (fail.code != NE_NOERROR)
    goto PRERR;
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n,
                         t, pdt, "Reordered matrix T", 0, &fail);
  printf("\n");
PRERR:
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

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

  return exit_status;
}