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

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

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

int main(void) {
  /* Scalars */
  double eerrbd, eps;
  Integer exit_status = 0, i, j, n;
  /* Arrays */
  char nag_enum_arg[40];
  double *ap = 0, *dummy = 0, *w = 0;
  /* Nag Types */
  Nag_OrderType order;
  Nag_UploType uplo;
  NagError fail;

#ifdef NAG_COLUMN_MAJOR
#define AP_UPPER(I, J) ap[J * (J - 1) / 2 + I - 1]
#define AP_LOWER(I, J) ap[(2 * n - J) * (J - 1) / 2 + I - 1]
  order = Nag_ColMajor;
#else
#define AP_LOWER(I, J) ap[I * (I - 1) / 2 + J - 1]
#define AP_UPPER(I, J) ap[(2 * n - I) * (I - 1) / 2 + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_dspev (f08gac) Example Program Results\n\n");

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

  /* 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 (!(ap = NAG_ALLOC(n * (n + 1) / 2, double)) ||
      !(dummy = NAG_ALLOC(1, double)) || !(w = NAG_ALLOC(n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the upper or lower triangular part of the matrix A from data file */
  if (uplo == Nag_Upper) {
    for (i = 1; i <= n; ++i)
      for (j = i; j <= n; ++j)
        scanf("%lf", &AP_UPPER(i, j));
    scanf("%*[^\n]");
  } else if (uplo == Nag_Lower) {
    for (i = 1; i <= n; ++i)
      for (j = 1; j <= i; ++j)
        scanf("%lf", &AP_LOWER(i, j));
    scanf("%*[^\n]");
  }

  /* nag_lapackeig_dspev (f08gac).
   * Solve the symmetric eigenvalue problem.
   */
  nag_lapackeig_dspev(order, Nag_EigVals, uplo, n, ap, w, dummy, 1, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_dspev (f08gac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

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

  /* Get the machine precision, eps, using nag_machine_precision (X02AJC)
   * and compute the approximate error bound for the computed eigenvalues.
   * Note that for the 2-norm, ||A|| = max {|w[i]|, i=0..n-1}, and since
   * the eigenvalues are in ascending order ||A|| = max( |w[0]|, |w[n-1]|).
   */
  eps = nag_machine_precision;
  eerrbd = eps * MAX(fabs(w[0]), fabs(w[n - 1]));

  /* Print the approximate error bound for the eigenvalues */
  printf("\nError estimate for the eigenvalues\n");
  printf("%11.1e\n", eerrbd);

END:
  NAG_FREE(ap);
  NAG_FREE(dummy);
  NAG_FREE(w);

  return exit_status;
}

#undef AP_UPPER
#undef AP_LOWER