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

NAG CL Interface Introduction
Example description
/* nag_sparseig_feast_complex_gen_solve (f12jtc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */
#include <math.h>
#include <nag.h>

#define X(I, J) x[(J - 1) * pdx + I - 1]
#define Y(I, J) y[(J - 1) * pdy + I - 1]
#define Z(I, J) z[(J - 1) * pdz + I - 1]

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer i, j, m, n, pdx, pdy, pdz, m0, iter, nconv, irevcm, exit_loop,
      nnz, nnzz, nnzzh, npivm, itn, nnzc, nnzch, ccn, la;
  double eps, tol, rnorm;
  Complex ze, cone, czero;
  /* Arrays */
  Complex *a = 0, *x = 0, *z = 0, *w = 0, *zedge = 0, *d = 0;
  double *resid = 0;
  Complex *y = 0, *az = 0, *azh = 0;
  Integer *ipiv = 0, *irow = 0, *icol = 0, *icolz = 0, *irowz = 0, *istr = 0,
          *ipivp = 0, *ipivq = 0, *idiag = 0, *istrh = 0,
          *idiagh = 0, *icolzh = 0, *ipivph = 0, *ipivqh = 0, *irowzh = 0,
          *nedge = 0, *tedge = 0;
  void *handle = 0;
  /* Nag Types */
  Nag_OrderType order = Nag_ColMajor;
  NagError fail;

  INIT_FAIL(fail);

  /* Output preamble */
  printf("nag_sparseig_feast_complex_gen_solve (f12jtc) ");
  printf("Example Program Results\n\n");
  fflush(stdout);

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

  /* Read in the matrix size and the required rank */
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  scanf("%" NAG_IFMT "%*[^\n]", &nnz);

  pdx = n;
  pdy = n;
  pdz = n;
  m0 = n;
  m = MIN(n, 50);
  la = 2 * (nnz + n);

  tol = sqrt(X02AJC);
  cone = nag_complex_create(1.0, 0.0);
  czero = nag_complex_create(0.0, 0.0);

  /* Memory allocation */
  if (!(a = NAG_ALLOC(nnz, Complex)) || !(icol = NAG_ALLOC(nnz, Integer)) ||
      !(irow = NAG_ALLOC(nnz, Integer)) ||
      !(x = NAG_ALLOC(pdx * 2 * m0, Complex)) ||
      !(y = NAG_ALLOC(pdy * m0, Complex)) ||
      !(z = NAG_ALLOC(pdz * 2 * m0, Complex)) ||
      !(resid = NAG_ALLOC(2 * m0, double)) || !(d = NAG_ALLOC(m0, Complex)) ||
      !(ipiv = NAG_ALLOC(n, Integer)) || !(w = NAG_ALLOC(n, Complex)) ||
      !(az = NAG_ALLOC(la, Complex)) || !(azh = NAG_ALLOC(la, Complex)) ||
      !(icolz = NAG_ALLOC(la, Integer)) || !(irowz = NAG_ALLOC(la, Integer)) ||
      !(icolzh = NAG_ALLOC(la, Integer)) ||
      !(irowzh = NAG_ALLOC(la, Integer)) ||
      !(idiag = NAG_ALLOC(n, Integer)) || !(ipivp = NAG_ALLOC(n, Integer)) ||
      !(ipivq = NAG_ALLOC(n, Integer)) || !(ipivph = NAG_ALLOC(n, Integer)) ||
      !(ipivqh = NAG_ALLOC(n, Integer)) ||
      !(istrh = NAG_ALLOC(n + 1, Integer)) ||
      !(idiagh = NAG_ALLOC(n, Integer)) ||
      !(istr = NAG_ALLOC(n + 1, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read in the matrix A from data file */
  for (i = 0; i < nnz; i++)
    scanf(" ( %lf , %lf ) %" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &a[i].re,
          &a[i].im, &irow[i], &icol[i]);

  /* Initialize the data handle using nag_sparseig_feast_init (f12jac) */
  nag_sparseig_feast_init(&handle, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac)\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Set options using nag_sparseig_feast_option (f12jbc) */
  nag_sparseig_feast_option(handle, "Integration Type = Trapezoidal", &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparseig_feast_option (f12jbc)\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  /* Define arrays for the custom contour */
  ccn = 4;
  if (!(nedge = NAG_ALLOC(ccn, Integer)) ||
      !(tedge = NAG_ALLOC(ccn, Integer)) ||
      !(zedge = NAG_ALLOC(ccn, Complex))) {
    exit_status = -2;
    goto END;
  }

  zedge[0] = nag_complex_create(0.0, 1.0);
  zedge[1] = nag_complex_create(0.0, -1.0);
  zedge[2] = nag_complex_create(-1.0, -1.0);
  zedge[3] = nag_complex_create(-1.0, 1.0);

  tedge[0] = 50;
  tedge[1] = 0;
  tedge[2] = 0;
  tedge[3] = 0;

  nedge[0] = 20;
  nedge[1] = 1;
  nedge[2] = 1;
  nedge[3] = 1;

  /* Generate the contour using nag_sparseig_feast_custom_contour (f12jgc) */
  nag_sparseig_feast_custom_contour(handle, ccn, nedge, tedge, zedge, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparseig_feast_custom_contour (f12jgc)\n%s\n",
           fail.message);
    exit_status = 3;
    goto END;
  }

  exit_loop = 0;
  irevcm = 0;

  do {
    /* Call solver nag_sparseig_feast_real_herm_solve (f12jrc) */
    nag_sparseig_feast_complex_gen_solve(handle, &irevcm, &ze, n, x, pdx, y,
                                         pdy, &m0, &nconv, d, z, pdz, &eps,
                                         &iter, resid, &fail);
    switch (irevcm) {
    case 1:
      /* Form the sparse matrix ze I-A */
      nnzz = nnz + n;
      for (i = 0; i < nnz; i++) {
        az[i] = nag_complex_subtract(czero, a[i]);
        irowz[i] = irow[i];
        icolz[i] = icol[i];
      }
      for (i = 0; i < n; i++) {
        irowz[nnz + i] = i + 1;
        icolz[nnz + i] = i + 1;
        az[nnz + i] = ze;
      }
      /* Sort the elements into correct coordinate storage format using
       * nag_sparse_complex_gen_sort (f11znc)
       */
      nag_sparse_complex_gen_sort(n, &nnzz, az, irowz, icolz,
                                  Nag_SparseNsym_SumDups,
                                  Nag_SparseNsym_RemoveZeros, istr, &fail);
      /* Form incomplete LU factorization of ze I - A using
       * nag_sparse_complex_gen_precon_ilu (f11dnc)
       */
      nag_sparse_complex_gen_precon_ilu(n, nnzz, az, la, irowz, icolz, 0, 0.0,
                                        Nag_SparseNsym_PartialPiv,
                                        Nag_SparseNsym_UnModFact, ipivp, ipivq,
                                        istr, idiag, &nnzc, &npivm, &fail);
      if (fail.code != NE_NOERROR) {
        exit_loop = 1;
      }
      break;
    case 2:
      /* Solve the linear system (ze I - A)w = y, with m0 righthand sides */
      for (j = 1; j <= m0; j++) {
        for (i = 1; i <= n; i++) {
          w[i - 1] = Y(i, j);
          /* Initial guess */
          Y(i, j) = cone;
        }
        /* Call linear system solver for a single righthand side
         * nag_sparse_complex_gen_solve_ilu (f11dqc)
         */
        nag_sparse_complex_gen_solve_ilu(
            Nag_SparseNsym_RGMRES, n, nnzz, az, la, irowz, icolz, ipivp, ipivq,
            istr, idiag, w, m, tol, 500, &Y(1, j), &rnorm, &itn, &fail);
      }
      if (fail.code != NE_NOERROR) {
        exit_loop = 1;
      }
      break;
    case 3:
      /* Form the sparse matrix (ze I - A)^H */
      nnzzh = nnz + n;
      for (i = 0; i < nnz; i++) {
        azh[i] = nag_complex_subtract(czero, nag_complex_conjg(a[i]));
        irowzh[i] = icol[i];
        icolzh[i] = irow[i];
      }
      for (i = 0; i < n; i++) {
        irowzh[nnz + i] = i + 1;
        icolzh[nnz + i] = i + 1;
        azh[nnz + i] = nag_complex_conjg(ze);
      }
      /* Sort the elements into correct coordinate storage format using
       * nag_sparse_complex_gen_sort (f11znc)
       */
      nag_sparse_complex_gen_sort(n, &nnzzh, azh, irowzh, icolzh,
                                  Nag_SparseNsym_SumDups,
                                  Nag_SparseNsym_RemoveZeros, istrh, &fail);
      /* Form incomplete LU factorization of (ze I - A)^H using
       * nag_sparse_complex_gen_precon_ilu (f11dnc)
       */
      nag_sparse_complex_gen_precon_ilu(
          n, nnzzh, azh, la, irowzh, icolzh, 0, 0.0, Nag_SparseNsym_PartialPiv,
          Nag_SparseNsym_UnModFact, ipivph, ipivqh, istrh, idiagh, &nnzch,
          &npivm, &fail);
      if (fail.code != NE_NOERROR) {
        exit_loop = 1;
      }
      break;
    case 4:
      /* Solve the linear system (ze I - A)^H w = y, with m0 righthand sides */
      for (j = 1; j <= m0; j++) {
        for (i = 1; i <= n; i++) {
          w[i - 1] = Y(i, j);
          /* Initial guess */
          Y(i, j) = cone;
        }
        nag_sparse_complex_gen_solve_ilu(Nag_SparseNsym_RGMRES, n, nnzzh, azh,
                                         la, irowzh, icolzh, ipivph, ipivqh,
                                         istrh, idiagh, w, m, tol, 500,
                                         &Y(1, j), &rnorm, &itn, &fail);
      }
      if (fail.code != NE_NOERROR) {
        exit_loop = 1;
      }
      break;
    case 5:
      /* Compute x <- Az */
      for (j = 1; j <= m0; j++) {
        nag_sparse_complex_gen_matvec(Nag_NoTrans, n, nnz, a, irow, icol,
                                      Nag_SparseNsym_NoCheck, &Z(1, j),
                                      &X(1, j), &fail);
      }
      if (fail.code != NE_NOERROR) {
        exit_loop = 1;
      }
      break;
    case 6:
      /* Compute x <- A^H z */
      for (j = 1; j <= m0; j++) {
        nag_sparse_complex_gen_matvec(Nag_ConjTrans, n, nnz, a, irow, icol,
                                      Nag_SparseNsym_NoCheck, &Z(1, j),
                                      &X(1, j), &fail);
      }
      if (fail.code != NE_NOERROR) {
        exit_loop = 1;
      }
      break;
    case 7:
      /* Since we are not solving a generalized eigenvalue problem set x = z */
      for (j = 1; j <= m0; j++) {
        for (i = 1; i <= n; i++) {
          X(i, j) = Z(i, j);
        }
      }
      break;
    case 8:
      /* Since we are not solving a generalized eigenvalue problem set x = z */
      for (j = 1; j <= m0; j++) {
        for (i = 1; i <= n; i++) {
          X(i, j) = Z(i, j);
        }
      }
      break;
    }
  } while (irevcm != 0 && exit_loop == 0);

  if (fail.code != NE_NOERROR) {
    printf("Error during reverse communication solve\n%s\n", fail.message);
    exit_status = 4;
    goto END;
  }

  /* Print solution */
  printf(" Eigenvalues\n");
  for (i = 0; i < nconv; ++i) {
    if (d[i].im == 0.0)
      printf("%13.4e%s", d[i].re, (i + 1) % 4 == 0 ? "\n" : " ");
    else
      printf(" (%13.4e, %13.4e)%s", d[i].re, d[i].im,
             (i + 1) % 4 == 0 ? "\n" : " ");
  }
  printf("\n\n");

  /* Print eigenvectors using nag_file_print_matrix_complex_gen (x04dac) */
  nag_file_print_matrix_complex_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                                    n, nconv, z, pdz, "Right Eigenvectors",
                                    NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_complex_gen (x04dac)\n%s\n",
           fail.message);
    exit_status = 5;
    goto END;
  }
  nag_file_print_matrix_complex_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                                    n, nconv, &z[m0 * pdz], pdz,
                                    "Left Eigenvectors", NULL, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_complex_gen (x04dac)\n%s\n",
           fail.message);
    exit_status = 6;
    goto END;
  }

END:
  NAG_FREE(a);
  NAG_FREE(w);
  NAG_FREE(az);
  NAG_FREE(azh);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(z);
  NAG_FREE(resid);
  NAG_FREE(d);
  NAG_FREE(ipiv);
  NAG_FREE(icolz);
  NAG_FREE(nedge);
  NAG_FREE(tedge);
  NAG_FREE(zedge);
  NAG_FREE(irowz);
  NAG_FREE(icol);
  NAG_FREE(irow);
  NAG_FREE(icolzh);
  NAG_FREE(irowzh);
  NAG_FREE(idiag);
  NAG_FREE(idiagh);
  NAG_FREE(ipivp);
  NAG_FREE(ipivph);
  NAG_FREE(ipivq);
  NAG_FREE(ipivqh);
  NAG_FREE(idiagh);
  NAG_FREE(istr);
  NAG_FREE(istrh);

  /* Destroy the handle using nag_sparseig_feast_free (f12jzc) */
  nag_sparseig_feast_free(&handle, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparseig_feast_free (f12jzc)\n%s\n", fail.message);
    exit_status = 7;
  }

  return exit_status;
}