/* nag_linsys_complex_gen_norm_rcomm (f04zdc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf04.h>
#include <nagf07.h>
#include <nagf16.h>

int main(void)
{

  /* Scalars */
  Integer exit_status = 0, irevcm = 0, seed = 354;
  Integer i, j, m, n, pda, pdx, pdy, t;
  double cond = 0.0, nrma = 0.0, nrminv = 0.0;

  /* Arrays */
  Integer *icomm = 0, *ipiv = 0;
  Complex *a = 0, *work = 0, *x = 0, *y = 0;
  double *rwork = 0;

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

  INIT_FAIL(fail);

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

  order = Nag_ColMajor;

  /* Output preamble */
  printf("nag_linsys_complex_gen_norm_rcomm (f04zdc) ");
  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("%" NAG_IFMT " %" NAG_IFMT " %" NAG_IFMT " %*[^\n] ", &m, &n, &t);

  pda = n;
  pdx = n;
  pdy = m;

  if (!(a = NAG_ALLOC(m * n, Complex)) ||
      !(x = NAG_ALLOC(n * t, Complex)) ||
      !(y = NAG_ALLOC(m * t, Complex)) ||
      !(work = NAG_ALLOC(m * t, Complex)) ||
      !(rwork = NAG_ALLOC(2 * n, double)) ||
      !(ipiv = NAG_ALLOC(n, Integer)) ||
      !(icomm = NAG_ALLOC(2 * n + 5 * t + 20, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

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

  /* Compute the 1-norm of A */
  nag_zge_norm(order, Nag_OneNorm, m, n, a, pda, &nrma, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dge_norm\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("Estimated norm of A is: %7.2f\n\n", nrma);

  /* 
   * Estimate the norm of A^(-1) witohut explicitly forming A^(-1) 
   */

  /* Compute and LU factorization of A using nag_zgetrf (f07arc) */
  nag_zgetrf(order, m, n, a, pda, ipiv, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_zgetrf\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  /* Estimate the norm of A^(-1) using the LU factors of A
   * nag_linsys_complex_gen_norm_rcomm (f04zdc)
   * Estimate of the 1-norm of a complex matrix
   */
  do {
    nag_linsys_complex_gen_norm_rcomm(&irevcm, m, n, x, pdx, y, pdy,
                                      &nrminv, t, seed, work, rwork, icomm,
                                      &fail);
    if (irevcm == 1) {
      /* Compute y = inv(A)*x by solving Ay = x */
      trans = Nag_NoTrans;
      nag_zgetrs(order, trans, n, t, a, pda, ipiv, x, pdx, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_zgetrs\n%s\n", fail.message);
        exit_status = 3;
        goto END;
      }
      for (i = 0; i < n * t; i++)
        y[i] = x[i];
    }

    else if (irevcm == 2) {
      /* Compute x = herm(inv(A))*y by solving A^H x = y */
      trans = Nag_ConjTrans;
      nag_zgetrs(order, trans, n, t, a, pda, ipiv, y, pdy, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_zgetrs\n%s\n", fail.message);
        exit_status = 4;
        goto END;
      }
      for (i = 0; i < n * t; i++)
        x[i] = y[i];
    }
  } while (irevcm != 0);

  if (fail.code != NE_NOERROR) {
    printf("Error from nag_linsys_complex_gen_norm_rcomm (f04zdc) \n%s\n",
           fail.message);
    exit_status = 5;
    goto END;
  }

  printf("Etimated norm of inverse of A is: %7.2f\n\n", nrminv);

  /* Compute and print the estimated condition number */
  cond = nrma * nrminv;

  printf("Estimated condition number of A is: %7.2f\n", cond);

END:
  NAG_FREE(a);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(work);
  NAG_FREE(rwork);
  NAG_FREE(icomm);
  NAG_FREE(ipiv);
  return exit_status;
}