/* nag_dtrsen (f08qgc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2001.
 */

#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 */
  Integer       i, j, m, n, pda, pdc, pdq, pdt, select_len, w_len;
  Integer       exit_status = 0;
  double        alpha, beta, norm, s, sep;
  NagError      fail;
  Nag_OrderType order;
  /* Arrays */
  double        *a = 0, *c = 0, *q = 0, *t = 0, *wi = 0, *wr = 0;
  char          nag_enum_arg[40];
  Nag_Boolean   *select = 0;

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

  INIT_FAIL(fail);

  printf("nag_dtrsen (f08qgc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%ld%*[^\n] ", &n);
#ifdef NAG_COLUMN_MAJOR
  pda = n;
  pdc = n;
  pdq = n;
  pdt = n;
#else
  pda = n;
  pdc = n;
  pdq = n;
  pdt = n;
#endif
  w_len = n;
  select_len = n;

  /* Allocate memory */
  if (!(a = NAG_ALLOC(n * n, double)) ||
      !(c = NAG_ALLOC(n * n, double)) ||
      !(q = NAG_ALLOC(n * n, double)) ||
      !(wi = NAG_ALLOC(w_len, double)) ||
      !(wr = NAG_ALLOC(w_len, double)) ||
      !(select = NAG_ALLOC(select_len, Nag_Boolean)) ||
      !(t = NAG_ALLOC(n * n, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  /* Read T and Q from data file */
  for (i = 1; i <= n; ++i)
    {
      for (j = 1; j <= n; ++j)
        scanf("%lf", &T(i, j));
    }
  scanf("%*[^\n] ");
  for (i = 1; i <= n; ++i)
    {
      for (j = 1; j <= n; ++j)
        scanf("%lf", &Q(i, j));
    }
  scanf("%*[^\n] ");
  for (i = 0; i < n; ++i)
    {
      scanf("%39s", nag_enum_arg);
      /* nag_enum_name_to_value (x04nac).
       * Converts NAG enum member name to value
       */
      select[i] = (Nag_Boolean) nag_enum_name_to_value(nag_enum_arg);
    }
  scanf("%*[^\n] ");

  /* nag_dgemm (f16yac): Compute Q*T*QT and store in matrix A */
  alpha = 1.0;
  beta = 0.0;
  nag_dgemm(order, Nag_NoTrans, Nag_NoTrans, n, n, n, alpha, q, pdq, 
            t, pdt, beta, c, pdc, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgemm (f16yac).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  nag_dgemm(order, Nag_NoTrans, Nag_Trans, n, n, n, alpha, c, pdc, q, 
            pdq, beta, a, pda, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgemm (f16yac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_gen_real_mat_print (x04cac): Print Matrix A. */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n,
                         a, pda, "Matrix A", 0, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  printf("\n");

  /* Reorder the Schur factorization T */
  /* nag_dtrsen (f08qgc).
   * Reorder Schur factorization of real matrix, form
   * orthonormal basis of right invariant subspace for
   * selected eigenvalues, with estimates of sensitivities
   */
  nag_dtrsen(order, Nag_DoBoth, Nag_UpdateSchur, select, n, t, pdt,
             q, pdq, wr, wi, &m, &s, &sep, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dtrsen (f08qgc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_dgemm (f16yac): Compute A = Q*T*Q^T - Qt*Tt*Qt^T */ 
  alpha = 1.0;
  beta = 0.0;
  nag_dgemm(order, Nag_NoTrans, Nag_NoTrans, n, n, n, alpha, q, pdq, 
            t, pdt, beta, c, pdc, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgemm (f16yac).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  alpha = -1.0;
  beta = 1.0;
  nag_dgemm(order, Nag_NoTrans, Nag_Trans, n, n, n, alpha, c, pdc, q, 
            pdq, beta, a, pda, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgemm (f16yac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* nag_dge_norm (f16rac): Find norm of matrix A and print warning if */
  /* it is too large */
  nag_dge_norm(order, Nag_OneNorm, n, n, a, pda, &norm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dge_norm (f16rac).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  if (norm>pow(x02ajc(),0.8)) {
    printf("%s\n%s\n","Norm of Q*T*Q^H - (Qt*Tt*Qt^H) is much greater than 0.",
           "Schur factorization has failed.");
  } else {
    /* Print condition number estimates */
    printf(" Condition number estimate of the selected cluster of"
           " eigenvalues = %11.2e\n", 1.0/s);
    printf("\n Condition number estimate of the specified invariant"
           " subspace = %11.2e\n", 1.0/sep);
  }
 END:
  NAG_FREE(a);
  NAG_FREE(c);
  NAG_FREE(q);
  NAG_FREE(t);
  NAG_FREE(wi);
  NAG_FREE(wr);
  NAG_FREE(select);

  return exit_status;
}