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

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

#define A(I, J, K) a[(K - 1) * pda * n + (J - 1) * pda + I - 1]
#define AZ(I, J) az[(J - 1) * n + I - 1]
#define Z(I, J) z[(J - 1) * pdz + I - 1]

int main(void) {
  /* Scalars */
  Integer exit_status = 0;
  Integer i, j, n, k, pda, pdx, pdy, pdz, m0, iter, nconv, irevcm, exit_loop,
      deg, l;
  double r, eps;
  Complex ze, emid, cone, czero, tmp1, tmp2;
  /* Arrays */
  Complex *a = 0, *x = 0, *z = 0, *d = 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_poly_symm_solve (f12juc) ");
  printf("Example Program Results\n\n");
  fflush(stdout);

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

  /* Read in the matrix size and the polynomial degree */
  scanf("%" NAG_IFMT "", &n);
  scanf("%" NAG_IFMT "", &deg);
  scanf("%*[^\n]");

  pda = n;
  pdx = n;
  pdy = n;
  pdz = n;
  m0 = n;
  if (!(a = NAG_ALLOC(pda * n * (deg + 1), 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;
  }

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

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

  /* Initialize the 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 option using nag_sparseig_feast_option (f12jbc) */
  nag_sparseig_feast_option(handle, "Ellipse Contour Ratio = 80", &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sparseig_feast_option (f12jbc)\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  emid = nag_complex_create(-2.0, -2.0);
  r = 1.5;
  /* 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 = 3;
    goto END;
  }

  exit_loop = 0;
  irevcm = 0;

  do {
    /* Call solver nag_sparseig_feast_poly_symm_solve (f12juc) */
    nag_sparseig_feast_poly_symm_solve(handle, &irevcm, deg, &ze, n, x, pdx, y,
                                       pdy, &k, &m0, &nconv, d, z, pdz, &eps,
                                       &iter, resid, &fail);
    switch (irevcm) {
    case 1:
      /* Form the matrix \sum ze^i A_i */
      for (j = 1; j <= n; j++) {
        for (i = 1; i <= j; i++) {
          AZ(i, j) = A(i, j, 1);
        }
      }
      for (l = 1; l <= deg; l++) {
        for (j = 1; j <= n; j++) {
          for (i = 1; i <= j; i++) {
            tmp1 =
                nag_complex_add(nag_complex_multiply(
                                    A(i, j, l + 1), nag_complex_i_power(ze, l)),
                                AZ(i, j));
            AZ(i, j) = tmp1;
          }
        }
      }
      /* Compute a Bunch-Kaufman factorization of \sum ze^i A_i */
      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 (\sum ze^i A_i)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 <- A_k z */
      nag_zsymm(Nag_ColMajor, Nag_LeftSide, Nag_Upper, n, m0, cone,
                &A(1, 1, k + 1), pda, 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 = 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");

  /* Normalize the eigenvectors: unit first element */
  for (j = 1; j <= nconv; j++) {
    tmp2 = Z(1, j);
    for (i = 1; i <= n; i++) {
      tmp1 = Z(i, j);
      Z(i, j) = nag_complex_divide(tmp1, tmp2);
    }
  }

  /* 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 = 5;
    goto END;
  }

END:
  NAG_FREE(a);
  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 = 6;
  }

  return exit_status;
}