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

NAG CL Interface Introduction
Example description
/* nag_sparse_direct_real_gen_lu (f11mec) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.3, 2022.
 */

#include <nag.h>
#include <stdio.h>

/* Table of constant values */

int main(void) {
  double flop, thresh;
  Integer exit_status = 0, i, n, nnz, nnzl, nnzu, nzlmx, nzlumx, nzumx;
  double *a = 0, *lval = 0, *uval = 0;
  Integer *icolzp = 0, *il = 0, *iprm = 0, *irowix = 0;
  Integer *iu = 0;
  /* Nag types */
  NagError fail;
  Nag_ColumnPermutationType ispec;
  Nag_OrderType order = Nag_ColMajor;
  Nag_MatrixType matrix = Nag_GeneralMatrix;
  Nag_DiagType diag = Nag_NonUnitDiag;

  INIT_FAIL(fail);

  /* nag_sparse_direct_real_gen_lu (f11mec).
   * LU factorization of real sparse matrix
   */
  printf("nag_sparse_direct_real_gen_lu (f11mec) Example Program Results\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read order of matrix */
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
  /* Read the matrix A */
  /* Allocate memory */
  if (!(icolzp = NAG_ALLOC(n + 1, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 1; i <= n + 1; ++i)
    scanf("%" NAG_IFMT "%*[^\n] ", &icolzp[i - 1]);
  nnz = icolzp[n] - 1;
  /* Allocate memory */
  if (!(irowix = NAG_ALLOC(nnz, Integer)) || !(a = NAG_ALLOC(nnz, double)) ||
      !(il = NAG_ALLOC(7 * n + 8 * nnz + 4, Integer)) ||
      !(iu = NAG_ALLOC(2 * n + 8 * nnz + 1, Integer)) ||
      !(uval = NAG_ALLOC(8 * nnz, double)) ||
      !(lval = NAG_ALLOC(8 * nnz, double)) ||
      !(iprm = NAG_ALLOC(7 * n, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 1; i <= nnz; ++i)
    scanf("%lf%" NAG_IFMT "%*[^\n] ", &a[i - 1], &irowix[i - 1]);
  /* Calculate COLAMD permutation */
  ispec = Nag_Sparse_Colamd;
  /* nag_sparse_direct_real_gen_setup (f11mdc).
   * Real sparse nonsymmetric linear systems, setup for
   * nag_sparse_direct_real_gen_lu (f11mec)
   */
  nag_sparse_direct_real_gen_setup(ispec, n, icolzp, irowix, iprm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_direct_real_gen_setup (f11mdc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  /* Factorise */
  thresh = 1.;
  nzlmx = 8 * nnz;
  nzlumx = 8 * nnz;
  nzumx = 8 * nnz;
  /* nag_sparse_direct_real_gen_lu (f11mec), see above. */
  nag_sparse_direct_real_gen_lu(n, irowix, a, iprm, thresh, nzlmx, &nzlumx,
                                nzumx, il, lval, iu, uval, &nnzl, &nnzu, &flop,
                                &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_direct_real_gen_lu (f11mec).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Output results */
  printf("\n");
  printf("Number of nonzeros in factors (excluding unit diagonal)\n");
  printf("%8" NAG_IFMT "\n\n", nnzl + nnzu - n);
  /* nag_file_print_matrix_real_gen_comp (x04cbc).
   * Print real general matrix (comprehensive)
   */
  fflush(stdout);
  nag_file_print_matrix_real_gen_comp(
      order, matrix, diag, 1, 10, lval, 1, "%6.2f", "Factor elements in LVAL",
      Nag_NoLabels, NULL, Nag_NoLabels, NULL, 80, 1, 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  /* nag_file_print_matrix_real_gen_comp (x04cbc), see above. */
  fflush(stdout);
  nag_file_print_matrix_real_gen_comp(
      order, matrix, diag, 1, 4, uval, 1, "%6.2f", "Factor elements in LVAL",
      Nag_NoLabels, NULL, Nag_NoLabels, NULL, 80, 1, 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen_comp (x04cbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

END:
  NAG_FREE(a);
  NAG_FREE(lval);
  NAG_FREE(uval);
  NAG_FREE(icolzp);
  NAG_FREE(il);
  NAG_FREE(iprm);
  NAG_FREE(irowix);
  NAG_FREE(iu);

  return exit_status;
}