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

NAG AD Library Introduction
Example description
/* F11XE_A1W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.2, 2024.
 */
#include <dco.hpp>
#include <iostream>
#include <nag.h>
#include <nagad.h>
#include <nagx04.h>
#include <stdio.h>
using namespace std;

int main()
{
  int               exit_status = 0;
  nag::ad::handle_t ad_handle;
  Integer           ifail = 0;

  cout << "F11XE_A1W_F C++ Header Example Program Results\n\n";
  // Skip heading in data file
  string mystr;
  getline(cin, mystr);

  // Read order of matrix and number of nonzero entries
  Integer n, nnz;
  cin >> n;
  cin >> nnz;

  nagad_a1w_w_rtype *a = 0, *x = 0, *y = 0;
  double *           ar = 0, *yr = 0, *dydx = 0;
  Integer *          icol = 0, *irow = 0;

  a    = new nagad_a1w_w_rtype[nnz];
  x    = new nagad_a1w_w_rtype[n];
  y    = new nagad_a1w_w_rtype[n];
  icol = new Integer[nnz];
  irow = new Integer[nnz];
  ar   = new double[nnz];
  yr   = new double[n];
  dydx = new double[n * n];

  // Read the matrix A
  for (int i = 0; i < nnz; i++)
  {
    cin >> ar[i] >> irow[i] >> icol[i];
    a[i] = ar[i];
  }

  // Create AD tape
  dco::ga1s<double>::global_tape = dco::ga1s<double>::tape_t::create();

  // Read the vector x
  for (int i = 0; i < n; i++)
  {
    cin >> yr[i];
    x[i] = yr[i];
    dco::ga1s<double>::global_tape->register_variable(x[i]);
  }

  // Create AD configuration data object
  ifail = 0;

  // Calculate matrix-vector product
  ifail = 0;
  nag::ad::f11xe(ad_handle, n, nnz, a, irow, icol, "C", x, y, ifail);

  // Output results
  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(4);
  cout << "  Matrix-vector product" << endl;
  for (int i = 0; i < n; ++i)
  {
    cout.width(12);
    cout << dco::value(y[i]) << endl;
  }

  cout << "\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives of Y w.r.t X (A):\n";

  // Setup evaluation of derivatives via adjoints
  for (int i = 0; i < n; i++)
  {
    // Reset adjoints, initialize derivative, and evaluate adjoint
    dco::ga1s<double>::global_tape->zero_adjoints();
    double inc = 1.0;
    dco::derivative(y[i]) += inc;
    ifail                                              = 0;
    dco::ga1s<double>::global_tape->sparse_interpret() = true;
    dco::ga1s<double>::global_tape->interpret_adjoint();

    for (int j = 0; j < n; j++)
    {
      Integer k = i + j * n;
      dydx[k]   = dco::derivative(x[j]);
    }
  }
  // Print derivatives
  cout << endl;
  NagError fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, dydx, n,
         "       dy_i/dx_j", 0, &fail);

  ifail = 0;

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

  delete[] a;
  delete[] x;
  delete[] y;
  delete[] icol;
  delete[] irow;
  delete[] ar;
  delete[] yr;
  delete[] dydx;

  return exit_status;
}