/* nag_real_gen_matrix_exp (f01ecc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
/* Pre-processor includes */
#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf01.h>
#include <nagx04.h>

int main(void)
{
  /*Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer i, j, n;
  Integer pda;
  NagError fail;
  /*Double scalar and array declarations */
  double *a = 0;
  Nag_OrderType order;

  INIT_FAIL(fail);

  printf("%s\n", "nag_real_gen_matrix_exp (f01ecc) Example Program Results");
  printf("\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
#ifdef NAG_COLUMN_MAJOR
  pda = n;
#define A(I, J) a[(J-1)*pda + I-1]
  order = Nag_ColMajor;
#else
  pda = n;
#define A(I, J) a[(I-1)*pda + J-1]
  order = Nag_RowMajor;
#endif
  if (!(a = NAG_ALLOC(pda * n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Read A from data file */
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++)
      scanf("%lf", &A(i, j));
  }
  scanf("%*[^\n] ");
  /* Find exp( A ) */
  /*
   * nag_real_gen_matrix_exp (f01ecc)
   * Real matrix exponential
   */
  nag_real_gen_matrix_exp(order, n, a, pda, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_real_gen_matrix_exp (f01ecc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  /* Print solution */
  /*
   * nag_gen_real_mat_print (x04cac)
   * Print real general matrix (easy-to-use)
   */
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n,
                         a, pda, "Exp(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;
  }

END:
  NAG_FREE(a);

  return exit_status;
}