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

NAG AD Library Introduction
Example description
/* C05QS_A1W_F C++ Header Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 * Mark 27.2, 2021.
 */

#include <dco.hpp>
#include <iostream>
#include <math.h>
#include <nag.h>
#include <nagad.h>
#include <nagx02.h>
#include <nagx04.h>
#include <stdio.h>
#include <string>
using namespace std;

extern "C"
{
  static void NAG_CALL fcn(void *&                 ad_handle,
                           const Integer &         n,
                           const Integer &         lindf,
                           const Integer           indf[],
                           const nagad_a1w_w_rtype x[],
                           nagad_a1w_w_rtype       fvec[],
                           Integer                 iuser[],
                           nagad_a1w_w_rtype       ruser[],
                           Integer &               iflag);
}

int main(void)
{
  // Scalars
  int           exit_status = 0;
  const Integer n = 7, lrcomm = 12 + 3 * n, licomm = 8 * n + 19 + 3 * n;

  cout << "C05QS_A1W_F C++ Header Example Program Results\n\n";

  // problem parameters and starting value
  nagad_a1w_w_rtype ruser[5], x[7];

  ruser[0] = -1.0;
  ruser[1] = 3.0;
  ruser[2] = -2.0;
  ruser[3] = -2.0;
  ruser[4] = -1.0;

  for (int i = 0; i < n; ++i)
    {
      x[i] = -1.0;
    }

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

  // Create AD configuration data object
  Integer ifail     = 0;
  void *  ad_handle = 0;
  nag::ad::x10aa(ad_handle, ifail);

  // Register variables to differentiate w.r.t.
  for (int i = 0; i < 5; ++i)
    {
      dco::ga1s<double>::global_tape->register_variable(ruser[i]);
    }

  // Call AD routine
  nagad_a1w_w_rtype fvec[n], rcomm[lrcomm], xtol;
  Integer           icomm[licomm], iuser[1];
  logical           init = Nag_TRUE;

  xtol = sqrt(X02AJC);

  ifail = 0;
  nag::ad::c05qs(ad_handle, fcn, n, x, fvec, xtol, init, rcomm, lrcomm, icomm,
                 licomm, -1, iuser, -1, ruser, ifail);

  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(4);
  cout << "           Solution:\n";
  for (int i = 0; i < n; ++i)
    {
      cout.width(10);
      cout << i + 1;
      cout.width(20);
      cout << dco::value(x[i]) << endl;
    }

  cout << "\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives are of solution w.r.t function params\n\n";

  // Setup evaluation of derivatives via adjoints
  double dr[5 * n];
  for (int i = 0; i < n; ++i)
    {

      dco::ga1s<double>::global_tape->zero_adjoints();
      double inc            = 1.0;
      dco::derivative(x[i]) = inc;

      ifail                                              = 0;
      dco::ga1s<double>::global_tape->sparse_interpret() = true;
      dco::ga1s<double>::global_tape->interpret_adjoint();

      for (int j = 0; j < 5; ++j)
        {
          int k = j * n + i;
          dr[k] = dco::derivative(ruser[j]);
        }
    }

  NagError fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, 5, dr, n,
         "    dx/druser", 0, &fail);

  // Remove computational data object and tape
  nag::ad::x10ab(ad_handle, ifail);
  dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);

  return exit_status;
}

static void NAG_CALL fcn(void *&                 ad_handle,
                         const Integer &         n,
                         const Integer &         lindf,
                         const Integer           indf[],
                         const nagad_a1w_w_rtype x[],
                         nagad_a1w_w_rtype       fvec[],
                         Integer                 iuser[],
                         nagad_a1w_w_rtype       ruser[],
                         Integer &               iflag)
{
  for (int ind = 0; ind < lindf; ++ind)
    {
      int i   = indf[ind] - 1;
      fvec[i] = (ruser[1] + ruser[2] * x[i]) * x[i] - ruser[4];
      if (i > 0)
        fvec[i] = fvec[i] + ruser[0] * x[i - 1];
      if (i < n - 1)
        fvec[i] = fvec[i] + ruser[3] * x[i + 1];
    }
  iflag = 0;
  return;
}