NAG Library Manual, Mark 30.2
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_sparse_real_gen_precon_ilu (f11dac) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

#include <nag.h>
#include <stdio.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_real_gen_precon_ilu (f11dac) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  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);

  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%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &a[i - 1], &irow[i - 1],
          &icol[i - 1]);

  /* Calculate incomplete LU factorization */

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