/* D01RL_T1W_F C++ Header Example Program.
*
* Copyright 2023 Numerical Algorithms Group.
* Mark 29.0, 2023.
*/
#include <dco.hpp>
#include <iostream>
#include <math.h>
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
using namespace std;
int main()
{
// Scalars
int exit_status = 0;
cout << "D01RL_T1W_F C++ Header Example Program Results\n\n";
nagad_t1w_w_rtype a, b, epsabs, epsrel;
a = 0.0;
b = 1.0;
epsabs = 0.0;
epsrel = 1.0e-4;
Integer npts = 1;
Integer maxsub = 20;
Integer lrinfo = 80 + npts + 6;
Integer liinfo = 40 + npts + 4;
nagad_t1w_w_rtype *rinfo = 0;
Integer * iinfo = 0;
rinfo = new nagad_t1w_w_rtype[lrinfo];
iinfo = new Integer[liinfo];
// Create AD configuration data object
Integer ifail = 0;
nag::ad::handle_t ad_handle;
double inc = 1.0, zero = 0.0;
nagad_t1w_w_rtype result, abserr, ruser[20], points[1];
Integer iuser[1];
points[0] = 1.0 / 7.0;
iuser[0] = 0;
for (int i = 0; i < 20; ++i)
{
ruser[i] = 0.0;
}
auto f = [&](nag::ad::handle_t & ad_handle,
const nagad_t1w_w_rtype *x,
const Integer & nx,
nagad_t1w_w_rtype *fv,
Integer & iflag)
{
// dco/c++ used here to perform AD of the following
iflag = 0;
for (int i = 0; i < nx; i++)
{
fv[i] = x[i] - 1.0 / 7.0;
if (fv[i] == 0.0)
{
ruser[iflag] = x[i];
iflag++;
}
else if (fv[i] < 0.0)
{
fv[i] = -fv[i];
}
}
iuser[0] = iflag;
if (iflag == 0)
{
for (int i = 0; i < nx; i++)
{
fv[i] = 1.0 / sqrt(fv[i]);
}
}
else
{
iflag = -iflag;
}
};
// Call the AD routine incrementing each active input in turn
dco::derivative(a) = inc;
ifail = -1;
nag::ad::d01rl(ad_handle, f, a, b, npts, points, epsabs, epsrel, maxsub,
result, abserr, rinfo, iinfo, ifail);
dco::derivative(a) = zero;
if (ifail < 0)
{
cout << "\n ** nag::ad::d01rl failed error exit ifail = " << ifail << endl;
goto END;
}
double da;
da = dco::derivative(result);
dco::derivative(b) = inc;
ifail = -1;
nag::ad::d01rl(ad_handle, f, a, b, npts, points, epsabs, epsrel, maxsub,
result, abserr, rinfo, iinfo, ifail);
double db;
db = dco::derivative(result);
// Print inputs and primal outputs.
cout << "\n lower limit of integration (a) = " << dco::value(a) << endl;
cout << " upper limit of integration (b) = " << dco::value(b) << endl;
cout << " given break point (points[0]) = " << dco::value(points[0]) << endl;
cout << " absolute accuracy requested = " << dco::value(epsabs) << endl;
cout << " relative accuracy requested = " << dco::value(epsrel) << endl;
cout << " maximum number of subintervals = " << maxsub << endl;
cout.setf(ios::scientific, ios::floatfield);
cout.precision(4);
if (ifail >= 0)
{
cout << "\n approximation to the integral : " << dco::value(result)
<< endl;
cout << " estimate of the absolute error : " << dco::value(abserr) << endl;
cout << " number of function evaluations : " << iinfo[0] << endl;
}
cout << "\n Derivatives calculated: First order tangents\n";
cout << " Computational mode : algorithmic\n";
// Output derivatives
cout << "\n Derivative of solution w.r.t end points:\n";
cout << " dI/da = " << da << endl;
cout << " dI/db = " << db << endl;
END:
delete[] rinfo;
delete[] iinfo;
return exit_status;
}