/* nag_sparse_nherm_fac (f11dnc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
#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("%" NAG_IFMT "%*[^\n]", &n);
  scanf("%" NAG_IFMT "%*[^\n]", &nnz);
  scanf("%" NAG_IFMT "%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 ) %" NAG_IFMT "%" NAG_IFMT "%*[^\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     = %6" NAG_IFMT "\n", n);
  printf("  nnz   = %6" NAG_IFMT "\n", nnz);

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

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

  for (i = nnz; i < nnz + nnzc; ++i)
    printf("%8" NAG_IFMT " (%16.4e, %16.4e) %8" NAG_IFMT "%8" NAG_IFMT "\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("%10" NAG_IFMT "%10" NAG_IFMT "%10" NAG_IFMT "\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;
}