/* F08ME_P0W_F C++ Header Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
* Mark 30.0, 2024.
*/
#include <iostream>
#include <dco.hpp>
#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_P0W_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;
double *c = 0, *d = 0, *e = 0, *u = 0, *vt = 0, *work = 0;
c = new double[1];
d = new double[n];
e = new double[n - 1];
u = new double[n * n];
vt = new double[n * n];
work = new double[4 * n];
// Read the matrix A, register and copy
for (int i = 0; i < n; i++)
{
cin >> d[i];
}
for (int i = 0; i < n - 1; i++)
{
cin >> e[i];
}
// 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;
}
// 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);
// 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 << d[i];
}
cout << endl;
cout << endl;
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, u, n,
" Left Singular values (columns)", 0, &fail);
cout << endl;
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, vt, n,
" Right Singular values (rows)", 0, &fail);
delete[] c;
delete[] d;
delete[] e;
delete[] u;
delete[] vt;
delete[] work;
return exit_status;
}