/* nag_dsterf (f08jfc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

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

int main(void)
{
  /* Scalars */
  Integer i, n, d_len, e_len;
  Integer exit_status = 0;
  NagError fail;
  /* Arrays */
  double *d = 0, *e = 0;

  INIT_FAIL(fail);

  printf("nag_dsterf (f08jfc) Example Program Results\n\n");

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

  /* Allocate memory */
  if (!(d = NAG_ALLOC(d_len, double)) || !(e = NAG_ALLOC(e_len, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Read T from data file */
  for (i = 0; i < d_len; ++i)
    scanf("%lf", &d[i]);
  for (i = 0; i < e_len; ++i)
    scanf("%lf", &e[i]);
  /* Calculate all the eigenvalues of T */
  /* nag_dsterf (f08jfc).
   * All eigenvalues of real symmetric tridiagonal matrix,
   * root-free variant of QL or QR
   */
  nag_dsterf(n, d, e, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_dsterf (f08jfc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  /* Print eigenvalues */
  printf(" Eigenvalues\n");
  for (i = 0; i < n; ++i)
    printf("  %7.4f", d[i]);
  printf("\n");
END:
  NAG_FREE(d);
  NAG_FREE(e);
  return exit_status;
}