/* nag_dtrsyl (f08qhc) 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 <nagx04.h>
#include <nagx02.h>

int main(void)
{
  /* Scalars */
  Integer       i, j, m, n, pda, pdb, pdc, pdd, pde, pdf;
  Integer       exit_status = 0;
  double        alpha, beta, norm, scale;
  Nag_SignType  sign = Nag_Minus;
  NagError      fail;
  Nag_OrderType order;
  /* Arrays */
  double        *a = 0, *b = 0, *c = 0, *d = 0, *e = 0, *f = 0;

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J-1)*pda + I - 1]
#define B(I, J) b[(J-1)*pdb + I - 1]
#define C(I, J) c[(J-1)*pdc + I - 1]
#define D(I, J) d[(J-1)*pdd + I - 1]
#define E(I, J) e[(J-1)*pde + I - 1]
#define F(I, J) f[(J-1)*pdf + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I-1)*pda + J - 1]
#define B(I, J) b[(I-1)*pdb + J - 1]
#define C(I, J) c[(I-1)*pdc + J - 1]
#define D(I, J) d[(I-1)*pdd + J - 1]
#define E(I, J) e[(I-1)*pde + J - 1]
#define F(I, J) f[(I-1)*pdf + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_dtrsyl (f08qhc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%ld%ld%*[^\n] ", &m, &n);
#ifdef NAG_COLUMN_MAJOR
  pda = m;
  pdb = n;
  pdc = m;
  pdd = m;
  pde = m;
  pdf = m;
#else
  pda = m;
  pdb = n;
  pdc = n;
  pdd = n;
  pde = n;
  pdf = n;
#endif

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

  /* Read A, B and C from data file */
  for (i = 1; i <= m; ++i)
    {
      for (j = 1; j <= m; ++j)
        scanf("%lf", &A(i, j));
    }
  scanf("%*[^\n] ");
  for (i = 1; i <= n; ++i)
    {
      for (j = 1; j <= n; ++j)
        scanf("%lf", &B(i, j));
    }
  scanf("%*[^\n] ");
  for (i = 1; i <= m; ++i)
    {
      for (j = 1; j <= n; ++j)
        scanf("%lf", &C(i, j));
    }
  scanf("%*[^\n] ");

  /* Copy C into F */
  for (i = 1; i <= m; ++i)
    {
      for (j = 1; j <= m; ++j)
        F(i, j) = C(i, j);
    }

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

  /* Reorder the Schur factorization T */
  /* nag_dtrsyl (f08qhc).
   * Solve real Sylvester matrix equation AX + XB = C, A and B
   * are upper quasi-triangular or transposes
   */
  nag_dtrsyl(order, Nag_NoTrans, Nag_NoTrans, sign, m, n, a, pda,
             b, pdb, c, pdc, &scale, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dtrsyl (f08qhc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
  
  /* nag_dgemm (f16yac): Compute aC - (A*X + X*B*sign) from solution*/
  /* and store in matrix E*/
  alpha = 1.0;
  beta = 0.0;
  nag_dgemm(order, Nag_NoTrans, Nag_NoTrans, m, n, m, alpha, a, pda, 
            c, pdc, beta, d, pdd, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgemm (f16yac).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  if(sign == Nag_Minus)
    alpha = -1.0;
  else
    alpha = 1.0;
  beta = 1.0;
  nag_dgemm(order, Nag_NoTrans, Nag_NoTrans, m, n, n, alpha, c, pdc, b, 
            pdb, beta, d, pdd, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_dgemm (f16yac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  for(i=1; i<=m; i++)
    {
      for (j=1; j<=n; j++)
        E(i, j) = scale * F(i, j) - D(i, j);
    }

  /* nag_dge_norm (f16rac): Find norm of matrix E and print warning if */
  /* it is too large */
  nag_dge_norm(order, Nag_OneNorm, n, n, e, pde, &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 aC - (A*X + X*B*sign) is much greater than 0.",
             "nag_dtrsyl (f08qhc) has failed.");
    }
  else
    {
      printf(" SCALE = %11.2e\n", scale);
    }
 END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(c);
  NAG_FREE(d);
  NAG_FREE(e);
  NAG_FREE(f);

  return exit_status;
}