/* nag_sparse_nherm_precon_ilu_solve (f11dpc) 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, *x = 0, *y = 0;
  Integer                  *icol = 0, *idiag = 0, *ipivp = 0, *ipivq = 0,
                           *irow = 0, *istr = 0;
  /* NAG types */
  Nag_SparseNsym_Piv       pstrat;
  Nag_SparseNsym_Fact      milu;
  Nag_SparseNsym_CheckData check;
  Nag_TransType            trans;
  NagError                 fail;

  INIT_FAIL(fail);

  printf("nag_sparse_nherm_precon_ilu_solve (f11dpc) Example Program Results");
  printf("\n\n");
  /* Skip heading in data file*/
  scanf("%*[^\n]");
  scanf("%ld%*[^\n]", &n);
  scanf("%ld%*[^\n]", &nnz);
  la = 3 * nnz;
  if (
      !(a = NAG_ALLOC((la), Complex)) ||
      !(x = NAG_ALLOC((n), Complex)) ||
      !(y = NAG_ALLOC((n), 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 non-zero elements of 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]);
  /* Read the vector y */
  for (i = 0; i < n; i++) scanf(" ( %lf , %lf )", &y[i].re, &y[i].im);

  /* Calculate LU factorization*/
  lfill = -1;
  dtol = 0.0;
  pstrat = Nag_SparseNsym_CompletePiv;
  milu = Nag_SparseNsym_UnModFact;
  /* 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_nherm_fac (f11dnc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Check value of npivm */
  if (npivm > 0) {
    printf("Factorization is not complete\n");
  } else {
    /* Solve P L D U x = y */
    check = Nag_SparseNsym_Check;
    trans = Nag_NoTrans;
    /* nag_sparse_nherm_precon_ilu_solve (f11dpc).
     * Solution of complex linear system involving incomplete LU
     * preconditioning matrix
     */
    nag_sparse_nherm_precon_ilu_solve(trans, n, a, la, irow, icol, ipivp, ipivq,
                                      istr, idiag, check, y, x, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_sparse_nherm_precon_ilu_solve.\n%s\n",
             fail.message);
      exit_status = 2;
      goto END;
    }
    /* Output results*/
    printf(" Solution of linear system \n");
    for (i = 0; i < n; i++) printf(" ( %13.4e, %13.4e) \n", x[i].re, x[i].im);
  }
 END:
  NAG_FREE(a);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(icol);
  NAG_FREE(idiag);
  NAG_FREE(ipivp);
  NAG_FREE(ipivq);
  NAG_FREE(irow);
  NAG_FREE(istr);
  return exit_status;
}