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

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

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

int main()
{
  int               exit_status = 0;
  nagad_a1w_w_rtype objf;
  cout << "E04DG_A1W_F C++ Header Example Program Results\n\n";

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

  // Create AD configuration data object
  Integer           ifail = 0;
  nag::ad::handle_t ad_handle;

  // Read problem parameters and register for differentiation
  // Skip first line of data file
  string mystr;
  getline(cin, mystr);

  Integer n;
  cin >> n;

  // AD routine fixed length array arguments
  Integer           iwsav[610];
  nagad_a1w_w_rtype ruser[1], rwsav[475];
  char              cwsav[1];
  logical           lwsav[120];
  const Charlen     name_l = 6, cwsav_l = 1;

  auto objfun = [&](nag::ad::handle_t &     ad_handle,
                  Integer &               mode,
                  const Integer &         n,
                  const nagad_a1w_w_rtype *x,
                  nagad_a1w_w_rtype &     objf,
                  nagad_a1w_w_rtype *objgrd,
                  const Integer &         nstate)
                {
                  // dco/c++ used here to perform AD of objfun
                  nagad_a1w_w_rtype x1, x2, y1, y2, expx1;

                  x1    = x[0];
                  x2    = x[1];
                  expx1 = exp(x1);
                  y1    = 2.0 * x1;
                  y1    = y1 + x2;
                  y2    = x2 + 1.0;
                  x1    = y1 * y1;
                  x2    = y2 * y2;
                  x1    = x1 + x2;
                  objf  = expx1 * x1;

                  if (mode == 2)
                  {
                    y2        = y1 + y2;
                    y2        = 2.0 * y2;
                    y1        = 4.0 * y1;
                    y1        = expx1 * y1;
                    objgrd[0] = y1 + objf;
                    objgrd[1] = expx1 * y2;
                  }
                };

  // AD routine variable length arrays
  Integer *          iwork = 0;
  nagad_a1w_w_rtype *x = 0, *x_in = 0, *objgrd = 0, *work = 0;

  if (!(iwork = NAG_ALLOC(n + 1, Integer)) ||
      !(x = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
      !(x_in = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
      !(objgrd = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
      !(work = NAG_ALLOC(13 * n, nagad_a1w_w_rtype)))
  {
    cout << "Allocation failure\n";
    exit_status = -1;
    goto END;
  }

  double xr;
  for (int i = 0; i < n; i++)
  {
    cin >> xr;
    x_in[i] = xr;
    dco::ga1s<double>::global_tape->register_variable(x_in[i]);
    x[i] = x_in[i];
  }

  // Initialize sav arrays
  ifail = 0;
  nag::ad::e04wb("E04DGA", cwsav, 1, lwsav, 120, iwsav, 610, rwsav, 475, ifail);

  // Options can be set here via E04DJA and/or E04DKA
  // Solve the problem
  Integer iter;
  ifail = 0;
  nag::ad::e04dg(ad_handle, n, objfun, iter, objf, objgrd, x, iwork, work, lwsav, iwsav, rwsav, ifail);

  // Primal results
  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(3);
  cout << "\n Objective value = ";
  cout.width(12);
  cout << dco::value(objf);
  cout << "\n Solution point  = ";
  for (int i = 0; i < n; i++)
  {
    cout.width(12);
    cout << dco::value(x[i]);
  }

  cout << "\n Estim gradient  = ";
  for (int i = 0; i < n; i++)
  {
    cout.width(12);
    cout << dco::value(objgrd[i]);
  }

  cout << "\n\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n\n";
  cout << " Derivatives:\n\n";

  // Setup evaluation of derivatives of objf via adjoints.
  {
    double inc = 1.0;
    dco::derivative(objf) += inc;
  }
  ifail                                              = 0;
  dco::ga1s<double>::global_tape->sparse_interpret() = true;
  dco::ga1s<double>::global_tape->interpret_adjoint();

  // Get derivatives of solution points
  cout << "      dobjf/dx : ";
  for (int i = 0; i < n; i++)
  {
    double d = dco::derivative(x[i]);
    cout.width(12);
    cout << d;
  }
  cout << endl;

  //  Setup evaluation of derivatives via adjoints
  for (int j = 0; j < n; j++)
  {
    dco::ga1s<double>::global_tape->zero_adjoints();
    double inc = 1.0;
    dco::derivative(objgrd[j]) += inc;
    ifail                                              = 0;
    dco::ga1s<double>::global_tape->sparse_interpret() = true;
    dco::ga1s<double>::global_tape->interpret_adjoint();
    cout << " dobjgrd(";
    cout.width(1);
    cout << j + 1;
    cout << ")/dx : ";
    for (int i = 0; i < n; i++)
    {
      double d = dco::derivative(x[i]);
      cout.width(12);
      cout << d;
    }
    cout << endl;
  }

END:

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

  NAG_FREE(iwork);
  NAG_FREE(x);
  NAG_FREE(x_in);
  NAG_FREE(objgrd);
  NAG_FREE(work);

  return exit_status;
}