/* nag_sparse_nherm_fac (f11dnc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */
#include <nag.h>
#include <nag_stdlib.h>
#include <naga02.h>
#include <nagf11.h>
int main(void)
{
  /* Scalars */
  Integer             exit_status = 0;
  double              dtol;
  Integer             i, la, lfill, n, nnz, nnzc, npivm;
  /* Arrays */
  Complex             *a = 0;
  Integer             *icol = 0, *idiag = 0, *ipivp = 0, *ipivq = 0,
                      *irow = 0, *istr = 0;
  char                nag_enum_arg[40];
  /* NAG types */
  Nag_SparseNsym_Piv  pstrat;
  Nag_SparseNsym_Fact milu;
  NagError            fail;

  INIT_FAIL(fail);

  printf("nag_sparse_nherm_fac (f11dnc) Example Program Results \n\n");
  /* Skip heading in data file*/
  scanf("%*[^\n] ");
  /* Read algorithmic parameters*/
  scanf("%ld%*[^\n]", &n);
  scanf("%ld%*[^\n]", &nnz);
  scanf("%ld%lf%*[^\n]", &lfill, &dtol);
  scanf("%39s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  pstrat = (Nag_SparseNsym_Piv) nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s%*[^\n]", nag_enum_arg);
  milu = (Nag_SparseNsym_Fact) nag_enum_name_to_value(nag_enum_arg);
  la = 2 * nnz;
  if (
      !(a = NAG_ALLOC((la), Complex)) ||
      !(icol = NAG_ALLOC((la), Integer)) ||
      !(idiag = NAG_ALLOC((n), Integer)) ||
      !(ipivp = NAG_ALLOC((n), Integer)) ||
      !(ipivq = NAG_ALLOC((n), Integer)) ||
      !(irow = NAG_ALLOC((la), Integer)) ||
      !(istr = NAG_ALLOC((n + 1), Integer))
      ) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the matrix a */
  for (i = 0; i < nnz; ++i)
    scanf(" ( %lf , %lf ) %ld%ld%*[^\n]",
          &a[i].re, &a[i].im, &irow[i], &icol[i]);

  /* Calculate incomplete LU factorization */
  /* nag_sparse_nherm_fac (f11dnc)
   * Complex sparse non-Hermitian linear systems, incomplete LU factorization
   */
  nag_sparse_nherm_fac(n, nnz, a, la, irow, icol, lfill, dtol, pstrat, milu,
                       ipivp, ipivq, istr, idiag, &nnzc, &npivm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_nsym_fac (f11dac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Output original matrix*/
  printf("  Original Matrix\n  n     = %6ld\n", n);
  printf("  nnz   = %6ld\n", nnz);

  for (i = 0; i < nnz; ++i)
    printf("%8ld (%16.4e, %16.4e) %8ld%8ld\n",
            i, a[i].re, a[i].im, irow[i], icol[i]);
  printf("\n");

  /* Output details of the factorization*/
  printf("  Factorization\n  n     = %6ld\n", n);
  printf("  nnz   = %6ld\n", nnzc);
  printf("  npivm = %6ld\n", npivm);

  for (i = nnz; i < nnz + nnzc; ++i)
    printf("%8ld (%16.4e, %16.4e) %8ld%8ld\n",
            i, a[i].re,a[i].im, irow[i], icol[i]);

  printf("\n         i       ipivp[i]  ipivq[i]\n"); /* */
  for (i = 0; i < n; ++i)
    printf("%10ld%10ld%10ld\n", i, ipivp[i], ipivq[i]);
 END:
  NAG_FREE(a);
  NAG_FREE(icol);
  NAG_FREE(idiag);
  NAG_FREE(ipivp);
  NAG_FREE(ipivq);
  NAG_FREE(irow);
  NAG_FREE(istr);
  return exit_status;
}