/* D01UA_P0W_F C++ Header Example Program.
*
* Copyright 2022 Numerical Algorithms Group.
* Mark 28.7, 2022.
*/
#include <iostream>
#include <math.h>
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
using namespace std;
int main()
{
// Scalars
int exit_status = 0;
Integer n = 64;
// Arrays
double ruser[1];
Integer iuser[1];
ruser[0] = 1.0;
cout << "D01UA_P0W_F C++ Header Example Program Results\n\n";
Integer ifail = 0;
nag::ad::handle_t ad_handle;
double a, b;
Integer key;
for (int funid = 1; funid <= 6; funid++)
{
if (funid == 1)
{
a = 0.0;
b = 1.0;
key = 0;
}
if (funid == 2)
{
a = 2.0;
b = 0.0;
key = -5;
}
if (funid == 3)
{
a = 2.0;
b = 1.0;
key = -3;
}
if (funid == 4)
{
a = 2.0;
b = 1.0;
key = 3;
}
if (funid == 5)
{
a = -1.0;
b = 3.0;
key = -4;
}
if (funid == 6)
{
a = -1.0;
b = 3.0;
key = 4;
}
iuser[0] = funid;
auto f = [&](nag::ad::handle_t & ad_handle,
const double *x,
const Integer & nx,
double *fv,
Integer & iflag)
{
// dco/c++ overloading used here to perform AD
Integer i;
double xx, fx, rux;
for (i = 0; i < nx && iflag >= 0; i++)
{
xx = x[i];
rux = ruser[0] * xx;
if (iuser[0] == 1)
{
fx = 4.0 / (1.0 + ruser[0] * xx * xx);
}
if (iuser[0] == 2)
{
fx = 1.0 / (xx * xx * log(ruser[0] * xx));
}
if (iuser[0] == 3)
{
fx = exp(-ruser[0] * xx) / xx;
}
if (iuser[0] == 4)
{
fx = ruser[0] / xx;
}
if (iuser[0] == 5)
{
fx = exp(-3.0 * ruser[0] * xx * xx - 4.0 * xx - 1.0);
}
if (iuser[0] == 6)
{
fx = exp(2.0 * ruser[0] * xx + 2.0);
}
if (iuser[0] < 0 || iuser[0] > 6)
{
iflag = -1;
}
fv[i] = fx;
}
};
// Call the passive routine
ifail = 0;
double dinest;
nag::ad::d01ua(ad_handle, key, a, b, n, f, dinest, ifail);
cout.setf(ios::scientific, ios::floatfield);
cout.setf(ios::right);
cout.precision(4);
cout << "\n dinest = ";
cout.width(12);
cout << dinest << " ";
}
cout << endl;
return exit_status;
}