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

NAG CL Interface Introduction
Example description
/* nag_sparseig_feast_complex_symm_solve (f12jsc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.1, 2024.
 */
#include <nag.h>

#define A(I, J) a[(J - 1) * pda + I - 1]
#define AZ(I, J) az[(J - 1) * n + I - 1]
#define B(I, J) b[(J - 1) * pdb + I - 1]

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer i, j, n, pda, pdb, pdx, pdy, pdz, m0, iter, nconv, irevcm, exit_loop;
  double r, eps;
  Complex ze, emid, cone, czero;
  /* Arrays */
  Complex *a = 0, *x = 0, *z = 0, *d = 0, *b = 0;
  double *resid = 0;
  Complex *y = 0, *az = 0;
  Integer *ipiv = 0;
  void *handle = 0;
  /* Nag Types */
  Nag_OrderType order = Nag_ColMajor;
  NagError fail;

  INIT_FAIL(fail);

  /* Output preamble */
  printf("nag_sparseig_feast_complex_symm_solve (f12jsc) ");
  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);

  pda = n;
  pdb = n;
  pdx = n;
  pdy = n;
  pdz = n;
  m0 = n;

  cone = nag_complex_create(1.0, 0.0);
  czero = nag_complex_create(0.0, 0.0);

  if (!(a = NAG_ALLOC(pda * n, Complex)) ||
      !(b = NAG_ALLOC(pdb * n, Complex)) ||
      !(x = NAG_ALLOC(pdx * m0, Complex)) ||
      !(y = NAG_ALLOC(pdy * m0, Complex)) ||
      !(z = NAG_ALLOC(pdz * m0, Complex)) || !(resid = NAG_ALLOC(m0, double)) ||
      !(d = NAG_ALLOC(m0, Complex)) || !(az = NAG_ALLOC(n * n, Complex)) ||
      !(ipiv = NAG_ALLOC(n, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

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

  /* Read in the matrix B from data file */
  for (i = 1; i <= n; i++)
    for (j = i; j <= n; j++)
      scanf(" ( %lf , %lf ) ", &B(i, j).re, &B(i, j).im);

  /* 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_sparseig_feast_init (f12jac)\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Set options using nag_sparseig_feast_option (f12jbc) */
  nag_sparseig_feast_option(handle, "Ellipse Contour Ratio = 10", &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparseig_feast_option (f12jbc)\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }
  nag_sparseig_feast_option(handle, "Ellipse Rotation = 45", &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparseig_feast_option (f12jbc)\n%s\n", fail.message);
    exit_status = 3;
    goto END;
  }

  emid = nag_complex_create(0.0, 0.0);
  r = 1.0;
  /* Generate the contour using nag_sparseig_feast_gen_contour (f12jfc) */
  nag_sparseig_feast_gen_contour(handle, emid, r, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparseig_feast_gen_contour (f12jfc)\n%s\n",
           fail.message);
    exit_status = 4;
    goto END;
  }

  exit_loop = 0;
  irevcm = 0;

  do {
    /* Call solver nag_sparseig_feast_complex_symm_solve (f12jsc) */
    nag_sparseig_feast_complex_symm_solve(handle, &irevcm, &ze, n, x, pdx, y,
                                          pdy, &m0, &nconv, d, z, pdz, &eps,
                                          &iter, resid, &fail);
    switch (irevcm) {
    case 1:
      /* Form the matrix ze B-A */
      for (j = 1; j <= n; j++) {
        for (i = 1; i <= n; i++) {
          AZ(i, j) =
              nag_complex_subtract(nag_complex_multiply(ze, B(i, j)), A(i, j));
        }
      }
      /* Compute Bunch Kaufman factorization of ze B - A */
      nag_lapacklin_zsytrf(Nag_ColMajor, Nag_Upper, n, az, n, ipiv, &fail);
      if (fail.code != NE_NOERROR) {
        exit_loop = 1;
      }
      break;
    case 2:
      /* Solve the linear system (ze B -A)w = y, overwriting y with w */
      nag_lapacklin_zsytrs(Nag_ColMajor, Nag_Upper, n, m0, az, n, ipiv, y, pdy,
                           &fail);
      if (fail.code != NE_NOERROR) {
        exit_loop = 1;
      }
      break;
    case 3:
      /* Compute x <- Az */
      nag_zsymm(Nag_ColMajor, Nag_LeftSide, Nag_Upper, n, m0, cone, a, pda, z,
                pdz, czero, x, pdx, &fail);
      if (fail.code != NE_NOERROR) {
        exit_loop = 1;
      }
      break;
    case 4:
      /* Compute x <- Bz */
      nag_zsymm(Nag_ColMajor, Nag_LeftSide, Nag_Upper, n, m0, cone, b, pdb, z,
                pdz, czero, x, pdx, &fail);
      if (fail.code != NE_NOERROR) {
        exit_loop = 1;
      }
      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 = 5;
    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, "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(b);
  NAG_FREE(az);
  NAG_FREE(x);
  NAG_FREE(y);
  NAG_FREE(z);
  NAG_FREE(resid);
  NAG_FREE(d);
  NAG_FREE(ipiv);

  /* Destroy the data 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;
}