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

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

extern "C"
{
Integer iuser[],
static void (NAG_CALL cb_sym)(int callmode, void* cb_handle);

}

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

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

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

double            ar, br, epsr, etar, ruserr;
cin >> ar;
cin >> br;
cin >> epsr;
cin >> etar;
cin >> ruserr;

// Create AD configuration data object
Integer ifail = 0;

Integer mode;
cin >> mode;

// Set this mode.
ifail = 0;

// Register variables to differentiate w.r.t.
nagad_a1w_w_rtype a, b, eps, eta, ruser[1];
a = ar;
b = br;
eps = epsr;
eta = etar;
ruser[0] = ruserr;

Integer           iuser[1];
iuser[0] = 0;
// if mode is symbolic then set iuser[0] = 1 use symbolic adjoint callback

iuser[0] = 1;
}

ifail = 0;

// Setup evaluation of derivatives via adjoints
double inc = 1.0;

ifail = 0;

cout << "\n Derivatives calculated: First order adjoints\n";
cout << " Computational mode    : algorithmic\n";
} else {
cout << " Computational mode    : symbolic\n";
}

// Get derivatives

cout.setf(ios::scientific,ios::floatfield);
cout.precision(5);

cout << " Solution, x = " << nagad_a1w_get_value(x) << endl;
cout << "\n Derivatives:\n";
cout << "     d/da(x)     = " << da << endl;
cout << "     d/db(x)     = " << db << endl;
cout << "     d/druser(x) = " << druser << endl;

// Remove computational data object and tape

return exit_status;
}

Integer iuser[],
{
Integer ifail = 0, mode;
// Get computational mode
// Evaluate nonlinear function (this needs dco/c++)
z = exp(-x) - x*ruser[0];
} else {
Integer cb_mode;
ifail = 0;
// Evaluate primal only
double zr = exp(-xr) - xr*rr;
z = zr;
} else {
// Perform differentials symbolically using companion callback

// Create callback data object
void    *cb_handle;
ifail = 0;
x10ba_a1w_f_(cb_handle,ifail);
// Write cb_mode to object
x10be_a1w_f_(cb_handle,cb_mode,ifail);
// Write inputs to object
x10bj_a1w_f_(cb_handle,x,ifail);
x10bj_a1w_f_(cb_handle,ruser[0],ifail);

z = zr;
// Register z
// Write output to object
ifail = 0;
x10bj_a1w_f_(cb_handle,z,ifail);

// Insert callback
void *cb_loc = (void *) cb_sym;
x10bb_a1w_f_(cb_handle,cb_loc,ifail);
}
}
return;
}

static void (NAG_CALL cb_sym)(int callmode, void* cb_handle)
{
// Extract data from callback data object

if (callmode == FREE_CHECKPOINT) return;
Integer ifail, cb_mode;
ifail = 0;
x10ce_a1w_f_(cb_handle,cb_mode,ifail);
x10cj_a1w_f_(cb_handle,x,ifail);
x10cj_a1w_f_(cb_handle,ruser,ifail);
x10cj_a1w_f_(cb_handle,z,ifail);

// d/dx