/* F08KP_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 << "F08KP_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, ldu = m, ldvt = n, lwork;
nagad_a1w_w_ctype *a=0, *u=0, *vt=0, *work=0, dummy[1];
nagad_a1w_w_rtype *ar=0, *ai=0, *s=0, *rwork=0;
Complex *uc=0, *vtc=0, *dsda=0;
Charlen lena = 1;
a = new nagad_a1w_w_ctype [m*n];
ar = new nagad_a1w_w_rtype [m*n];
ai = new nagad_a1w_w_rtype [m*n];
s = new nagad_a1w_w_rtype [m];
rwork = new nagad_a1w_w_rtype [5*n];
dsda = new Complex [n*m];
u = new nagad_a1w_w_ctype [m*m];
vt = new nagad_a1w_w_ctype [n*n];
// Create AD tape
nagad_a1w_ir_create();
// Read the matrix A, register and copy
double dd, di;
for (int i = 0; i<m; i++) {
for (int j = 0; j<n; j++) {
cin >> dd >> di;
Integer k = i + j*m;
ar[k] = dd;
ai[k] = di;
if (j==0) {
nagad_a1w_ir_register_variable(&ar[k]);
nagad_a1w_ir_register_variable(&ai[k]);
}
a[k].real(ar[k]);
a[k].imag(ai[k]);
}
}
// Create AD configuration data object
ifail = 0;
x10aa_a1w_f_(ad_handle,ifail);
// Use routine workspace query to get optimal workspace.
ifail = 0;
lwork = -1;
f08kp_a1w_f_(ad_handle,"A","A",m,n,a,lda,s,u,ldu,vt,ldvt,dummy,lwork,rwork,
ifail,lena,lena);
lwork = (Integer) nagad_a1w_get_value(real(dummy[0])) + 1;
work = new nagad_a1w_w_ctype [lwork];
// Compute the singular values and left and right singular vectors
// of A (A = U*S*(V**T), m < n)
f08kp_a1w_f_(ad_handle,"A","A",m,n,a,lda,s,u,ldu,vt,ldvt,work,lwork,rwork,
ifail,lena,lena);
// Print primal solution
cout.precision(4);
cout.width(12); cout << " ";
cout << " Singular values:\n";
for (int i=0; i<n; i++) {
cout.width(11); cout << nagad_a1w_get_value(s[i]);
}
// Copy primal values to array for printing
uc = new Complex [m*m];
vtc = new Complex [n*n];
for (int i=0; i<m*m; i++) {
uc[i].re = nagad_a1w_get_value(real(u[i]));
uc[i].im = nagad_a1w_get_value(imag(u[i]));
}
for (int i=0; i<n*n; i++) {
vtc[i].re = nagad_a1w_get_value(real(vt[i]));
vtc[i].im = nagad_a1w_get_value(imag(vt[i]));
}
cout << "\n\n";
x04dac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,m,m,uc,m,
"Left singular vectors by column",0,&fail);
cout << "\n";
x04dac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n,vtc,n,
"Right singular vectors by row",0,&fail);
cout << "\n\n Derivatives calculated: First order adjoints\n";
cout << " Computational mode : algorithmic\n";
cout << "\n Derivatives of Singular values w.r.t first column of A\n";
// Obtain derivatives for each singular value w.r.t first column of A
cout.setf(ios::scientific,ios::floatfield);
cout.setf(ios::right);
cout.precision(2);
for (int i=0; i<n; i++) {
// Setup evaluation of derivatives via adjoints
nagad_a1w_ir_zero_adjoints();
double inc = 1.0;
nagad_a1w_inc_derivative(&s[i],inc);
ifail = 0;
nagad_a1w_ir_interpret_adjoint(ifail);
// Get derivatives
for (int j=0; j<m; j++) {
dsda[i+n*j].re = nagad_a1w_get_derivative(ar[j]);
dsda[i+n*j].im = nagad_a1w_get_derivative(ai[j]);
}
}
cout << "\n";
x04dac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,m,dsda,n,
" dS_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 [] ar;
delete [] ai;
delete [] s;
delete [] u;
delete [] vt;
delete [] work;
delete [] rwork;
delete [] uc;
delete [] vtc;
delete [] dsda;
return exit_status;
}