/* nag_rnla_svd_rowext_real (f10cac) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.3, 2024.
*/
#include <math.h>
#include <nag.h>
#include <stdio.h>
int main(void) {
/* Scalars */
Integer exit_status = 0, i, j, m, n, pda, pdu, pdvt;
/* Arrays */
double *a = 0, *a_copy = 0, *rcondu = 0, *rcondv = 0;
double *s = 0, *u = 0, *uerrbd = 0, *verrbd = 0, *vt = 0, *work = 0;
/* Nag Types */
NagError fail;
Nag_OrderType order;
/* Declarations for randomised algorithm */
Integer k = 5, r = 0;
double rtol_abs, rtol_rel;
char nag_enum_arg1[40];
char nag_enum_arg2[40];
Nag_ComputeUType jobu;
Nag_ComputeVTType jobvt;
/* Declarations for RNG */
Integer lstate;
Integer *state = 0;
double *x = 0;
Nag_BaseRNG genid = Nag_Basic;
Integer subid = 0, lseed = 1;
Integer seed[] = {1762543};
#define A(I, J) a[(J - 1) * pda + I - 1]
order = Nag_ColMajor;
INIT_FAIL(fail);
printf("nag_rnla_svd_rowext_real (f10cac) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n]");
scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n);
if (m < 0 || n < 0) {
printf("Invalid m or n\n");
exit_status = 1;
goto END;
}
/* Allocate memory */
if (!(a = NAG_ALLOC(m * n, double)) || !(a_copy = NAG_ALLOC(m * n, double)) ||
!(rcondu = NAG_ALLOC(n, double)) || !(rcondv = NAG_ALLOC(n, double)) ||
!(s = NAG_ALLOC(MIN(m, n), double)) || !(u = NAG_ALLOC(m * m, double)) ||
!(uerrbd = NAG_ALLOC(n, double)) || !(verrbd = NAG_ALLOC(n, double)) ||
!(vt = NAG_ALLOC(m * n, double)) ||
!(work = NAG_ALLOC(MIN(m, n), double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
pdu = m;
pda = m;
pdvt = n;
/* Read the m by n matrix A from data file */
for (i = 1; i <= m; ++i) {
for (j = 1; j <= n; ++j)
scanf("%lf", &A(i, j));
}
scanf("%*[^\n]");
/* Read in values for jobu, jobvt, k */
scanf("%39s %39s %" NAG_IFMT "%*[^\n] ", nag_enum_arg1, nag_enum_arg2, &k);
jobu = (Nag_ComputeUType)nag_enum_name_to_value(nag_enum_arg1);
jobvt = (Nag_ComputeVTType)nag_enum_name_to_value(nag_enum_arg2);
/* Copy a into a_copy */
for (i = 0; i < m * n; i++)
a_copy[i] = a[i];
/* nag_file_print_matrix_real_gen (x04cac)
* Print real general matrix A.
*/
fflush(stdout);
nag_file_print_matrix_real_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m,
n, a, pda, "Matrix A", 0, &fail);
printf("\n");
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_print_matrix_real_gen (x04cac).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* Perform deterministic SVD of A */
/* nag_lapackeig_dgesvd (f08kbc).
* Compute the singular values and left and right singular vectors
* of A (A = U*S*(V^T), m >= n)
*/
nag_lapackeig_dgesvd(order, Nag_AllU, Nag_AllVT, m, n, a_copy, pda, s, u, pdu,
vt, pdvt, work, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_dgesvd (f08kbc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
printf(" Singular values of A using dgesvd: \n");
for (i = 0; i < n; ++i)
printf(" %10.1e\n", s[i]);
printf("\n");
/* Random number generation */
/* Initialize the error structure */
INIT_FAIL(fail);
/* Get the length of the state array */
lstate = -1;
nag_rand_init_repeat(genid, subid, seed, lseed, state, &lstate, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_rand_init_repeat (g05kfc).\n%s\n", fail.message);
exit_status = 2;
goto END;
}
/* Allocate arrays */
if (!(x = NAG_ALLOC(n, double)) || !(state = NAG_ALLOC(lstate, Integer))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Initialize the generator to a repeatable sequence */
nag_rand_init_repeat(genid, subid, seed, lseed, state, &lstate, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_rand_init_repeat (g05kfc).\n%s\n", fail.message);
exit_status = 3;
goto END;
}
/* Randomised SVD of A */
rtol_abs = pow(X02AJC, 0.875);
rtol_rel = rtol_abs;
/* nag_rnla_svd_rowext_real (f10cac).
* Compute the singular values and left and right singular vectors
* of A (A = U*S*(V^T)
*/
nag_rnla_svd_rowext_real(jobu, jobvt, m, n, a, pda, k, rtol_abs, rtol_rel,
state, s, u, pdu, vt, pdvt, &r, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_rnla_svd_rowext_real (f10cac).\n%s\n", fail.message);
exit_status = 4;
goto END;
}
printf(" Singular values of A using Randomized SVD:\n");
for (i = 0; i < r; ++i)
printf(" %10.1e\n", s[i]);
END:
NAG_FREE(a);
NAG_FREE(a_copy);
NAG_FREE(rcondu);
NAG_FREE(rcondv);
NAG_FREE(s);
NAG_FREE(u);
NAG_FREE(uerrbd);
NAG_FREE(verrbd);
NAG_FREE(vt);
NAG_FREE(work);
NAG_FREE(x);
NAG_FREE(state);
return exit_status;
}
#undef A