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

NAG CL Interface Introduction
Example description
/* nag_sparse_complex_herm_solve_jacssor (f11jsc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */

#include <nag.h>

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  double omega, rnorm, tol;
  Integer i, itn, maxitn, n, nnz;
  /* Arrays */
  Complex *a = 0, *b = 0, *x = 0;
  double *rdiag = 0;
  Integer *icol = 0, *irow = 0;
  char nag_enum_arg[40];
  /* NAG types */
  Nag_SparseSym_Method method;
  Nag_SparseSym_PrecType precon;
  NagError fail;

  INIT_FAIL(fail);

  printf(
      "nag_sparse_complex_herm_solve_jacssor (f11jsc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  /* Read algorithmic parameters */
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  scanf("%" NAG_IFMT "%*[^\n]", &nnz);
  scanf("%39s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  method = (Nag_SparseSym_Method)nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s%*[^\n]", nag_enum_arg);
  precon = (Nag_SparseSym_PrecType)nag_enum_name_to_value(nag_enum_arg);
  scanf("%lf%*[^\n]", &omega);
  scanf("%lf%" NAG_IFMT "%*[^\n]", &tol, &maxitn);

  /* Allocate memory */
  if (!(a = NAG_ALLOC((nnz), Complex)) || !(b = NAG_ALLOC((n), Complex)) ||
      !(x = NAG_ALLOC((n), Complex)) || !(rdiag = NAG_ALLOC((n), double)) ||
      !(icol = NAG_ALLOC((nnz), Integer)) ||
      !(irow = NAG_ALLOC((nnz), Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Read the matrix  a */
  for (i = 0; i < nnz; i++)
    scanf(" ( %lf , %lf ) %" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &a[i].re,
          &a[i].im, &irow[i], &icol[i]);
  /* Read rhs vector b and initial approximate solution x */
  for (i = 0; i < n; i++)
    scanf(" ( %lf , %lf ) ", &b[i].re, &b[i].im);
  scanf("%*[^\n]");
  for (i = 0; i < n; i++)
    scanf(" ( %lf , %lf ) ", &x[i].re, &x[i].im);
  scanf("%*[^\n]");

  /* nag_sparse_complex_herm_solve_jacssor (f11jsc).
   * Solution of complex sparse Hermitian linear system, conjugate
   * gradient/Lanczos method, Jacobi or SSOR preconditioner
   */
  nag_sparse_complex_herm_solve_jacssor(method, precon, n, nnz, a, irow, icol,
                                        omega, b, tol, maxitn, x, &rnorm, &itn,
                                        rdiag, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_complex_herm_solve_jacssor (f11jsc)\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  printf("Converged in  %10" NAG_IFMT "  iterations \n", itn);
  printf("Final residual norm = %10.3e\n", rnorm);

  /* Output x */
  printf("\n     Converged Solution\n");
  for (i = 0; i < n; i++)
    printf("(%13.4e, %13.4e)\n", x[i].re, x[i].im);

END:
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(x);
  NAG_FREE(rdiag);
  NAG_FREE(icol);
  NAG_FREE(irow);
  return exit_status;
}