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

NAG CL Interface Introduction
Example description
/* nag_sparse_direct_real_gen_setup (f11mdc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.3, 2021.
 */

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

int main(void) {
  Integer exit_status = 0, i, n, nnz;
  Integer *icolzp = 0, *iprm = 0, *irowix = 0;
  /* Nag types */
  Nag_ColumnPermutationType ispec;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_sparse_direct_real_gen_setup (f11mdc) Example Program Results"
         "\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read order of matrix */
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
  if (!(icolzp = NAG_ALLOC(n + 1, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Read the matrix A */
  for (i = 1; i <= n + 1; ++i)
    scanf("%" NAG_IFMT "%*[^\n] ", &icolzp[i - 1]);
  nnz = icolzp[n] - 1;
  if (!(irowix = NAG_ALLOC(nnz, Integer)) ||
      !(iprm = NAG_ALLOC(7 * n, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 1; i <= nnz; ++i)
    scanf("%" NAG_IFMT "%*[^\n] ", &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;
  }

  /* Output results */
  printf("\n");
  printf("%s\n", "COLAMD Permutation");
  for (i = 1; i <= n; ++i)
    printf("%6" NAG_IFMT "%s", iprm[n + i - 1],
           i % 10 == 0 || i == n ? "\n" : " ");

  /* Calculate user permutation */
  ispec = Nag_Sparse_User;
  iprm[0] = 4;
  iprm[1] = 3;
  iprm[2] = 2;
  iprm[3] = 1;
  iprm[4] = 0;
  /* nag_sparse_direct_real_gen_setup (f11mdc), see above. */
  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;
  }
  /* Output results */
  printf("\n");
  printf("%s", "User Permutation");
  printf("\n");
  for (i = 1; i <= n; ++i)
    printf("%6" NAG_IFMT "%s", iprm[n + i - 1],
           i % 10 == 0 || i == n ? "\n" : " ");

  /* Calculate natural permutation */
  ispec = Nag_Sparse_Identity;
  /* nag_sparse_direct_real_gen_setup (f11mdc), see above. */
  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;
  }
  /* Output results */
  printf("\n");
  printf("%s\n", "Natural Permutation");
  for (i = 1; i <= n; ++i)
    printf("%6" NAG_IFMT "%s", iprm[n + i - 1],
           i % 10 == 0 || i == n ? "\n" : " ");
  printf("\n");

END:
  NAG_FREE(icolzp);
  NAG_FREE(iprm);
  NAG_FREE(irowix);

  return exit_status;
}