/* nag_matop_complex_gen_matrix_frcht_pow (f01kfc) 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]
#define E(I,J) e[J*pde + I]

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

  INIT_FAIL(fail);

  printf("nag_matop_complex_gen_matrix_frcht_pow (f01kfc) ");
  printf("Example Program Results\n\n");
  fflush(stdout);

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

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

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

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

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

  /* Find A^p and L(A,E) using
   * nag_matop_complex_gen_matrix_frcht_pow (f01kfc)
   * Frechet derivative of complex matrix power
   */
  nag_matop_complex_gen_matrix_frcht_pow(n, a, pda, e, pde, p, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_matop_complex_gen_matrix_frcht_pow (f01kfc)\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print matrix A^p using nag_gen_cmplx_mat_print (x04dac)
   * Print complex general matrix (easy-to-use)
   */
  nag_gen_complx_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n,
                           a, pda, "A^p", NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_cmplx_mat_print (x04dac)\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  /* Print matrix L(A,E) using nag_gen_cmplx_mat_print (x04dac)
   * Print complex general matrix (easy-to-use)
   */
  nag_gen_complx_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n,
                           e, pde, "L(A,E)", NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_gen_cmplx_mat_print (x04dac)\n%s\n", fail.message);
    exit_status = 3;
    goto END;
  }

END:

  NAG_FREE(a);
  NAG_FREE(e);

  return exit_status;
}