/* nag_sparse_nsym_sort (f11zac) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 *
 */

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

int main(void)
{
  double *a = 0, anorm = 0.0;
  Integer *icol = 0;
  Integer *irow = 0, *istr = 0;
  Integer exit_status = 0, i, n, nnz;
  Nag_SparseNsym_Zeros zero;
  Nag_SparseNsym_Dups dup;
  Nag_NormType norm;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_sparse_nsym_sort (f11zac) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf(" %*[^\n]");

  /* Read order of matrix and number of nonzero entries */
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  scanf("%" NAG_IFMT "%*[^\n]", &nnz);

  /* Allocate memory */
  istr = NAG_ALLOC(n + 1, Integer);
  a = NAG_ALLOC(nnz, double);
  irow = NAG_ALLOC(nnz, Integer);
  icol = NAG_ALLOC(nnz, Integer);

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

  /* Read and output the original nonzero elements */
  for (i = 0; i < nnz; ++i)
    scanf("%lf%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &a[i], &irow[i], &icol[i]);

  printf("Original elements \n");
  printf("nnz =  %4" NAG_IFMT "\n", nnz);
  printf("%8s%16s%8s%8s\n", "", "a", "irow", "icol");
  for (i = 0; i < nnz; ++i)
    printf("%8" NAG_IFMT "%16.4e%8" NAG_IFMT "%8" NAG_IFMT "\n", i, a[i],
           irow[i], icol[i]);

  /* Reorder along rows, sum duplicates and remove zeros */

  dup = Nag_SparseNsym_SumDups;
  zero = Nag_SparseNsym_RemoveZeros;

  /* nag_sparse_nsym_sort (f11zac).
   * Sparse sort (nonsymmetric)
   */
  nag_sparse_nsym_sort(n, &nnz, a, irow, icol, dup, zero, istr, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_nsym_sort (f11zac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Output results */
  printf("Reordered elements, along rows first\n");
  printf("nnz = %4" NAG_IFMT "\n", nnz);

  for (i = 0; i < nnz; ++i)
    printf("%8" NAG_IFMT "%16.4e%8" NAG_IFMT "%8" NAG_IFMT "\n", i, a[i],
           irow[i], icol[i]);

  /* Reorder down columns, fail on duplicates or zeros.
   *  Creates CCS storage format as side-effect
   */

  dup = Nag_SparseNsym_FailDups;
  zero = Nag_SparseNsym_FailZeros;
  INIT_FAIL(fail);

  /* nag_sparse_nsym_sort (f11zac).
   * Sparse sort (nonsymmetric)
   */
  nag_sparse_nsym_sort(n, &nnz, a, icol, irow, dup, zero, istr, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_nsym_sort (f11zac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Output results */
  printf("Reordered elements, along columns first\n");
  printf("nnz = %4" NAG_IFMT "\n", nnz);

  for (i = 0; i < nnz; ++i)
    printf("%8" NAG_IFMT "%16.4e%8" NAG_IFMT "%8" NAG_IFMT "\n", i, a[i],
           irow[i], icol[i]);
  printf("%8s%8s\n", "", "istr");
  for (i = 0; i <= n; ++i)
    printf("%8" NAG_IFMT "%8" NAG_IFMT "\n", i, istr[i]);

  /* Calculate 1-norm in Compressed Column Storage format */
  norm = Nag_RealOneNorm;
  INIT_FAIL(fail);

  /* nag_superlu_matrix_norm (f11mlc).
   * 1-norm, infinity-norm, largest absolute element, real
   * general matrix
   */
  nag_superlu_matrix_norm(norm, &anorm, n, istr, irow, a, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_superlu_matrix_norm (f11mlc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  /* Output norm */
  printf("%s %16.4e\n", "One-norm", anorm);

END:
  NAG_FREE(istr);
  NAG_FREE(irow);
  NAG_FREE(icol);
  NAG_FREE(a);

  return exit_status;
}