/* F07CE_A1W_F C++ Header Example Program.
*
* Copyright 2019 Numerical Algorithms Group.
* Mark 27, 2019.
*/
#include <nag.h>
#include <nagx04.h>
#include <nagad.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
int exit_status = 0;
void *ad_handle = 0;
Integer nrhs = 1, ifail = 0;
cout << "F07CE_A1W_F C++ Header Example Program Results\n\n";
// Skip heading in data file
string mystr;
getline (cin, mystr);
// Read problem size and number of right-hand-sides
Integer n;
cin >> n;
cin >> nrhs;
// Allocate arrays containing A and its factorized form, B
// and the solution X.
nagad_a1w_w_rtype *dl=0, *d=0, *du=0, *du2=0, *b=0;
nagad_a1w_w_rtype *dlf=0, *df=0, *duf=0, *x=0;
double *sol=0;
Integer *ipiv = 0;
Integer n1 = n-1, n2 = n-2;
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)) ||
!(du2 = NAG_ALLOC(n2, 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)) ||
!(b = NAG_ALLOC(n*nrhs, nagad_a1w_w_rtype)) ||
!(ipiv = NAG_ALLOC(n, Integer)) ||
!(x = NAG_ALLOC(n*n, nagad_a1w_w_rtype)) ||
!(sol = NAG_ALLOC(n*n, double))) {
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++) {
for (int j = 0; j<nrhs; j++) {
cin >> dd;
int k = i + j*n;
b[k].value = dd;
b[k].id = 0;
nagad_a1w_ir_register_variable(&b[k]);
x[k] = b[k];
}
}
// Create AD configuration data object
ifail = 0;
x10aa_a1w_f_(ad_handle,ifail);
// Factorize the tridiagonal matrix A
ifail = 0;
f07cd_a1w_f_(ad_handle,n,dlf,df,duf,du2,ipiv,ifail);
// Solve the equations Ax = b for x
ifail = 0;
f07ce_a1w_f_(ad_handle,"N",n,nrhs,dlf,df,duf,du2,ipiv,x,n,ifail,1);
// Print primal solution
for (int i=0; i<n*nrhs; i++) {
sol[i] = nagad_a1w_get_value(x[i]);
}
cout << "\n\n";
NagError fail;
INIT_FAIL(fail);
x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,nrhs,sol,n,
" Solution",0,&fail);
cout << "\n\n Derivatives calculated: First order adjoints\n";
cout << " Computational mode : algorithmic\n";
cout << "\n Derivatives of first solution column w.r.t. inputs:\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();
double 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;
}