/* nag_sparse_nsym_fac (f11dac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 5, 1998.
 */

#include <nag.h>
#include <nag_stdlib.h>
#include <nag_string.h>
#include <stdio.h>
#include <nagf11.h>

int main(void)
{
  double              dtol;
  double              *a = 0;
  Integer             *icol = 0, *irow = 0, *istr = 0, *idiag = 0, *ipivp = 0;
  Integer             *ipivq = 0;
  Integer             nnzc, exit_status = 0, i, n, lfill, npivm, nnz, num;
  char                nag_enum_arg[40];
  Nag_SparseNsym_Fact milu;
  Nag_SparseNsym_Piv  pstrat;
  NagError            fail;

  INIT_FAIL(fail);

  printf("nag_sparse_nsym_fac (f11dac) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  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);

  num = 2*nnz;
  istr = NAG_ALLOC(n+1, Integer);
  idiag = NAG_ALLOC(n, Integer);
  ipivp = NAG_ALLOC(n, Integer);
  ipivq = NAG_ALLOC(n, Integer);
  irow = NAG_ALLOC(num, Integer);
  icol = NAG_ALLOC(num, Integer);
  a = NAG_ALLOC(num, double);

  if (!istr || !idiag || !ipivp || !ipivq || !irow || !icol || !a)
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
      return exit_status;
    }

  /* Read the matrix a */

  for (i = 1; i <= nnz; ++i)
    scanf("%lf%ld%ld%*[^\n]", &a[i-1], &irow[i-1], &icol[i-1]);

  /* Calculate incomplete LU factorization */

  /* nag_sparse_nsym_fac (f11dac).
   * Incomplete LU factorization (nonsymmetric)
   */
  nag_sparse_nsym_fac(n, nnz, &a, &num, &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 = 1; i <= nnz; ++i)
    printf("%8ld%16.6e%8ld%8ld\n", i, a[i-1],
            irow[i-1], icol[i-1]);
  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 + 1; i <= nnz + nnzc; ++i)
    printf("%8ld%16.6e%8ld%8ld\n", i, a[i-1],
            irow[i-1], icol[i-1]);

  printf("\n         i       ipivp[i-1]  ipivq[i-1] \n"); /* */

  for (i = 1; i <= n; ++i)
    printf("%10ld%10ld%10ld\n", i, ipivp[i-1],
            ipivq[i-1]);

 END:
  NAG_FREE(istr);
  NAG_FREE(idiag);
  NAG_FREE(ipivp);
  NAG_FREE(ipivq);
  NAG_FREE(irow);
  NAG_FREE(icol);
  NAG_FREE(a);

  return exit_status;
}