/* F08KP_P0W_F C++ Header Example Program.
*
* Copyright 2019 Numerical Algorithms Group.
* Mark 27, 2019.
*/
#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_P0W_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;
Complex *a=0, *u=0, *vt=0, *work=0, dummy[1];
double *s=0, *rwork=0;
Charlen lena = 1;
a = new Complex [m*n];
s = new double [m];
rwork = new double [5*n];
u = new Complex [m*m];
vt = new Complex [n*n];
// 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;
a[k].re = dd;
a[k].im = di;
}
}
// Use routine workspace query to get optimal workspace.
ifail = 0;
lwork = -1;
f08kp_p0w_f_(ad_handle,"A","A",m,n,a,lda,s,u,ldu,vt,ldvt,dummy,lwork,rwork,
ifail,lena,lena);
lwork = (Integer) dummy[0].re + 1;
work = new Complex [lwork];
// Compute the singular values and left and right singular vectors
// of A (A = U*S*(V**T), m < n)
f08kp_p0w_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 << s[i];
}
cout << "\n\n";
x04dac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,m,m,u,m,
"Left singular vectors by column",0,&fail);
cout << "\n";
x04dac(Nag_ColMajor,Nag_GeneralMatrix,Nag_NonUnitDiag,n,n,vt,n,
"Right singular vectors by row",0,&fail);
delete [] a;
delete [] s;
delete [] u;
delete [] vt;
delete [] work;
delete [] rwork;
return exit_status;
}