/* nag_superlu_column_permutation (f11mdc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 8, 2005.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf11.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_superlu_column_permutation (f11mdc) Example Program Results"
    "\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read order of matrix */
  scanf("%ld%*[^\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("%ld%*[^\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("%ld%*[^\n] ", &irowix[i - 1]);
  /* Calculate COLAMD permutation */
  ispec = Nag_Sparse_Colamd;
  /* nag_superlu_column_permutation (f11mdc).
   * Real sparse nonsymmetric linear systems, setup for
   * nag_superlu_lu_factorize (f11mec)
   */
  nag_superlu_column_permutation(ispec, n, icolzp, irowix, iprm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf(
              "Error from nag_superlu_column_permutation (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("%6ld%s", iprm[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_superlu_column_permutation (f11mdc), see above. */
  nag_superlu_column_permutation(ispec, n, icolzp, irowix, iprm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf(
              "Error from nag_superlu_column_permutation (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("%6ld%s", iprm[i - 1], i%10 == 0 || i == n?"\n":" ");

  /* Calculate natural permutation */
  ispec = Nag_Sparse_Identity;
  /* nag_superlu_column_permutation (f11mdc), see above. */
  nag_superlu_column_permutation(ispec, n, icolzp, irowix, iprm, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf(
              "Error from nag_superlu_column_permutation (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("%6ld%s", iprm[i - 1], i%10 == 0 || i == n?"\n":" ");
  printf("\n");

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

  return exit_status;
}