/* nag_sparse_nherm_sol (f11dsc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */
#include <nag.h>
#include <nag_stdlib.h>
#include <naga02.h>
#include <nagf11.h>
int main(void)
{
  /* Scalars */
  Integer                 exit_status = 0;
  double                  omega, rnorm, tol;
  Integer                 i, itn, m, maxitn, n, nnz;
  /* Arrays */
  Complex                 *a = 0, *b = 0, *x = 0;
  Integer                 *icol = 0, *irow = 0;
  char                    nag_enum_arg[40];
  /* NAG types */
  Nag_SparseNsym_Method   method;
  Nag_SparseNsym_PrecType precon;
  NagError                fail;

  INIT_FAIL(fail);

  printf("nag_sparse_nherm_sol (f11dsc) Example Program Results\n\n");
  /* Skip heading in data file*/
  scanf("%*[^\n]");
  scanf("%ld%*[^\n]", &n);
  scanf("%ld%*[^\n]", &nnz);
  scanf("%39s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  method = (Nag_SparseNsym_Method) nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s%*[^\n]", nag_enum_arg);
  precon = (Nag_SparseNsym_PrecType) nag_enum_name_to_value(nag_enum_arg);
  scanf("%lf%*[^\n]", &omega);
  scanf("%ld%lf%ld%*[^\n]", &m, &tol, &maxitn);
  if (
      !(a = NAG_ALLOC((nnz), Complex)) ||
      !(b = NAG_ALLOC((n), Complex)) ||
      !(x = NAG_ALLOC((n), Complex)) ||
      !(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 ) %ld%ld%*[^\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);

  /* solve ax = b */
  /* nag_sparse_nherm_sol (f11dsc).
   * Solution of complex sparse non-Hermitian linear system, RGMRES, CGS,
   * Bi-CGSTAB or TFQMR method, Jacobi or SSOR preconditioner Black Box.
   */
  nag_sparse_nherm_sol(method, precon, n, nnz, a, irow, icol, omega, b, m, tol,
                       maxitn, x, &rnorm, &itn, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparse_nherm_sol (f11dsc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("Converged in%13ld iterations\n", itn);
  printf("Final residual norm = %11.3e\n\n", rnorm);
  /* Output x*/
  printf("%14s\n","Solution");
  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(icol);
  NAG_FREE(irow);
  return exit_status;
}