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

NAG CL Interface Introduction
Example description
/* nag_sparse_complex_gen_sort (f11znc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */

#include <nag.h>

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer i, n, nnz;
  /* Arrays */
  char nag_enum_arg[40];
  Integer *irow = 0, *icol = 0, *istr = 0;
  Complex *a = 0;
  /* NAG types */
  NagError fail;
  Nag_SparseNsym_Dups dup;
  Nag_SparseNsym_Zeros zero;

  INIT_FAIL(fail);

  printf("nag_sparse_complex_gen_sort (f11znc) 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 */
  if (!(a = NAG_ALLOC(nnz, Complex)) || !(icol = NAG_ALLOC(nnz, Integer)) ||
      !(irow = NAG_ALLOC(nnz, Integer)) ||
      !(istr = NAG_ALLOC((n + 1), Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

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

    /* Reorder, sum duplicates and remove zeros */
    /* Nag_SparseNsym_SumDups */
  scanf("%39s%*[^\n]", nag_enum_arg);
  dup = (Nag_SparseNsym_Dups)nag_enum_name_to_value(nag_enum_arg);

  /* Nag_SparseNsym_RemoveZeros */
  scanf("%39s%*[^\n]", nag_enum_arg);
  zero = (Nag_SparseNsym_Zeros)nag_enum_name_to_value(nag_enum_arg);

  /* Output original */
  printf("Original elements\n");
  printf("%s%4" NAG_IFMT "\n", " n   = ", n);
  printf("%s%4" NAG_IFMT "\n", " nnz = ", nnz);

  printf("%9s%14s%22s%9s\n", "i", "a", "irow", "icol");
  for (i = 0; i < nnz; i++)
    printf("%9" NAG_IFMT " (%13.4e, %13.4e)%9" NAG_IFMT "%9" NAG_IFMT "\n", i,
           a[i].re, a[i].im, irow[i], icol[i]);

  /* nag_sparse_complex_gen_sort (f11znc).
   * Complex sparse non-Hermitian matrix reorder function.
   */
  nag_sparse_complex_gen_sort(n, &nnz, a, irow, icol, dup, zero, istr, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_complex_gen_sort (f11znc)\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Output results */
  printf("\nReordered elements\n");
  printf("%s%4" NAG_IFMT "\n", " nnz = ", nnz);

  printf("%9s%14s%22s%9s\n", "i", "a", "irow", "icol");
  for (i = 0; i < nnz; i++)
    printf("%9" NAG_IFMT " (%13.4e, %13.4e)%9" NAG_IFMT "%9" NAG_IFMT "\n", i,
           a[i].re, a[i].im, irow[i], icol[i]);

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

  return exit_status;
}