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

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

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

int main()
{
  // Scalars
  int exit_status = 0;

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

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

  // Skip first line of data file
  string mystr;
  getline(cin, mystr);

  // Read problem sizes
  Integer n, nclin, ncnln;
  cin >> n;
  cin >> nclin;
  cin >> ncnln;

  Integer liwork = 3 * n + nclin + 2 * ncnln;
  Integer lda = nclin, sda = n, ldcj = ncnln, ldr = n;
  Integer lwork;

  lwork = 20 * n;
  if (nclin > 0 || ncnln > 0)
  {
    lwork = lwork + 2 * n * n;
    if (nclin > 0)
    {
      lwork = lwork + 11 * nclin;
      ;
    }
    if (ncnln > 0)
    {
      lwork = lwork + n * nclin + 2 * n * ncnln + 21 * ncnln;
    }
  }
  if (ncnln > 0)
  {
    lwork = lwork + n * nclin + (2 * n + 21) * ncnln;
  }
  Integer            lb = n + nclin + ncnln;
  nagad_t1w_w_rtype *a = 0, *bl = 0, *bu = 0, *c = 0, *cjac = 0;
  nagad_t1w_w_rtype *objgrd = 0, *clamda = 0, *r = 0, *x = 0, *work = 0,
                    *rwsav = 0;
  Integer *istate = 0, *iwork = 0, *iwsav = 0;
  logical *lwsav = 0;
  double * dr = 0, *x_r = 0;
  a      = new nagad_t1w_w_rtype[lda * sda];
  bl     = new nagad_t1w_w_rtype[lb];
  bu     = new nagad_t1w_w_rtype[lb];
  c      = new nagad_t1w_w_rtype[ncnln];
  cjac   = new nagad_t1w_w_rtype[ncnln * n];
  clamda = new nagad_t1w_w_rtype[lb];
  r      = new nagad_t1w_w_rtype[ldr * n];
  x      = new nagad_t1w_w_rtype[n];
  x_r    = new double[n];
  dr     = new double[n];
  objgrd = new nagad_t1w_w_rtype[n];
  work   = new nagad_t1w_w_rtype[lwork];
  rwsav  = new nagad_t1w_w_rtype[475];
  lwsav  = new logical[120];
  istate = new Integer[lb];
  iwork  = new Integer[liwork];
  iwsav  = new Integer[610];

  // Read problem parameters and register for differentiation
  double yr;
  for (int i = 0; i < nclin; i++)
  {
    for (int j = 0; j < sda; j++)
    {
      Integer k = i + j * nclin;
      cin >> yr;
      a[k] = yr;
    }
  }
  for (int i = 0; i < lb; i++)
  {
    cin >> yr;
    bl[i] = yr;
  }
  for (int i = 0; i < lb; i++)
  {
    cin >> yr;
    bu[i] = yr;
  }
  for (int i = 0; i < n; i++)
  {
    cin >> x_r[i];
  }
  nagad_t1w_w_rtype ruser[3];
  for (int i = 0; i < 3; i++)
  {
    ruser[i] = 1.0;
  }

  double            inc = 1.0, zero = 0.0;
  nagad_t1w_w_rtype objf;
  for (int i = 0; i < 3; ++i)
  {
    dco::derivative(ruser[i]) = inc;
    // Initialize sav arrays
    ifail = 0;
    char cwsav[1];
    nag::ad::e04wb("E04UCA", cwsav, 1, lwsav, 120, iwsav, 610, rwsav, 475,
                   ifail);

    auto objfun = [&](nag::ad::handle_t &     ad_handle,
                  Integer &               mode,
                  const Integer &         n,
                  const nagad_t1w_w_rtype *x,
                  nagad_t1w_w_rtype &     objf,
                  nagad_t1w_w_rtype *objgrd,
                  const Integer &         nstate)
                {
                  if (mode == 0 || mode == 2)
                  {
                    objf = x[0] * x[3] * (ruser[0] * x[0] + ruser[1] * x[1] + ruser[2] * x[2]) +
                          x[2];
                  }
                  if (mode == 1 || mode == 2)
                  {
                    objgrd[0] =
                        x[3] * (2.0 * ruser[0] * x[0] + ruser[1] * x[1] + ruser[2] * x[2]);
                    objgrd[1] = x[0] * x[3] * ruser[1];
                    objgrd[2] = x[0] * x[3] * ruser[2] + 1.0;
                    objgrd[3] = x[0] * (ruser[0] * x[0] + ruser[1] * x[1] + ruser[2] * x[2]);
                  }
                };
  auto confun = [&](nag::ad::handle_t &     ad_handle,
                  Integer &               mode,
                  const Integer &         ncnln,
                  const Integer &         n,
                  const Integer &         ldcj,
                  const Integer           needc[],
                  const nagad_t1w_w_rtype *x,
                  nagad_t1w_w_rtype *c,
                  nagad_t1w_w_rtype *cjac,
                  const Integer &         nstate)
                {
                  if (nstate == 1)
                  {
                    for (int i = 0; i < ncnln * n; ++i)
                    {
                      cjac[i] = 0.0;
                    }
                  }
                  if (needc[0] > 0)
                  {
                    if (mode == 0 || mode == 2)
                    {
                      c[0] = x[0] * x[0] + x[1] * x[1] + x[2] * x[2] + x[3] * x[3];
                    }
                    if (mode == 1 || mode == 2)
                    {
                      cjac[0]         = x[0] + x[0];
                      cjac[ncnln]     = x[1] + x[1];
                      cjac[2 * ncnln] = x[2] + x[2];
                      cjac[3 * ncnln] = x[3] + x[3];
                    }
                  }
                  if (needc[1] > 0)
                  {
                    if (mode == 0 || mode == 2)
                    {
                      c[1] = x[0] * x[1] * x[2] * x[3];
                    }
                    if (mode == 1 || mode == 2)
                    {
                      cjac[1]             = x[1] * x[2] * x[3];
                      cjac[ncnln + 1]     = x[0] * x[2] * x[3];
                      cjac[2 * ncnln + 1] = x[0] * x[1] * x[3];
                      cjac[3 * ncnln + 1] = x[0] * x[1] * x[2];
                    }
                  }
                };
                
    // Solve the problem
    for (int j = 0; j < n; j++)
    {
      x[j] = x_r[j];
    }
    objf = 0.0;
    Integer iter, iuser[1];
    ifail = -1;
    nag::ad::e04uc(ad_handle, n, nclin, ncnln, lda, ldcj, ldr, a, bl, bu,
                   confun, objfun, iter, istate, c, cjac, clamda, objf, objgrd,
                   r, x, iwork, liwork, work, lwork, lwsav, iwsav, rwsav, ifail);
    dr[i]                     = dco::derivative(objf);
    dco::derivative(ruser[i]) = zero;
  }
  // Primal results
  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(4);
  cout << "\n Optimal objective function 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 << endl;

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

  // Get derivatives of objf w.r.t. ruser
  cout << "  derivatives of x[0] w.r.t ruser[0:2]:\n";
  for (int i = 0; i < 3; i++)
  {
    cout.width(12);
    cout << dr[i];
    if (i % 4 == 3)
    {
      cout << endl;
    }
  }
  cout << endl;

  delete[] a;
  delete[] bl;
  delete[] bu;
  delete[] c;
  delete[] cjac;
  delete[] clamda;
  delete[] r;
  delete[] x;
  delete[] x_r;
  delete[] dr;
  delete[] objgrd;
  delete[] work;
  delete[] rwsav;
  delete[] lwsav;
  delete[] istate;
  delete[] iwork;
  delete[] iwsav;
  return exit_status;
}

