#include "dco.hpp"
/* E02DE_A1W_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 <nagx04.h>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
// Scalars
int exit_status = 0;
cout << "E02DE_A1W_F C++ Header Example Program Results\n\n";
// Skip first line of data file
string mystr;
getline(cin, mystr);
// Read number of x and y data points
Integer mx, my;
cin >> mx;
cin >> my;
// Allocate arrays for data and interpolant
nagad_a1w_w_rtype *x = 0, *lamda = 0, *y = 0, *mu = 0, *f = 0, *c = 0,
*wrk = 0;
double * dx = 0, *dy = 0, *df = 0;
Integer *iwrk = 0;
Integer lwrk = (my + 6) * (mx + 6);
if (!(x = NAG_ALLOC(mx, nagad_a1w_w_rtype)) ||
!(y = NAG_ALLOC(my, nagad_a1w_w_rtype)) ||
!(lamda = NAG_ALLOC(mx + 4, nagad_a1w_w_rtype)) ||
!(mu = NAG_ALLOC(my + 4, nagad_a1w_w_rtype)) ||
!(f = NAG_ALLOC(my * mx, nagad_a1w_w_rtype)) ||
!(c = NAG_ALLOC(my * mx, nagad_a1w_w_rtype)) ||
!(wrk = NAG_ALLOC(lwrk, nagad_a1w_w_rtype)) ||
!(dx = NAG_ALLOC(mx, double)) || !(dy = NAG_ALLOC(my, double)) ||
!(df = NAG_ALLOC(my * mx, double)) || !(iwrk = NAG_ALLOC(lwrk, Integer)))
{
printf("Allocation failure\n");
exit_status = -2;
}
if (exit_status == 0)
{
// Create AD tape
dco::ga1s<double>::global_tape = dco::ga1s<double>::tape_t::create();
// Create AD configuration data object
Integer ifail = 0;
nag::ad::handle_t ad_handle;
// Read data and register variables
for (int i = 0; i < mx; i++)
{
double xr;
cin >> xr;
x[i] = xr;
dco::ga1s<double>::global_tape->register_variable(x[i]);
}
for (int i = 0; i < my; i++)
{
double yr;
cin >> yr;
y[i] = yr;
dco::ga1s<double>::global_tape->register_variable(y[i]);
}
for (int i = 0; i < my; i++)
{
double fr;
for (int j = 0; j < mx; j++)
{
Integer k = j * my + i;
cin >> fr;
f[k] = fr;
dco::ga1s<double>::global_tape->register_variable(f[k]);
}
}
// Call the AD routine
Integer px, py;
ifail = 0;
nag::ad::e01da(ad_handle, mx, my, x, y, f, px, py, lamda, mu, c, wrk,
ifail);
// Evaluate interpolant and derivatives at an internal point
const Integer m = 1;
nagad_a1w_w_rtype tx[m], ty[m], ff[m];
tx[0] = 1.4;
ty[0] = 0.5;
ifail = 0;
nag::ad::e02de(ad_handle, m, px, py, tx, ty, lamda, mu, c, ff, wrk, iwrk,
ifail);
cout << "\n Interpolant evaluated at x = " << dco::value(tx[0]);
cout << " and y = " << dco::value(ty[0]);
cout.precision(5);
cout << "\n Value of interpolant = ";
cout << dco::value(ff[0]) << endl;
// Setup evaluation of derivatives via adjoints.
double inc = 1.0;
dco::derivative(ff[0]) += inc;
ifail = 0;
dco::ga1s<double>::global_tape->sparse_interpret() = true;
dco::ga1s<double>::global_tape->interpret_adjoint();
cout << "\n Derivatives calculated: First order adjoints\n";
cout << " Computational mode : algorithmic\n";
// Get derivatives
cout << "\n Derivatives of fitted value w.r.t. data points:\n\n";
for (int j = 0; j < mx; j++)
{
dx[j] = dco::derivative(x[j]);
}
for (int j = 0; j < my; j++)
{
dy[j] = dco::derivative(y[j]);
}
for (int j = 0; j < my * mx; j++)
{
df[j] = dco::derivative(f[j]);
}
cout << endl;
NagError fail;
INIT_FAIL(fail);
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, 1, mx, dx, 1,
" d/dx", 0, &fail);
cout << endl;
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, 1, my, dy, 1,
" d/dy", 0, &fail);
cout << endl;
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, my, mx, df, my,
" d/df", 0, &fail);
dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);
}
NAG_FREE(x);
NAG_FREE(y);
NAG_FREE(lamda);
NAG_FREE(mu);
NAG_FREE(f);
NAG_FREE(c);
NAG_FREE(wrk);
NAG_FREE(dx);
NAG_FREE(dy);
NAG_FREE(df);
NAG_FREE(iwrk);
return exit_status;
}