Example description
/* E04NE_T1W_F C++ Header Example Program.
 *
 * Copyright 2020 Numerical Algorithms Group.
 * Mark 27.1, 2020.
 */

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

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

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

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

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

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

  Integer liwork = n;
  Integer lda = m, sda = n, ldc = nclin, lb = n + nclin;
  Integer lwork;

  lwork = 9*n;
  if (nclin>0) {
    lwork = lwork + 2*n*n + 6*nclin;
  }
  nagad_t1w_w_rtype *a_in=0, *a=0, *b_in=0, *b=0, *bl=0, *bu=0, *c=0;
  nagad_t1w_w_rtype *cvec=0, *clamda=0, *work=0, *x=0, *x_in=0, *rwsav=0;
  Integer           *istate=0, *iwork=0, *iwsav=0, *kx;
  logical           *lwsav=0;
  double            *db=0, *dbl=0, *dbu=0;
  a      = new nagad_t1w_w_rtype [lda*sda];
  a_in   = new nagad_t1w_w_rtype [lda*sda];
  b      = new nagad_t1w_w_rtype [m];
  b_in   = new nagad_t1w_w_rtype [m];
  bl     = new nagad_t1w_w_rtype [lb];
  bu     = new nagad_t1w_w_rtype [lb];
  c      = new nagad_t1w_w_rtype [nclin*n];
  clamda = new nagad_t1w_w_rtype [lb];
  x      = new nagad_t1w_w_rtype [n];
  x_in   = new nagad_t1w_w_rtype [n];
  kx     = new Integer [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];
  db     = new double            [m];
  dbl    = new double            [lb];
  dbu    = new double            [lb];

  // Read problem parameters and register for differentiation
  double            yr;
  for (int i=0; i<m; i++) {
    for (int j=0; j<n; j++) {
      Integer k = i + j*m;
      cin >> yr;
      a_in[k] = yr;
    }
  }
  for (int i=0; i<m; i++) {
    cin >> yr;
    b_in[i] = yr;
  }
  for (int i=0; i<nclin; i++) {
    for (int j=0; j<n; j++) {
      Integer k = i + j*nclin;
      cin >> yr;
      c[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 >> yr;
    x_in[i] = yr;
  }

  double            inc = 1.0, zero = 0.0;
  nagad_t1w_w_rtype obj;
  Integer           k;
  for (int i=0; i<m+2*lb; i++) {
    if (i<m) {
      k = i;
      nagad_t1w_inc_derivative(&b_in[k],inc);
    } else if (i<m+lb) {
      k = i - m;
      nagad_t1w_inc_derivative(&bl[k],inc);
    } else {
      k = i - m - lb;
      nagad_t1w_inc_derivative(&bu[k],inc);
    }
    // Initialize sav arrays
    ifail = 0;
    char cwsav[1];
    obj = 0.0;
    e04wb_t1w_f_("E04NCA",cwsav,1,lwsav,120,iwsav,610,rwsav,475,ifail,6,1);
    // Set option via string
    e04ne_t1w_f_("Print Level = -1",lwsav,iwsav,rwsav,16);
 
    // Solve the problem
    for (int j=0; j<m; j++) {
      b[j] = b_in[j];
      for (int l=0; l<n; l++) {
        a[j+l*m] = a_in[j+l*m];
      }
    }
    for (int j=0; j<n; j++) {
      x[j] = x_in[j];
    }
    for (int j=0; j<lb; j++) {
      istate[j] = 0.0;
    }
    Integer           iter;
    ifail = -1;
    e04nc_t1w_f_(ad_handle,m,n,nclin,ldc,lda,c,bl,bu,cvec,istate,kx,x,a,
                 b,iter,obj,clamda,iwork,liwork,work,lwork,lwsav,iwsav,rwsav,
                 ifail);
    if (i<m) {
      nagad_t1w_set_derivative(&b_in[k],zero);
      db[k] = nagad_t1w_get_derivative(obj);
    } else if (i<m+lb) {
      nagad_t1w_set_derivative(&bl[k],zero);
      dbl[k] = nagad_t1w_get_derivative(obj);
    } else {
      nagad_t1w_set_derivative(&bu[k],zero);
      dbu[k] = nagad_t1w_get_derivative(obj);
    }
  }
  
  // Primal results
  cout.setf(ios::scientific,ios::floatfield);
  cout.precision(4);
  cout << "\n Optimal objective function value = ";
  cout.width(12); cout << nagad_t1w_get_value(obj) << endl;
  cout << "\n Solution point: " << endl;
  for (int i=0; i<n; i++) {
    cout.width(8); cout << i;
    cout.width(12); cout << nagad_t1w_get_value(x[i]) << endl;
  }

  cout << "\n Derivatives calculated: First order tangents\n";
  cout << " Computational mode    : algorithmic\n\n";
  cout << " Derivatives:\n\n";
    
  cout << "\nDerivatives of obj w.r.t. B, BL and BU\n\n";
  NagError  fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,1,m,db,1,
         "  dobj/dB",0,&fail);
  cout << endl;
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,1,lb,dbl,1,
         "  dobj/dBL",0,&fail);
  cout << endl;
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,1,lb,dbu,1,
         "  dobj/dBU",0,&fail);

  // Remove computational data object
  x10ab_t1w_f_(ad_handle,ifail);

  delete [] a;
  delete [] a_in;
  delete [] b;
  delete [] b_in;
  delete [] bl;
  delete [] bu;
  delete [] c;
  delete [] clamda;
  delete [] x;
  delete [] x_in;
  delete [] kx;
  delete [] work;
  delete [] rwsav;
  delete [] lwsav;
  delete [] istate;
  delete [] iwork;
  delete [] iwsav;
  delete [] db;
  delete [] dbl;
  delete [] dbu;
  return exit_status;
}