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

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

#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 << "E04WB_P0W_F C++ Header Example Program Results\n\n";

  Integer           ifail = 0;
  nag::ad::handle_t ad_handle;

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

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

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

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

  // Read problem parameters and register for differentiation
  for (int i = 0; i < nclin; i++)
  {
    for (int j = 0; j < sda; j++)
    {
      Integer k = i + j * nclin;
      cin >> a[k];
    }
  }
  for (int i = 0; i < m; i++)
  {
    cin >> y[i];
  }
  for (int i = 0; i < lb; i++)
  {
    cin >> bl[i];
  }
  for (int i = 0; i < lb; i++)
  {
    cin >> bu[i];
  }
  for (int i = 0; i < n; i++)
  {
    cin >> x[i];
  }

  double ruser[44] = {8.0,  8.0,  10.0, 10.0, 10.0, 10.0, 12.0, 12.0, 12.0,
                      12.0, 14.0, 14.0, 14.0, 16.0, 16.0, 16.0, 18.0, 18.0,
                      20.0, 20.0, 20.0, 22.0, 22.0, 22.0, 24.0, 24.0, 24.0,
                      26.0, 26.0, 26.0, 28.0, 28.0, 30.0, 30.0, 30.0, 32.0,
                      32.0, 34.0, 36.0, 36.0, 38.0, 38.0, 40.0, 42.0};
  // Initialize sav arrays
  ifail = 0;
  char cwsav[1];
  nag::ad::e04wb("E04USA", cwsav, 1, lwsav, 120, iwsav, 610, rwsav, 475, ifail);

  auto objfun = [&](nag::ad::handle_t &     ad_handle,
                  Integer &               mode,
                  const Integer &         m,
                  const Integer &         n,
                  const Integer &         ldfj,
                  const Integer &         needfi,
                  const double *x,
                  double *f,
                  double *fjac,
                  const Integer &         nstate)
                {
                  double x1, x2;
                  x1 = x[0];
                  x2 = x[1];

                  if (mode == 0 && needfi > 0)
                  {
                    f[needfi - 1] = x1 + (0.49 - x1) * exp(-x2 * (ruser[needfi - 1] - 8.0));
                  }
                  else
                  {
                    for (int i = 0; i < m; ++i)
                    {
                      double ai   = ruser[i] - 8.0;
                      double temp = exp(-x2 * ai);

                      if (mode == 0 || mode == 2)
                      {
                        f[i] = x1 + (0.49 - x1) * temp;
                      }
                      if (mode == 1 || mode == 2)
                      {
                        fjac[i]     = 1.0 - temp;
                        fjac[i + m] = -(0.49 - x1) * ai * temp;
                      }
                    }
                  }
                };
  auto confun = [&](nag::ad::handle_t &     ad_handle,
                  Integer &               mode,
                  const Integer &         ncnln,
                  const Integer &         n,
                  const Integer &         ldcj,
                  const Integer           needc[],
                  const double *x,
                  double *c,
                  double *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] = -0.09 - x[0] * x[1] + 0.49 * x[1];
                    }
                    if (mode == 1 || mode == 2)
                    {
                      cjac[0]     = -x[1];
                      cjac[ncnln] = -x[0] + 0.49;
                    }
                  }
                };
  // Solve the problem
  Integer           iter;
  double  objf;
  ifail = -1;
  nag::ad::e04us(ad_handle, m, n, nclin, ncnln, lda, ldcj, ldfj, ldr, a, bl, bu,
                 y, confun, objfun, iter, istate, c, cjac, f, fjac, clamda,
                 objf, r, x, iwork, liwork, work, lwork, lwsav, iwsav, rwsav, ifail);

  // Primal results
  cout.setf(ios::scientific, ios::floatfield);
  if (ifail == 0 || ifail > 1)
  {
    cout.precision(4);
    cout << "\n Optimal objective function value = ";
    cout.width(12);
    cout << objf;
    cout << "\n Solution point = ";
    for (int i = 0; i < n; i++)
    {
      cout.width(12);
      cout << x[i];
    }
    cout << endl;
  }
  else
  {
    cout << "nag::ad::e04us failed with ifail = " << ifail << endl;
  }

  delete[] a;
  delete[] bl;
  delete[] bu;
  delete[] y;
  delete[] c;
  delete[] cjac;
  delete[] f;
  delete[] fjac;
  delete[] clamda;
  delete[] r;
  delete[] x;
  delete[] work;
  delete[] rwsav;
  delete[] lwsav;
  delete[] istate;
  delete[] iwork;
  delete[] iwsav;
  return exit_status;
}