Example description
/* C05RB_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 <nag_stdlib.h>
#include <nagx02.h>
#include <nagx04.h>
#include <iostream>
#include <string>
using namespace std;

extern "C"
{
  static void (NAG_CALL fcn)(void* &ad_handle,
                             const Integer &n,
                             const nagad_a1w_w_rtype x[],
                             nagad_a1w_w_rtype fvec[],
                             nagad_a1w_w_rtype fjac[],
                             Integer iuser[],
                             nagad_a1w_w_rtype ruser[],
                             Integer &iflag);
}

int main (void)
{
  // Scalars
  int               exit_status = 0;
  const Integer     n = 7;      

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

  // problem parameters and starting value
  nagad_a1w_w_rtype r[5], x[7];

  r[0] = -1.0;
  r[1] =  3.0;
  r[2] = -2.0;
  r[3] = -2.0;
  r[4] = -1.0;

  for (int i=0; i<n; ++i) {
    x[i] = -1.0;
  }

  // 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);

  // Register variables to differentiate w.r.t.
  for (int i=0; i<5; ++i) {
    nagad_a1w_ir_register_variable(&r[i]);
  }

  // Call AD routine
  nagad_a1w_w_rtype fvec[n], fjac[n*n], xtol;
  Integer           iuser[1];

  xtol = sqrt(X02AJC);
  ifail = 0;
  c05rb_a1w_f_(ad_handle,fcn,n,x,fvec,fjac,xtol,iuser,r,ifail);

  cout.setf(ios::scientific,ios::floatfield);
  cout.precision(4);
  cout << "           Solution:\n";
  for (int i=0; i<n; ++i) {
    cout.width(10);
    cout << i+1;
    cout.width(20);
    cout << nagad_a1w_get_value(x[i]) << endl;
  }

  cout << "\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives are of solution w.r.t function params\n\n";

  // Setup evaluation of derivatives via adjoints
  double dr[5*n];
  for (int i = 0; i < n; ++i) {

    nagad_a1w_ir_zero_adjoints();
    double inc = 1.0;
    nagad_a1w_set_derivative(&x[i],inc);
  
    ifail = 0;
    nagad_a1w_ir_interpret_adjoint(ifail);

    for (int j=0; j<5; ++j) {
      int k = j*n + i;
      dr[k] = nagad_a1w_get_derivative(r[j]);
    }
  }

  NagError  fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,5,dr,n,
         "    dx/druser",0,&fail);

  // Remove computational data object and tape
  x10ab_a1w_f_(ad_handle,ifail);
  nagad_a1w_ir_remove();

  return exit_status;
}

static void (NAG_CALL fcn)(void* &ad_handle,
                           const Integer &n,
                           const nagad_a1w_w_rtype x[],
                           nagad_a1w_w_rtype fvec[],
                           nagad_a1w_w_rtype fjac[],
                           Integer iuser[],
                           nagad_a1w_w_rtype ruser[],
                           Integer &iflag)
{
  if (iflag!=2) {
    for (int i=0; i<n; ++i) {
      fvec[i] = (ruser[1] + ruser[2]*x[i])*x[i] - ruser[4];
    }
    for (int i=1; i<n; ++i) {
      fvec[i] = fvec[i] + ruser[0]*x[i-1];
    }
    for (int i=0; i<n-1; ++i) {
      fvec[i] = fvec[i] + ruser[3]*x[i+1];
    }
  } else {
    for (int i=0; i<n*n; ++i) {
      fjac[i] = 0.0;
    }
    fjac[0] = ruser[1] + 2.0*ruser[2]*x[0];
    fjac[n] = ruser[3];
    for (int i=1; i < n-1; ++i) {
      int k = i*n + i;
      fjac[k-n] = ruser[0];
      fjac[k] = ruser[1] + 2.0*ruser[2]*x[i];
      fjac[k+n] = ruser[3];
    }
    fjac[n*n-n-1] = ruser[0];
    fjac[n*n-1] = ruser[1] + 2.0*ruser[2]*x[n-1];
  }
  iflag = 0;
  return;
}