/* nag_dstevx (f08jbc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf08.h>
#include <nagx02.h>
#include <nagx04.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_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_real_safe_small_number (X02AMC).
   * Set the absolute error tolerance for eigenvalues. With abstol
   * set to zero, the default value would be used instead.
   */
  abstol = nag_real_safe_small_number * 2;

  /* nag_dstevx (f08jbc).
   * Solve the symmetric eigenvalue problem.
   */
  nag_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_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_gen_real_mat_print (x04cac).
   * Print selected eigenvectors.
   */
  INIT_FAIL(fail_print);
  fflush(stdout);
  nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m, z,
                         pdz, "Selected eigenvectors", 0, &fail_print);
  if (fail_print.code != NE_NOERROR) {
    printf("Error from nag_gen_real_mat_print (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;
}