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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_dstegr (f08jlc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.3, 2024.
 */

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

int main(void) {
  /* Scalars */
  double vl = 0.0, vu = 0.0;
  Integer i, j, m, n, pdz;
  Integer exit_status = 0;
  /* Arrays */
  char nag_enum_arg[40];
  double *d = 0, *e = 0, *w = 0, *z = 0;
  Integer *isuppz = 0;
  /* Nag Types */
  Nag_OrderType order;
  Nag_JobType job;
  Nag_RangeType range;
  NagError fail;

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

  INIT_FAIL(fail);

  printf("nag_lapackeig_dstegr (f08jlc) Example Program Results\n\n");

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

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

#ifdef NAG_COLUMN_MAJOR
  pdz = n;
#else
  pdz = m;
#endif

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

  /* Read the symmetric tridiagonal matrix T from data file, first
   * the diagonal elements, then the off diagonal elements.
   */
  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_lapackeig_dstegr (f08jlc).
   * Calculate all the eigenvalues of T.
   */
  nag_lapackeig_dstegr(order, job, range, n, d, e, vl, vu, 0, 0, &m, w, z, pdz,
                       isuppz, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dstegr (f08jlc).\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) = Z(i, j) / Z(1, j);

  /* Print eigenvalues and eigenvectors */
  printf(" %s\n", "Eigenvalues");
  for (i = 0; i < m; ++i)
    printf("%8.4f%s", w[i], (i + 1) % 8 == 0 ? "\n" : " ");
  printf("\n\n");

  /* nag_file_print_matrix_real_gen (x04cac).
   * Print eigenvectors.
   */
  fflush(stdout);
  nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
                                 m, z, pdz, "Eigenvectors", 0, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

END:
  NAG_FREE(d);
  NAG_FREE(e);
  NAG_FREE(w);
  NAG_FREE(z);
  NAG_FREE(isuppz);

  return exit_status;
}

#undef Z