/* nag_sparse_herm_chol_fac (f11jnc) 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;
  Integer             *icol = 0, *ipiv = 0, *irow = 0, *istr = 0;
  char                nag_enum_arg[100];
  /* NAG types */
  Nag_SparseSym_Piv   pstrat;
  Nag_SparseSym_Fact  mic;
  NagError            fail;

  INIT_FAIL(fail);

  printf("nag_sparse_herm_chol_fac (f11jnc) Example Program Results\n");
  /* Skip heading in data file*/
  scanf("%*[^\n]");
  /* Read algorithmic parameters*/
  scanf("%ld%*[^\n]%ld%*[^\n]", &n, &nnz);

  /* Allocate memory */
  la = 3 * nnz;
  if (
      !(a = NAG_ALLOC(la, 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;
    }
  scanf("%ld%lf%*[^\n]", &lfill, &dtol);
  scanf("%99s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  mic = (Nag_SparseSym_Fact) nag_enum_name_to_value(nag_enum_arg);
  scanf("%lf%*[^\n]", &dscale);
  scanf("%99s%*[^\n]", nag_enum_arg);
  pstrat = (Nag_SparseNsym_Piv) nag_enum_name_to_value(nag_enum_arg);

  /* 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 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;
    }

  /* Output original matrix*/
  printf(" Original Matrix \n");
  printf("  n = %4ld, nnz  = %4ld\n", n, nnz);
  printf("%8s%16s%23s%9s\n","i","a[i]","irow[i]","icol[i]");
  for (i = 0; i < nnz; i++)
    printf("%8ld (%13.4e, %13.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");
  printf("  n = %4ld, nnzc = %4ld, npivm = %4ld\n",
         n, nnzc, npivm);
  printf("%8s%16s%23s%9s\n","i","a[i]","irow[i]","icol[i]");
  for (i = nnz; i < nnz + nnzc; i++)
    printf("%8ld (%13.4e, %13.4e) %8ld %8ld \n",
           i , a[i].re, a[i].im, irow[i], icol[i]);
  printf("\n%8s%12s\n","i","ipiv[i-1]");
  for (i = 1; i <= n; i++)
    printf("%8ld%8ld\n", i, ipiv[i-1]);

 END:
  NAG_FREE(a);
  NAG_FREE(icol);
  NAG_FREE(ipiv);
  NAG_FREE(irow);
  NAG_FREE(istr);
  return exit_status;
}