/* F07CA_A1W_F C++ Header Example Program.
*
* Copyright 2017 Numerical Algorithms Group.
* Mark 26.2, 2017.
*/
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <iostream>
using namespace std;
int main(void)
{
int exit_status = 0;
void *ad_handle = 0;
Integer nrhs = 1, ifail = 0;
cout << "F07CA_A1W_F C++ Header Example Program Results\n\n";
// Skip heading in data file
string mystr;
getline (cin, mystr);
// Read number of x values and algorithmic mode
Integer n, mode;
cin >> n;
cin >> mode;
// Allocate arrays containing A and its factorized form, B
// and the solution X.
nagad_a1w_w_rtype *dl=0, *d=0, *du=0, *b=0;
nagad_a1w_w_rtype *dlf=0, *df=0, *duf=0, *x=0;
Integer n1 = n-1;
if (!(dl = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
!(d = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
!(du = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
!(b = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
!(dlf = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
!(df = NAG_ALLOC(n, nagad_a1w_w_rtype)) ||
!(duf = NAG_ALLOC(n1, nagad_a1w_w_rtype)) ||
!(x = NAG_ALLOC(n, nagad_a1w_w_rtype))) {
cout << "Allocation failure\n";
exit_status = -1;
goto END;
}
// Create AD tape
nagad_a1w_ir_create();
// Read the tridiagonal matrix A and right hand side B, register and copy
double dd;
for (int i = 0; i<n1; i++) {
cin >> dd;
du[i].value = dd;
du[i].id = 0;
nagad_a1w_ir_register_variable(&du[i]);
duf[i] = du[i];
}
for (int i = 0; i<n; i++) {
cin >> dd;
d[i].value = dd;
d[i].id = 0;
nagad_a1w_ir_register_variable(&d[i]);
df[i] = d[i];
}
for (int i = 0; i<n1; i++) {
cin >> dd;
dl[i].value = dd;
dl[i].id = 0;
nagad_a1w_ir_register_variable(&dl[i]);
dlf[i] = dl[i];
}
for (int i = 0; i<n; i++) {
cin >> dd;
b[i].value = dd;
b[i].id = 0;
nagad_a1w_ir_register_variable(&b[i]);
x[i] = b[i];
}
// Create AD configuration data object
ifail = 0;
x10aa_a1w_f_(ad_handle,ifail);
// Set AD computational mode
ifail = 0;
x10ac_a1w_f_(ad_handle,mode,ifail);
// Solve the equations Ax = b for x
ifail = 0;
f07ca_a1w_f_(ad_handle,n,nrhs,dlf,df,duf,x,n,ifail);
// Print primal solution
cout << " Solution:\n";
cout.precision(4);
cout.width(12); cout << " ";
for (int i=0; i<n; i++) {
cout.width(10); cout << nagad_a1w_get_value(x[i]);
}
cout << "\n\n Derivatives calculated: First order adjoints\n";
if (mode==nagad_symbolic) {
cout << " Computational mode : symbolic\n";
} else {
cout << " Computational mode : algorithmic\n";
}
// Obtain derivatives for each output solution point
cout.setf(ios::scientific,ios::floatfield);
cout.setf(ios::right);
cout.precision(2);
for (int i=0; i<n; i++) {
cout << "\n Solution point " << i+1 << endl;
// Reset adjoints, initialize derivative, and evaluate adjoint
nagad_a1w_ir_zero_adjoints();
Integer inc = 1.0;
nagad_a1w_inc_derivative(&x[i],inc);
ifail = -1;
nagad_a1w_ir_interpret_adjoint(ifail);
if (ifail != 0) {
exit_status = 3;
goto END;
}
cout << " dx/d(du) : ";
cout.width(10); cout << " ";
for (int j=0; j<n1; j++) {
double dd = nagad_a1w_get_derivative(du[j]);
cout.width(10); cout << dd;
}
cout << "\n dx/d(d) : ";
for (int j=0; j<n; j++) {
double dd = nagad_a1w_get_derivative(d[j]);
cout.width(10); cout << dd;
}
cout << "\n dx/d(dl) : ";
for (int j=0; j<n1; j++) {
double dd = nagad_a1w_get_derivative(dl[j]);
cout.width(10); cout << dd;
}
cout << "\n dx/d(b) : ";
for (int j=0; j<n; j++) {
double dd = nagad_a1w_get_derivative(b[j]);
cout.width(10); cout << dd;
}
cout << endl;
}
END:
// Remove computational data object and tape
ifail = 0;
x10ab_a1w_f_(ad_handle,ifail);
nagad_a1w_ir_remove();
return exit_status;
}