static void NAG_CALL objfun(nag::ad::handle_t &     ad_handle,
                            Integer &               mode,
                            const Integer &         n,
                            const nagad_t1w_w_rtype x[],
                            nagad_t1w_w_rtype &     objf,
                            nagad_t1w_w_rtype       objgrd[],
                            const Integer &         nstate,
                            Integer                 iuser[],
                            nagad_t1w_w_rtype       ruser[])
{
  if (mode == 0 || mode == 2)
  {
    objf = x[0] * x[3] * (ruser[0] * x[0] + ruser[1] * x[1] + ruser[2] * x[2]) +
           x[2];
  }
  if (mode == 1 || mode == 2)
  {
    objgrd[0] =
        x[3] * (2.0 * ruser[0] * x[0] + ruser[1] * x[1] + ruser[2] * x[2]);
    objgrd[1] = x[0] * x[3] * ruser[1];
    objgrd[2] = x[0] * x[3] * ruser[2] + 1.0;
    objgrd[3] = x[0] * (ruser[0] * x[0] + ruser[1] * x[1] + ruser[2] * x[2]);
  }
  return;
}
static void NAG_CALL confun(nag::ad::handle_t &     ad_handle,
                            Integer &               mode,
                            const Integer &         ncnln,
                            const Integer &         n,
                            const Integer &         ldcj,
                            const Integer           needc[],
                            const nagad_t1w_w_rtype x[],
                            nagad_t1w_w_rtype       c[],
                            nagad_t1w_w_rtype       cjac[],
                            const Integer &         nstate,
                            Integer                 iuser[],
                            nagad_t1w_w_rtype       ruser[])
{
  if (nstate == 1)
  {
    for (int i = 0; i < ncnln * n; ++i)
    {
      cjac[i] = 0.0;
    }
  }
  if (needc[0] > 0)
  {
    if (mode == 0 || mode == 2)
    {
      c[0] = x[0] * x[0] + x[1] * x[1] + x[2] * x[2] + x[3] * x[3];
    }
    if (mode == 1 || mode == 2)
    {
      cjac[0]         = x[0] + x[0];
      cjac[ncnln]     = x[1] + x[1];
      cjac[2 * ncnln] = x[2] + x[2];
      cjac[3 * ncnln] = x[3] + x[3];
    }
  }
  if (needc[1] > 0)
  {
    if (mode == 0 || mode == 2)
    {
      c[1] = x[0] * x[1] * x[2] * x[3];
    }
    if (mode == 1 || mode == 2)
    {
      cjac[1]             = x[1] * x[2] * x[3];
      cjac[ncnln + 1]     = x[0] * x[2] * x[3];
      cjac[2 * ncnln + 1] = x[0] * x[1] * x[3];
      cjac[3 * ncnln + 1] = x[0] * x[1] * x[2];
    }
  }
  return;
}