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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_zhbevx (f08hpc) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.6, 2022.
 */

#include <nag.h>
#include <stdio.h>

int main(void) {
  /* Scalars */
  double abstol, vl, vu;
  Integer exit_status = 0, i, il = 0, iu = 0, j, kd, m, n, pdab, pdq, pdz;
  /* Arrays */
  char nag_enum_arg[40];
  Complex *ab = 0, *q = 0, *z = 0;
  double *w = 0;
  Integer *index = 0;
  /* Nag Types */
  Nag_OrderType order;
  Nag_UploType uplo;
  NagError fail, fail_print;

#ifdef NAG_COLUMN_MAJOR
#define AB_UPPER(I, J) ab[(J - 1) * pdab + kd + I - J]
#define AB_LOWER(I, J) ab[(J - 1) * pdab + I - J]
  order = Nag_ColMajor;
#else
#define AB_UPPER(I, J) ab[(I - 1) * pdab + J - I]
#define AB_LOWER(I, J) ab[(I - 1) * pdab + kd + J - I]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_zhbevx (f08hpc) Example Program Results\n\n");

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

  /* Read uplo */
  scanf("%39s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  uplo = (Nag_UploType)nag_enum_name_to_value(nag_enum_arg);

  /* Allocate memory */
  if (!(ab = NAG_ALLOC((kd + 1) * n, Complex)) ||
      !(q = NAG_ALLOC(n * n, Complex)) || !(z = NAG_ALLOC(n * n, Complex)) ||
      !(w = NAG_ALLOC(n, double)) || !(index = NAG_ALLOC(n, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  pdab = kd + 1;
  pdq = n;
  pdz = n;

  /* Read the lower and upper bounds of the interval to be searched,
   * and read the upper or lower triangular part of the matrix A
   * from data file.
   */
  scanf("%lf%lf%*[^\n]", &vl, &vu);
  if (uplo == Nag_Upper) {
    for (i = 1; i <= n; ++i)
      for (j = i; j <= MIN(n, i + kd); ++j)
        scanf(" ( %lf , %lf )", &AB_UPPER(i, j).re, &AB_UPPER(i, j).im);
    scanf("%*[^\n]");
  } else if (uplo == Nag_Lower) {
    for (i = 1; i <= n; ++i)
      for (j = MAX(1, i - kd); j <= i; ++j)
        scanf(" ( %lf , %lf )", &AB_LOWER(i, j).re, &AB_LOWER(i, j).im);
    scanf("%*[^\n]");
  }

  /* Set the absolute error tolerance for eigenvalues.
   * With abstol set to zero, the default value is used instead.
   */
  abstol = 0.0;

  /* nag_lapackeig_zhbevx (f08hpc).
   * Solve the band symmetric eigenvalue problem.
   */
  nag_lapackeig_zhbevx(order, Nag_DoBoth, Nag_Interval, uplo, n, kd, ab, pdab,
                       q, pdq, vl, vu, il, iu, abstol, &m, w, z, pdz, index,
                       &fail);
  if (fail.code != NE_NOERROR && fail.code != NE_CONVERGENCE) {
    printf("Error from nag_lapackeig_zhbevx (f08hpc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print solution */
  printf("Number of eigenvalues found =%5" NAG_IFMT "\n", m);

  printf("\nEigenvalues\n");
  for (j = 0; j < m; ++j)
    printf("%8.4f%s", w[j], (j + 1) % 8 == 0 ? "\n" : " ");
  printf("\n");

  /* nag_file_print_matrix_complex_gen (x04dac).
   * Print selected eigenvectors.
   */
  INIT_FAIL(fail_print);
  fflush(stdout);
  nag_file_print_matrix_complex_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                                    n, m, z, pdz, "Selected eigenvectors", 0,
                                    &fail_print);
  if (fail_print.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_complex_gen (x04dac).\n%s\n",
           fail_print.message);
    exit_status = 1;
    goto END;
  }
  if (fail.code == NE_CONVERGENCE) {
    printf("eigenvectors failed to converge\n");
    printf("Indices of eigenvectors that did not converge\n");
    for (j = 0; j < m; ++j)
      printf("%8" NAG_IFMT "%s", index[j], (j + 1) % 8 == 0 ? "\n" : " ");
  }

END:
  NAG_FREE(ab);
  NAG_FREE(q);
  NAG_FREE(z);
  NAG_FREE(w);
  NAG_FREE(index);

  return exit_status;
}

#undef AB_UPPER
#undef AB_LOWER