/* nag_matop_real_gen_matrix_cond_exp (f01jgc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf01.h>
#include <nagx04.h>

#define A(I,J) a[J*pda + I]

int main(void)
{
  /* Scalars */
  Integer exit_status = 0;
  Integer i, j, n;
  Integer pda;
  double condea;
  /* Arrays */
  double *a = 0;
  /* Nag Types */
  Nag_OrderType order = Nag_ColMajor;
  NagError fail;

  INIT_FAIL(fail);

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

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

  /* Read in the problem size */
  scanf("%" NAG_IFMT "%*[^\n] ", &n);

  pda = n;
  if (!(a = NAG_ALLOC(pda * n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

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

  /* Find exp(A) and the condition number using
   * nag_matop_real_gen_matrix_cond_exp (f01jgc)
   * Condition number for real matrix exponential
   */
  nag_matop_real_gen_matrix_cond_exp(n, a, pda, &condea, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_matop_real_gen_matrix_cond_exp (f01jgc)\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print matrix exp(A) 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, 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 = 2;
  }

  /* Print relative condition number estimate */
  printf("Estimated relative condition number is: %7.2f\n", condea);

END:
  NAG_FREE(a);

  return exit_status;
}