/* F08ME_T1W_F C++ Header Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
* Mark 30.2, 2024.
*/
#include <dco.hpp>
#include <iostream>
#include <nag.h>
#include <nagad.h>
#include <nagx04.h>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
int exit_status = 0;
nag::ad::handle_t ad_handle;
Integer ifail;
NagError fail;
INIT_FAIL(fail);
cout << "F08ME_T1W_F C++ Header Example Program Results\n\n";
// Skip heading in data file
string mystr;
getline(cin, mystr);
// Read matrix dimensions
Integer n;
cin >> n;
// Allocate arrays containing A and its factorized form, B
// and the solution X.
Integer ldc = 1, ldu = n, ldvt = n;
nagad_t1w_w_rtype *c = 0, *d = 0, *e = 0, *d_in = 0, *e_in = 0, *u = 0,
*vt = 0, *work = 0;
double *dr = 0, *er = 0, *dsdd = 0, *dsde = 0, *ur = 0, *vtr = 0;
c = new nagad_t1w_w_rtype[1];
d = new nagad_t1w_w_rtype[n];
e = new nagad_t1w_w_rtype[n - 1];
d_in = new nagad_t1w_w_rtype[n];
e_in = new nagad_t1w_w_rtype[n - 1];
u = new nagad_t1w_w_rtype[n * n];
vt = new nagad_t1w_w_rtype[n * n];
work = new nagad_t1w_w_rtype[4 * n];
dr = new double[n];
er = new double[n - 1];
dsdd = new double[n * n];
dsde = new double[n * n - n];
ur = new double[n * n];
vtr = new double[n * n];
// Read the matrix A, register and copy
double ddd;
for (int i = 0; i < n; i++)
{
cin >> ddd;
d_in[i] = ddd;
}
for (int i = 0; i < n - 1; i++)
{
cin >> ddd;
e_in[i] = ddd;
}
// Initialize U and VT to be the unit matrix
for (int i = 0; i < n * n; i++)
{
u[i] = 0.0;
vt[i] = 0.0;
}
for (int i = 0; i < n; i++)
{
u[i * n + i] = 1.0;
vt[i * n + i] = 1.0;
}
// Create AD configuration data object
ifail = 0;
double inc = 1.0, zero = 0.0;
for (int i = 0; i < 2 * n - 1; i++)
{
if (i < n)
{
dco::derivative(d_in[i]) = inc;
}
else
{
dco::derivative(e_in[i - n]) = inc;
}
for (int j = 0; j < n; j++)
{
d[j] = d_in[j];
}
for (int j = 0; j < n - 1; j++)
{
e[j] = e_in[j];
}
// Initialize U and VT to be the unit matrix
for (int j = 0; j < n * n; j++)
{
u[j] = 0.0;
vt[j] = 0.0;
}
for (int j = 0; j < n; j++)
{
u[j * n + j] = 1.0;
vt[j * n + j] = 1.0;
}
// Calculate the SVD of bidiagonal matrix defined by d, e
ifail = 0;
nag::ad::f08me(ad_handle, "U", n, n, n, 0, d, e, u, ldu, vt, ldvt, c, ldc,
work, ifail);
if (i < n)
{
dco::derivative(d_in[i]) = zero;
for (int j = 0; j < n; j++)
{
Integer k = i * n + j;
dsdd[k] = dco::derivative(d[j]);
}
}
else
{
dco::derivative(e_in[i - n]) = zero;
for (int j = 0; j < n; j++)
{
Integer k = (i - n) * n + j;
dsde[k] = dco::derivative(d[j]);
}
}
}
// Print singular values
cout.precision(4);
cout << " Singular values:" << endl;
cout.width(12);
cout << " ";
for (int i = 0; i < n; i++)
{
cout.width(11);
cout << dco::value(d[i]);
}
cout << endl;
for (int i = 0; i < n * n; i++)
{
ur[i] = dco::value(u[i]);
vtr[i] = dco::value(vt[i]);
}
cout << endl;
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, ur, n,
" Left Singular values (columns)", 0, &fail);
cout << endl;
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, vtr, n,
" Right Singular values (rows)", 0, &fail);
cout << "\n\n Derivatives calculated: First order tangents\n";
cout << " Computational mode : algorithmic\n";
cout << "\n Derivatives of singular values w.r.t input d and e\n";
cout << endl;
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, dsdd, n,
" dS_i/dD_j", 0, &fail);
cout << endl;
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n - 1, dsde, n,
" dS_i/DE_j", 0, &fail);
ifail = 0;
delete[] c;
delete[] d;
delete[] e;
delete[] d_in;
delete[] e_in;
delete[] u;
delete[] vt;
delete[] work;
delete[] dr;
delete[] er;
delete[] dsdd;
delete[] dsde;
delete[] ur;
delete[] vtr;
return exit_status;
}