/* nag_sparse_herm_precon_ichol_solve (f11jpc) 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                    dscale, dtol;
  Integer                   i, la, lfill, n, nnz, nnzc, npivm;
  /* Arrays */
  Complex                   *a = 0, *x = 0, *y = 0;
  Integer                   *icol = 0, *ipiv = 0, *irow = 0, *istr = 0;
  /* NAG types */
  Nag_SparseSym_Fact        mic;
  Nag_SparseNsym_Piv        pstrat;
  Nag_SparseSym_CheckData   check;
  NagError                  fail;

  INIT_FAIL(fail);

  printf("nag_sparse_herm_precon_ichol_solve (f11jpc) Example Program Results");
  printf("\n\n");
  /* Skip heading in data file*/
  scanf("%*[^\n]");
  scanf("%ld%*[^\n]", &n);
  scanf("%ld%*[^\n]", &nnz);

  /* Allocate memory */
  la = 3 * nnz;
  if (
      !(a = NAG_ALLOC(la, Complex)) ||
      !(x = NAG_ALLOC(n, Complex)) ||
      !(y = NAG_ALLOC(n, Complex)) ||
      !(icol = NAG_ALLOC(la, Integer)) ||
      !(ipiv = 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 - 1; 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 - 1; i++)
    scanf(" ( %lf , %lf ) ", &y[i].re, &y[i].im);

  lfill = -1;
  dtol = 0.0;
  dscale = 0.0;
  mic = Nag_SparseSym_UnModFact;
  pstrat = Nag_SparseSym_MarkPiv;
  /* Calculate Cholesky factorization using  nag_sparse_herm_chol_fac (f11jnc).
   */
  nag_sparse_herm_chol_fac(n, nnz, a, la, irow, icol, lfill, dtol, mic, dscale,
                           pstrat, ipiv, istr, &nnzc, &npivm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_sparse_herm_chol_fac (f11jnc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
  /* Check the output value of npivm */
  if (npivm != 0)
    printf("Factorization is not complete \n");
  else
    {
      /* Solve complex linear system involving incomplete Cholesky factorization
       *
       *              H T
       *       P L D L P x = y
       *
       * using nag_sparse_herm_precon_ichol_solve (f11jpc).
       */
      check = Nag_SparseSym_Check;
      nag_sparse_herm_precon_ichol_solve(n, a, la, irow, icol, ipiv, istr, 
                                         check, y, x, &fail);
      if (fail.code != NE_NOERROR) {
        printf("Error from nag_sparse_herm_precon_ichol_solve (f11jpc).\n%s\n",
               fail.message);
        exit_status = 2;
        goto END;
      }
      /* Output results*/
      printf("Solution of linear system \n");
      for (i = 0; i <= n - 1; 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(ipiv);
  NAG_FREE(irow);
  NAG_FREE(istr);
  return exit_status;
}