/* F08KE_A1W_F C++ Header Example Program.
*
* Copyright 2019 Numerical Algorithms Group.
* Mark 27, 2019.
*/
#include <dco_light.hpp>
#include <nag.h>
#include <nagx04.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 ifail = 0;
NagError fail;
INIT_FAIL(fail);
cout << "F08KE_A1W_F C++ Header Example Program Results\n\n";
// Skip heading in data file
string mystr;
getline (cin, mystr);
// Read matrix dimensions
Integer m, n;
cin >> m;
cin >> n;
// Allocate arrays containing A and its factorized form, B
// and the solution X.
Integer lda = m, lwork=64*(m+n);
nagad_a1w_w_rtype *a=0, *a_in=0, *d=0, *e=0, *taup=0, *tauq, *work=0;
double *dd=0, *de = 0;
a = new nagad_a1w_w_rtype [m*n];
a_in = new nagad_a1w_w_rtype [m*n];
d = new nagad_a1w_w_rtype [n];
e = new nagad_a1w_w_rtype [n-1];
taup = new nagad_a1w_w_rtype [n];
tauq = new nagad_a1w_w_rtype [n];
work = new nagad_a1w_w_rtype [lwork];
dd = new double [m*n];
de = new double [m*n-n];
// Create AD tape
nagad_a1w_ir_create();
// Read the matrix A, register and copy
double ddd;
for (int i = 0; i<m; i++) {
for (int j = 0; j<n; j++) {
Integer k = i + j*m;
cin >> ddd;
a_in[k] = ddd;
nagad_a1w_ir_register_variable(&a_in[k]);
a[k] = a_in[k];
}
}
// Create AD configuration data object
ifail = 0;
x10aa_a1w_f_(ad_handle,ifail);
// reduce A to bidiagonal form
ifail = 0;
f08ke_a1w_f_(ad_handle,m,n,a,lda,d,e,tauq,taup,work,lwork,ifail);
// Print bidiagonal form
cout.precision(4);
cout << " Diagonal:" << endl;
cout.width(12); cout << " ";
for (int i=0; i<n; i++) {
cout.width(11); cout << nagad_a1w_get_value(d[i]);
}
cout << endl;
if (m>n) {
cout << " Superdiagonal:" << endl;
} else {
cout << " Subdiagonal:" << endl;
}
cout.width(12); cout << " ";
for (int i=0; i<n-1; i++) {
cout.width(11); cout << nagad_a1w_get_value(e[i]);
}
cout << "\n\n Derivatives calculated: First order adjoints\n";
cout << " Computational mode : algorithmic\n";
cout << "\n Derivatives of d/e w.r.t first column of A\n";
for (int i=0; i<n; i++) {
nagad_a1w_ir_zero_adjoints();
double inc = 1.0;
nagad_a1w_inc_derivative(&d[i],inc);
ifail = 0;
nagad_a1w_ir_interpret_adjoint(ifail);
for (int j=0; j<m; j++) {
Integer k = i + j*n;
dd[k] = nagad_a1w_get_derivative(a_in[j]);
}
}
cout << "\n\n";
x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,m,dd,n,
" dD_i/dA_j1",0,&fail);
for (int i=0; i<n-1; i++) {
nagad_a1w_ir_zero_adjoints();
double inc = 1.0;
nagad_a1w_inc_derivative(&e[i],inc);
ifail = 0;
nagad_a1w_ir_interpret_adjoint(ifail);
for (int j=0; j<m; j++) {
Integer k = i + j*(n-1);
de[k] = nagad_a1w_get_derivative(a_in[j]);
}
}
cout << "\n";
x04cac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n-1,m,de,n-1,
" dE_i/DA_j1",0,&fail);
// Remove computational data object and tape
ifail = 0;
x10ab_a1w_f_(ad_handle,ifail);
nagad_a1w_ir_remove();
delete [] a;
delete [] a_in;
delete [] d;
delete [] e;
delete [] taup;
delete [] tauq;
delete [] work;
delete [] dd;
delete [] de;
return exit_status;
}