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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_dstevx (f08jbc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */

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

int main(void) {
  /* Scalars */
  double abstol, vl, vu;
  Integer exit_status = 0, i, il = 0, iu = 0, j, m, n, pdz;
  /* Arrays */
  double *d = 0, *e = 0, *w = 0, *z = 0;
  Integer *index = 0;
  /* Nag Types */
  Nag_OrderType order;
  NagError fail, fail_print;

#ifdef NAG_COLUMN_MAJOR
  order = Nag_ColMajor;
#else
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_dstevx (f08jbc) Example Program Results\n\n");

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

  pdz = n;
  /* Allocate memory */
  if (!(d = NAG_ALLOC(n, double)) || !(e = NAG_ALLOC(n, double)) ||
      !(w = NAG_ALLOC(n, double)) || !(z = NAG_ALLOC(pdz * 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,
   * and read the diagonal and off-diagonal elements of the matrix
   * A from data file.
   */
  scanf("%lf%lf%*[^\n]", &vl, &vu);
  for (i = 0; i < n; ++i)
    scanf("%lf", &d[i]);
  scanf("%*[^\n]");

  for (i = 0; i < n - 1; ++i)
    scanf("%lf", &e[i]);
  scanf("%*[^\n]");

  /* nag_machine_real_safe (X02AMC).
   * Set the absolute error tolerance for eigenvalues. With abstol
   * set to zero, the default value would be used instead.
   */
  abstol = nag_machine_real_safe * 2;

  /* nag_lapackeig_dstevx (f08jbc).
   * Solve the symmetric eigenvalue problem.
   */
  nag_lapackeig_dstevx(order, Nag_DoBoth, Nag_Interval, n, d, e, 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_dstevx (f08jbc).\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\n");

  /* nag_file_print_matrix_real_gen (x04cac).
   * Print selected eigenvectors.
   */
  INIT_FAIL(fail_print);
  fflush(stdout);
  nag_file_print_matrix_real_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_real_gen (x04cac).\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" : " ");
    printf("\n");
  }

END:
  NAG_FREE(d);
  NAG_FREE(e);
  NAG_FREE(w);
  NAG_FREE(z);
  NAG_FREE(index);
  return exit_status;
}