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

NAG AD Library Introduction
Example description
/* F08GA_P0W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.1, 2024.
 */

#include <iostream>
#include <dco.hpp>
#include <math.h>
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
#include <string>

int main()
{
  /* Scalars */
  double  eerrbd, eps;
  Integer exit_status = 0, i, j, n;
  /* Arrays */
  double *    ap = 0, *dummy = 0, *w = 0, *work = 0;
  Integer     ifail;
  std::string uplo = "U";

#define AP(I, J) ap[J * (J - 1) / 2 + I - 1]

  printf("F08GA_P0W_F C++ Example Program Results\n\n");

  nag::ad::handle_t ad_handle;

  /* Skip heading in data file */
#ifdef _WIN32
  scanf_s("%*[^\n]");
#else
  scanf("%*[^\n]");
#endif
#ifdef _WIN32
  scanf_s("%" NAG_IFMT "%*[^\n]", &n);
#else
  scanf("%" NAG_IFMT "%*[^\n]", &n);
#endif

  /* Allocate memory */
  ap    = new double[n * (n + 1) / 2];
  dummy = new double[1];
  w     = new double[n];
  work  = new double[3 * n];

  /* Read the upper triangular part of the matrix A from data file */
  double tmp;
  for (i = 1; i <= n; ++i)
  {
    for (j = i; j <= n; ++j)
    {
      AP(i, j) = 0.0;
#ifdef _WIN32
      scanf_s("%lf", &tmp);
#else
      scanf("%lf", &tmp);
#endif
      AP(i, j) = tmp;
    }
  }
#ifdef _WIN32
  scanf_s("%*[^\n]");
#else
  scanf("%*[^\n]");
#endif

  ifail = 0;
  nag::ad::f08ga(ad_handle, "N", "U", n, ap, w, dummy, 1, work, ifail);

  /* 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    = X02AJC;
  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);

  delete[] ap;
  delete[] dummy;
  delete[] w;
  delete[] work;

  return exit_status;
}

#undef AP