/* nag_matop_real_gen_matrix_actexp (f01gac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 24, 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;
  double        t;

  /* Arrays */
  double        *a = 0;
  double        *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_real_gen_matrix_actexp (f01gac) ");
  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%*[^\n]", &n, &m, &t);

  lda = n;
  ldb = n;

  if (!(a = NAG_ALLOC(n*n, double)) ||
      !(b = NAG_ALLOC(n*m, double))) {
    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", &A(i, j));
  scanf("%*[^\n]");

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

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

  /* Print solution using
   * nag_gen_real_mat_print (x04cac)
   * Print real general matrix (easy-to-use)
   */
  nag_gen_real_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_real_mat_print (x04cac)\n%s\n", fail.message);
      exit_status = 2;
      goto END;
    }

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