Example description
/* E04US_A1W_F C++ Header Example Program.
 *
 * Copyright 2019 Numerical Algorithms Group.
 * Mark 27, 2019.
 */

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

extern "C"
{
  static void NAG_CALL objfun(void* &ad_handle,
                              Integer &mode,
                              const Integer &m,
                              const Integer &n,
                              const Integer &ldfj,
                              const Integer &needfi,
                              const nagad_a1w_w_rtype x[],
                              nagad_a1w_w_rtype f[],
                              nagad_a1w_w_rtype fjac[],
                              const Integer &nstate,
                              Integer iuser[],
                              nagad_a1w_w_rtype ruser[]);
  static void NAG_CALL confun(void* &ad_handle,
                              Integer &mode,
                              const Integer &ncnln,
                              const Integer &n,
                              const Integer &ldcj,
                              const Integer needc[],
                              const nagad_a1w_w_rtype x[],
                              nagad_a1w_w_rtype c[],
                              nagad_a1w_w_rtype cjac[],
                              const Integer &nstate,
                              Integer iuser[],
                              nagad_a1w_w_rtype ruser[]);
}

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

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

  // Create AD tape
  nagad_a1w_ir_create();
  
  // Create AD configuration data object
  Integer ifail = 0;
  void    *ad_handle = 0;
  x10aa_a1w_f_(ad_handle,ifail);

  // 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;
  nagad_a1w_w_rtype *a=0,    *bl=0,     *bu=0, *y=0, *c=0, *cjac=0, *f=0;
  nagad_a1w_w_rtype *fjac=0, *clamda=0, *r=0,  *x=0, *work=0, *rwsav=0;
  Integer           *istate=0, *iwork=0, *iwsav=0;
  logical           *lwsav=0;
  double            *dr=0, *rwsav_r=0;
  if (!(a      = NAG_ALLOC(lda*sda, nagad_a1w_w_rtype)) ||
      !(bl     = NAG_ALLOC(lb,      nagad_a1w_w_rtype)) ||
      !(bu     = NAG_ALLOC(lb,      nagad_a1w_w_rtype)) ||
      !(y      = NAG_ALLOC(m,       nagad_a1w_w_rtype)) ||
      !(c      = NAG_ALLOC(ncnln,   nagad_a1w_w_rtype)) ||
      !(cjac   = NAG_ALLOC(ncnln*n, nagad_a1w_w_rtype)) ||
      !(f      = NAG_ALLOC(m,       nagad_a1w_w_rtype)) ||
      !(fjac   = NAG_ALLOC(m*n,     nagad_a1w_w_rtype)) ||
      !(clamda = NAG_ALLOC(lb,      nagad_a1w_w_rtype)) ||
      !(r      = NAG_ALLOC(ldr*n,   nagad_a1w_w_rtype)) ||
      !(x      = NAG_ALLOC(n,       nagad_a1w_w_rtype)) ||
      !(work   = NAG_ALLOC(lwork,   nagad_a1w_w_rtype)) ||
      !(rwsav  = NAG_ALLOC(475,     nagad_a1w_w_rtype)) ||
      !(lwsav  = NAG_ALLOC(120,     logical)) ||
      !(rwsav_r = NAG_ALLOC(475,    double)) ||
      !(dr     = NAG_ALLOC(n*n,     double)) ||
      !(istate = NAG_ALLOC(lb,      Integer)) ||
      !(iwork  = NAG_ALLOC(liwork,  Integer)) ||
      !(iwsav  = NAG_ALLOC(610,  Integer)))
    {
      cout << "Allocation failure\n";
      exit_status = -1;
    } else {
  
    // 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<m; i++) {
      cin >> yr;
      y[i] = 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 >> yr;
      x[i] = yr;
    }

    double rr[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};
    nagad_a1w_w_rtype ruser[44];
    for (int i=0; i<44; i++) {
      ruser[i] = rr[i];
      nagad_a1w_ir_register_variable(&ruser[i]);
    }
    
    // Initialize sav arrays
    ifail = 0;
    char cwsav[1];
    e04wb_a1w_f_("E04USA",cwsav,1,lwsav,120,iwsav,610,rwsav,475,ifail,6,1);
    
    // Solve the problem
    Integer           iter, iuser[1];
    nagad_a1w_w_rtype objf;
    ifail = -1;
    e04us_a1w_f_(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,iuser,ruser,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 << nagad_a1w_get_value(objf);
      cout << "\n Solution point = ";
      for (int i=0; i<n; i++) {
        cout.width(12); cout << nagad_a1w_get_value(x[i]);
      }
      cout << endl;
    } else {
      cout << "e04us_a1w_f_ failed with ifail = " << ifail << endl;
      goto END;
    }
  
    cout << "\n Derivatives calculated: First order adjoints\n";
    cout << " Computational mode    : algorithmic\n\n";
    cout << " Derivatives:\n\n";
    
    // Setup evaluation of derivatives of fsumsq via adjoints.
    double inc = 1.0;
    nagad_a1w_inc_derivative(&x[0],inc);
    ifail = 0;
    nagad_a1w_ir_interpret_adjoint_sparse(ifail);

    // Get derivatives of x[0] w.r.t. ruser
    cout << "  derivatives of x[0] w.r.t ruser[0:43]:\n";
    for (int i=0; i<44; i++) {
      double d = nagad_a1w_get_derivative(ruser[i]);
      cout.width(12); cout << d;
      if (i%4==3) {
        cout << endl;
      }
    }
  }
 END:
  // Remove computational data object and tape
  x10ab_a1w_f_(ad_handle,ifail);
  nagad_a1w_ir_remove();

  return exit_status;
}

static void NAG_CALL objfun(void* &ad_handle,
                            Integer &mode,
                            const Integer &m,
                            const Integer &n,
                            const Integer &ldfj,
                            const Integer &needfi,
                            const nagad_a1w_w_rtype x[],
                            nagad_a1w_w_rtype f[],
                            nagad_a1w_w_rtype fjac[],
                            const Integer &nstate,
                            Integer iuser[],
                            nagad_a1w_w_rtype ruser[])
{
  nagad_a1w_w_rtype 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) {
      nagad_a1w_w_rtype ai   = ruser[i] - 8.0;
      nagad_a1w_w_rtype 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;
      }
    }
  }
  return;
}
static void NAG_CALL confun(void* &ad_handle,
                            Integer &mode,
                            const Integer &ncnln,
                            const Integer &n,
                            const Integer &ldcj,
                            const Integer needc[],
                            const nagad_a1w_w_rtype x[],
                            nagad_a1w_w_rtype c[],
                            nagad_a1w_w_rtype cjac[],
                            const Integer &nstate,
                            Integer iuser[],
                            nagad_a1w_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] = -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;
    }
  }
  return;
}