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

NAG AD Library Introduction
Example description
/* F08GA_A1W_F C++ Header Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 * Mark 28.3, 2022.
 */

#include <dco.hpp>
#include <iostream>
#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 */
  nagad_a1w_w_rtype *ap = 0, *dummy = 0, *w = 0, *work = 0, *ap_in = 0;
  double *           wr = 0, *dwda = 0;
  Integer            ifail;
  std::string        uplo = "U";

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

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

  // Create AD tape
  dco::ga1s<double>::global_tape = dco::ga1s<double>::tape_t::create();
  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 nagad_a1w_w_rtype[n * (n + 1) / 2];
  ap_in = new nagad_a1w_w_rtype[n * (n + 1) / 2];
  dummy = new nagad_a1w_w_rtype[1];
  w     = new nagad_a1w_w_rtype[n];
  work  = new nagad_a1w_w_rtype[3 * n];
  wr    = new double[n];
  dwda  = new double[n * 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

  for (int i = 0; i < n; i++)
  {
    dco::ga1s<double>::global_tape->register_variable(ap_in[(i * (i + 3)) / 2]);
  }

  for (int i = 0; i < (n * (n + 1)) / 2; i++)
  {
    ap[i] = ap_in[i];
  }

  nag::ad::f08ga(ad_handle, "N", "U", n, ap, w, dummy, 1, work, ifail);
  if (ifail != 0)
  {
    printf("Error from F08GA_A1W_F.\n%" NAG_IFMT " ", ifail);
    exit_status = 1;
    goto END;
  }
  /* Print solution */
  for (i = 0; i < n; ++i)
  {
    wr[i] = dco::value(w[i]);
  }
  printf("Eigenvalues\n");
  for (j = 0; j < n; ++j)
    printf("%8.4f%s", wr[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(wr[0]), fabs(wr[n - 1]));

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

  for (i = 0; i < n; i++)
  {
    dco::ga1s<double>::global_tape->zero_adjoints();
    dco::derivative(w[i]) += 1.0;
    dco::ga1s<double>::global_tape->sparse_interpret() = true;
    dco::ga1s<double>::global_tape->interpret_adjoint();

    for (int j = 0; j < n; j++)
      dwda[i + j * n] = dco::derivative(ap_in[j * (j + 3) / 2]);
  }

  printf("\nDerivatives of eigenvalues w.r.t. diagonals of A\n");
  NagError fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, dwda, n,
         "  dW_i/dA_jj", 0, &fail);

END:
  delete[] ap;
  delete[] ap_in;
  delete[] dummy;
  delete[] w;
  delete[] work;
  delete[] wr;
  delete[] dwda;

  dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);

  return exit_status;
}

#undef AP