/* F07CA_T1W_F C++ Header Example Program.
*
* Copyright 2019 Numerical Algorithms Group.
* Mark 27, 2019.
*/
#include <dco_light.hpp>
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
int exit_status = 0;
void *ad_handle = 0;
Integer nrhs = 1, ifail = 0;
NagError fail;
INIT_FAIL(fail);
cout << "F07CA_T1W_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;
cin >> n;
// Allocate arrays containing A and its factorized form, B
// and the solution X.
nagad_t1w_w_rtype *dl=0, *d=0, *du=0, *b=0;
nagad_t1w_w_rtype *dlf=0, *df=0, *duf=0, *x=0;
double *sol=0, *dxdu=0, *dxdd=0, *dxdl=0, *dxdb=0;
Integer *ipiv = 0;
Integer n1 = n-1;
dl = new nagad_t1w_w_rtype [n1];
d = new nagad_t1w_w_rtype [n];
du = new nagad_t1w_w_rtype [n1];
dlf = new nagad_t1w_w_rtype [n1];
df = new nagad_t1w_w_rtype [n];
duf = new nagad_t1w_w_rtype [n1];
b = new nagad_t1w_w_rtype [n*nrhs];
ipiv = new Integer [n];
x = new nagad_t1w_w_rtype [n*n];
sol = new double [n*n];
dxdu = new double [n*n1];
dxdd = new double [n*n];
dxdl = new double [n*n1];
dxdb = new double [n*n];
// 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] = dd;
}
for (int i = 0; i<n; i++) {
cin >> dd;
d[i] = dd;
}
for (int i = 0; i<n1; i++) {
cin >> dd;
dl[i] = dd;
}
for (int i = 0; i<n; i++) {
cin >> dd;
b[i] = dd;
}
// Create AD configuration data object
ifail = 0;
x10aa_t1w_f_(ad_handle,ifail);
double inc = 1.0, zero = 0.0;
for (int i=0; i<4*n-2; ++i) {
int k = i;
if (i<n1) {
nagad_t1w_inc_derivative(&du[i],inc);
} else if (i<n+n1) {
k = i - n1;
nagad_t1w_inc_derivative(&d[k],inc);
} else if (i<n+n1+n1) {
k = i - n - n1;
nagad_t1w_inc_derivative(&dl[k],inc);
} else {
k = i - n - n1 - n1;
nagad_t1w_inc_derivative(&b[k],inc);
}
for (int j=0; j<n1; ++j) {
dlf[j] = dl[j];
df[j] = d[j];
duf[j] = du[j];
x[j] = b[j];
}
df[n1] = d[n1]; x[n1] = b[n1];
// Solve the equations Ax = b for x
ifail = 0;
f07ca_t1w_f_(ad_handle,n,nrhs,dlf,df,duf,x,n,ifail);
if (i==0) {
// Print primal solution
for (int j=0; j<n*nrhs; ++j) {
sol[j] = nagad_t1w_get_value(x[j]);
}
cout << "\n\n";
x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,nrhs,sol,n,
" Solution",0,&fail);
}
if (i<n1) {
nagad_t1w_set_derivative(&du[k],zero);
for (int j=0; j<n; ++j) {
dxdu[j+k*n] = nagad_t1w_get_derivative(x[j]);
}
} else if (i<n+n1) {
nagad_t1w_set_derivative(&d[k],zero);
for (int j=0; j<n; ++j) {
dxdd[j+k*n] = nagad_t1w_get_derivative(x[j]);
}
} else if (i<n+n1+n1) {
nagad_t1w_set_derivative(&dl[k],zero);
for (int j=0; j<n; ++j) {
dxdl[j+k*n] = nagad_t1w_get_derivative(x[j]);
}
} else {
nagad_t1w_set_derivative(&b[k],zero);
for (int j=0; j<n; ++j) {
dxdb[j+k*n] = nagad_t1w_get_derivative(x[j]);
}
}
}
cout << "\n\n Derivatives calculated: First order tangents\n";
cout << " Computational mode : symbolic\n";
cout << "\n";
x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n1,dxdu,n,
" d(du(i))/dx(j)",0,&fail);
cout << "\n";
x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n,dxdd,n,
" d(d(i))/dx(j)",0,&fail);
cout << "\n";
x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n1,dxdl,n,
" d(dl(i))/dx(j)",0,&fail);
cout << "\n";
x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n,dxdb,n,
" d(b(i))/dx(j)",0,&fail);
// Remove computational data object
ifail = 0;
x10ab_t1w_f_(ad_handle,ifail);
delete [] dl;
delete [] d;
delete [] du;
delete [] dlf;
delete [] df;
delete [] duf;
delete [] b;
delete [] ipiv;
delete [] x;
delete [] sol;
delete [] dxdu;
delete [] dxdd;
delete [] dxdl;
delete [] dxdb;
return exit_status;
}