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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_zhpgvx (f08tpc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.2, 2021.
 */

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

int main(void) {
  /* Scalars */
  double abstol, vl, vu;
  Integer i, il = 0, iu = 0, j, m, n, pdz;
  Integer exit_status = 0;

  /* Arrays */
  Complex *ap = 0, *bp = 0, *z = 0;
  double *w = 0;
  Integer *index = 0;
  char nag_enum_arg[40];

  /* Nag Types */
  NagError fail;
  Nag_OrderType order;
  Nag_UploType uplo;

#ifdef NAG_COLUMN_MAJOR
#define A_UPPER(I, J) ap[J * (J - 1) / 2 + I - 1]
#define A_LOWER(I, J) ap[(2 * n - J) * (J - 1) / 2 + I - 1]
#define B_UPPER(I, J) bp[J * (J - 1) / 2 + I - 1]
#define B_LOWER(I, J) bp[(2 * n - J) * (J - 1) / 2 + I - 1]
#define Z(I, J) z[(J - 1) * pdz + I - 1]
  order = Nag_ColMajor;
#else
#define A_UPPER(I, J) ap[(2 * n - I) * (I - 1) / 2 + J - 1]
#define A_LOWER(I, J) ap[I * (I - 1) / 2 + J - 1]
#define B_UPPER(I, J) bp[(2 * n - I) * (I - 1) / 2 + J - 1]
#define B_LOWER(I, J) bp[I * (I - 1) / 2 + J - 1]
#define Z(I, J) z[(I - 1) * pdz + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_zhpgvx (f08tpc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%*[^\n]", &n);
  if (n < 0) {
    printf("Invalid n\n");
    exit_status = 1;
    goto END;
    ;
  }
  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);

  m = n;
#ifdef NAG_COLUMN_MAJOR
  pdz = n;
#else
  pdz = m;
#endif
  /* Allocate memory */
  if (!(ap = NAG_ALLOC(n * (n + 1) / 2, Complex)) ||
      !(bp = NAG_ALLOC(n * (n + 1) / 2, Complex)) ||
      !(z = NAG_ALLOC(n * m, Complex)) || !(w = NAG_ALLOC(n, double)) ||
      !(index = NAG_ALLOC(n, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the lower and upper bounds of the interval to be searched. */
  scanf("%lf%lf%*[^\n]", &vl, &vu);
  /* Read the triangular parts of the matrices A and B from data file. */
  if (uplo == Nag_Upper) {
    for (i = 1; i <= n; ++i)
      for (j = i; j <= n; ++j)
        scanf(" ( %lf , %lf )", &A_UPPER(i, j).re, &A_UPPER(i, j).im);
    scanf("%*[^\n]");
    for (i = 1; i <= n; ++i)
      for (j = i; j <= n; ++j)
        scanf(" ( %lf , %lf )", &B_UPPER(i, j).re, &B_UPPER(i, j).im);
  } else if (uplo == Nag_Lower) {
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= i; ++j)
        scanf(" ( %lf , %lf )", &A_LOWER(i, j).re, &A_LOWER(i, j).im);
    scanf("%*[^\n]");
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= i; ++j)
        scanf(" ( %lf , %lf )", &B_LOWER(i, j).re, &B_LOWER(i, j).im);
  }
  scanf("%*[^\n]");

  /* Use the default absolute error tolerance for eigenvalues. */
  abstol = 0.0;

  /* Solve the generalized Hermitian eigenvalue problem
   * A*x = lambda*B*x (itype = 1). using nag_lapackeig_zhpgvx (f08tpc).
   */
  nag_lapackeig_zhpgvx(order, 1, Nag_DoBoth, Nag_Interval, uplo, n, ap, bp, vl,
                       vu, il, iu, abstol, &m, w, z, pdz, index, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zhpgvx (f08tpc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Normalize the eigenvectors */
  for (j = 1; j <= m; j++)
    for (i = n; i >= 1; i--)
      Z(i, j) = nag_complex_divide(Z(i, j), Z(1, j));

  /* Print eigensolution */
  printf("Number of eigenvalues found =%5" NAG_IFMT "\n\n", m);
  printf(" Eigenvalues\n  ");
  for (j = 0; j < m; ++j)
    printf(" %11.4f%s", w[j], j % 8 == 7 ? "\n" : "");
  printf("\n");

  /* Print normalized vectors using nag_file_print_matrix_complex_gen (x04dac).
   */
  fflush(stdout);
  nag_file_print_matrix_complex_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
                                    n, m, z, pdz, "Selected eigenvectors", 0,
                                    &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_complex_gen (x04dac).\n%s\n",
           fail.message);
    exit_status = 1;
  }

END:
  NAG_FREE(ap);
  NAG_FREE(bp);
  NAG_FREE(z);
  NAG_FREE(w);
  NAG_FREE(index);

  return exit_status;
}