/* nag_matop_complex_gen_matrix_actexp (f01hac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2013.
 */

#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf01.h>
#include <nagx02.h>
#include <nagx04.h>

int main(void)
{

  /* Scalars */
  Integer       exit_status = 0;
  Integer       i, j, m, n, lda, ldb;
  Complex       t;

  /* Arrays */
  Complex       *a = 0;
  Complex       *b = 0;

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

  INIT_FAIL(fail);

#define A(I, J) a[(J-1)*lda + I-1]
#define B(I, J) b[(J-1)*ldb + I-1]

  order = Nag_ColMajor;

  /* Output preamble */
  printf("nag_matop_complex_gen_matrix_actexp (f01hac) ");
  printf("Example Program Results\n\n");
  fflush(stdout);

  /* Skip heading in data file */
  scanf("%*[^\n]");

  /* Read in the problem size and the value of the parameter t*/
  scanf("%ld %ld (%lf , %lf ) %*[^\n]", &n, &m, &t.re, &t.im);

  lda = n;
  ldb = n;

  if (!(a = NAG_ALLOC(n*n, Complex)) ||
      !(b = NAG_ALLOC(n*m, Complex))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read in the matrix a from data file */
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      scanf(" ( %lf , %lf ) ", &A(i, j).re, &A(i, j).im);
  scanf("%*[^\n]");

  /* Read in the matrix b from data file */
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      scanf(" ( %lf , %lf ) ", &B(i, j).re, &B(i, j).im);
  scanf("%*[^\n]");

  /* Find exp(tA) B using
   * nag_matop_complex_gen_matrix_actexp (f01hac)
   * Action of the exponential of a complex matrix on a complex matrix
   */
  nag_matop_complex_gen_matrix_actexp(n, m, a, lda, b, ldb, t, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_matop_complex_gen_matrix_actexp (f01hac)\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }

  /* Print solution using
   * nag_gen_complx_mat_print (x04dac)
   * Print complex general matrix (easy-to-use)
   */
  nag_gen_complx_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m,
                         b, ldb, "exp(tA) B", NULL, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_gen_complx_mat_print (x04cac)\n%s\n",
             fail.message);
      exit_status = 2;
      goto END;
    }

 END:
  NAG_FREE(a);
  NAG_FREE(b);
  return exit_status;
}