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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_zheevr (f08frc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

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

int main(void) {
  /* Scalars */
  double abstol, vl = 0.0, vu = 0.0;
  Integer i, il, iu, j, m, n, pda, pdz;
  Integer exit_status = 0;
  /* Arrays */
  Complex *a = 0, *z = 0;
  double *w = 0;
  Integer *isuppz = 0;
  /* Nag Types */
  Nag_OrderType order;
  NagError fail;

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
#define Z(I, J) z[(J - 1) * pdz + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
#define Z(I, J) z[(I - 1) * pdz + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_zheevr (f08frc) Example Program Results\n\n");

  /* Skip heading in data file and read n and the lower and upper
   * indices of the smallest and largest eigenvalues to be found.
   */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &n, &il, &iu);

  m = iu - il + 1;
  pda = n;
  pdz = n;

  /* Allocate memory */
  if (!(a = NAG_ALLOC(n * n, Complex)) || !(z = NAG_ALLOC(n * pdz, Complex)) ||
      !(w = NAG_ALLOC(n, double)) || !(isuppz = NAG_ALLOC(2 * pdz, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the upper triangular part of 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]");

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

  /* nag_lapackeig_zheevr (f08frc).
   * Solve the symmetric eigenvalue problem.
   */
  nag_lapackeig_zheevr(order, Nag_DoBoth, Nag_Indices, Nag_Upper, n, a, pda, vl,
                       vu, il, iu, abstol, &m, w, z, pdz, isuppz, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zheevr (f08frc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* nag_complex_divide (a02cdc).
   * 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 solution */
  printf("Selected eigenvalues\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.
   */
  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;
    goto END;
  }

END:
  NAG_FREE(a);
  NAG_FREE(z);
  NAG_FREE(w);
  NAG_FREE(isuppz);

  return exit_status;
}

#undef A
#undef Z