/* nag_lapackeig_dgesvdx (f08kmc) Example Program.
*
* Copyright 2021 Numerical Algorithms Group.
*
* Mark 27.3, 2021.
*/
#include <math.h>
#include <nag.h>
#include <stdio.h>
int main(void) {
/* Scalars */
double serrbd, vl, vu;
Integer exit_status = 0, i, j, mn;
Integer il, iu, m, n, ns, pda, pdu, pdvt;
/* Arrays */
double *rcondu = 0, *rcondv = 0, *uerrbd = 0, *verrbd = 0;
double *a = 0, *s = 0, *u = 0, *vt = 0, *work = 0;
Integer *jfail = 0;
/* Nag Types */
NagError fail;
Nag_OrderType order;
#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
order = Nag_RowMajor;
#endif
INIT_FAIL(fail);
printf("nag_lapackeig_dgesvdx (f08kmc) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n]");
scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n);
mn = MIN(m, n);
if (mn < 0) {
printf("Invalid m or n\n");
exit_status = 1;
goto END;
}
/* Allocate memory */
if (!(a = NAG_ALLOC(m * n, double)) || !(s = NAG_ALLOC(mn, double)) ||
!(u = NAG_ALLOC(m * mn, double)) || !(vt = NAG_ALLOC(mn * n, double)) ||
!(work = NAG_ALLOC(mn, double)) ||
!(jfail = NAG_ALLOC(2 * mn, Integer)) ||
!(rcondu = NAG_ALLOC(n, double)) || !(rcondv = NAG_ALLOC(n, double)) ||
!(uerrbd = NAG_ALLOC(n, double)) || !(verrbd = NAG_ALLOC(n, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
#ifdef NAG_COLUMN_MAJOR
pda = m;
pdu = m;
pdvt = mn;
#else
pda = n;
pdu = mn;
pdvt = n;
#endif
/* 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]");
/* Choose singular values in the interval [0,4] */
vl = 0.0;
vu = 4.0;
il = 1;
iu = 0;
/* nag_lapackeig_dgesvdx (f08kmc).
* Compute the singular values and left and right singular vectors
* of A (A = U*S*(V^T), m.ge.n)
*/
nag_lapackeig_dgesvdx(order, Nag_SingularVecs, Nag_SingularVecs, Nag_Interval,
m, n, a, pda, vl, vu, il, iu, &ns, s, u, pdu, vt, pdvt,
work, jfail, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_dgesvdx (f08kmc).\n%s\n", fail.message);
exit_status = 2;
goto END;
}
/* Print solution */
printf("Number of singular values in the range [%7.3f,%7.3f] = "
" %2" NAG_IFMT "\n\n",
vl, vu, ns);
printf("Selected singular values\n");
for (j = 0; j < ns; j++)
printf("%8.4f", s[j]);
printf("\n\n");
/* Compute the approximate error bound (as multiple of machine precision)
* for the computed singular values.
* Note that for the 2-norm, s[0] = norm(A).
*/
serrbd = s[0];
/* Estimate reciprocal condition numbers for the singular vectors using
* nag_lapackeig_ddisna (f08flc).
*/
nag_lapackeig_ddisna(Nag_LeftSingVecs, m, ns, s, rcondu, &fail);
if (fail.code == NE_NOERROR) {
nag_lapackeig_ddisna(Nag_RightSingVecs, m, ns, s, rcondv, &fail);
}
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_ddisna (f08flc).\n%s\n", fail.message);
exit_status = 3;
goto END;
}
/* Compute the error estimates for the singular vectors */
for (i = 0; i < ns; ++i) {
uerrbd[i] = serrbd / rcondu[i];
verrbd[i] = serrbd / rcondv[i];
}
/* Print the approximate error bounds for the singular values and vectors */
printf("Error estimates (as multiple of machine precision)\n\n");
printf(" for the singular values\n%4ld\n", lrint(serrbd));
printf("\n for the left singular vectors\n");
for (i = 0; i < ns; ++i)
printf("%4ld", lrint(uerrbd[i]));
printf("\n\n for the right singular vectors\n");
for (i = 0; i < ns; ++i)
printf("%4ld", lrint(verrbd[i]));
printf("\n");
END:
NAG_FREE(a);
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(jfail);
return exit_status;
}
#undef A