/* nag_lapackeig_zgesvj (f08kwc) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.3, 2024.
*/
#include <math.h>
#include <nag.h>
#include <stdio.h>
static Integer normalize_vectors(Nag_OrderType order, Integer m, Integer n,
Complex v[], Complex x[]);
int main(void) {
/* Scalars */
double eps, serrbd, ctol = 1.0;
Integer exit_status = 0;
Integer i, j, m, mv, n, n_vrows, n_vcols, pda, pdv, ranka, j1, j2;
/* Arrays */
Complex *a = 0, *v = 0, *x = 0;
double *rcondu = 0, *rcondv = 0, *s = 0;
double rwork[6];
char nag_enum_arg[40];
/* Nag Types */
Nag_OrderType order;
Nag_MatrixType joba;
Nag_LeftVecsType jobu;
Nag_RightVecsType jobv;
NagError fail;
#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
#define V(I, J) v[(J - 1) * pdv + I - 1]
order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
#define V(I, J) v[(I - 1) * pdv + J - 1]
order = Nag_RowMajor;
#endif
INIT_FAIL(fail);
printf("nag_lapackeig_zgesvj (f08kwc) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n]");
scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n);
if (n < 0 || m < n) {
printf("Invalid m or n\n");
exit_status = 1;
goto END;
;
}
/* Read Nag type arguments by name and convert to value */
scanf(" %39s%*[^\n]", nag_enum_arg);
/* nag_enum_name_to_value (x04nac).
* Converts NAG enum member name to value
*/
joba = (Nag_MatrixType)nag_enum_name_to_value(nag_enum_arg);
scanf(" %39s%*[^\n]", nag_enum_arg);
jobu = (Nag_LeftVecsType)nag_enum_name_to_value(nag_enum_arg);
scanf(" %39s%*[^\n]", nag_enum_arg);
jobv = (Nag_RightVecsType)nag_enum_name_to_value(nag_enum_arg);
scanf(" %39s%*[^\n]", nag_enum_arg);
n_vcols = n;
n_vrows = n;
mv = 0;
if (jobv == Nag_RightVecsMV) {
scanf("%" NAG_IFMT, &mv);
n_vrows = mv;
} else if (jobv == Nag_NotRightVecs) {
n_vrows = 1;
n_vcols = 1;
}
scanf("%*[^\n]");
#ifdef NAG_COLUMN_MAJOR
pda = m;
pdv = n_vrows;
#else
pda = n;
pdv = n_vcols;
#endif
if (!(a = NAG_ALLOC(m * n, Complex)) || !(rcondu = NAG_ALLOC(m, double)) ||
!(rcondv = NAG_ALLOC(m, double)) || !(s = NAG_ALLOC(n, double)) ||
!(v = NAG_ALLOC(n_vrows * n_vcols, Complex)) ||
!(x = NAG_ALLOC(n, Complex))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read the m by n matrix A from data file */
j1 = 1;
j2 = n;
if (joba == Nag_UpperMatrix) {
j1 = n;
} else if (joba == Nag_LowerMatrix) {
j2 = 1;
}
for (i = 1; i <= m; i++)
for (j = j1; j <= j2; j++)
scanf(" ( %lf , %lf )", &A(i, j).re, &A(i, j).im);
scanf("%*[^\n]");
if (jobv == Nag_RightVecsMV) {
/* The first mv rows of v must be set. */
for (i = 1; i <= mv; i++)
for (j = 1; j <= n; j++)
scanf(" ( %lf , %lf )", &V(i, j).re, &V(i, j).im);
scanf("%*[^\n]");
}
/* nag_lapackeig_zgesvj (f08kwc)
* Compute the singular values and left and right singular vectors
* of A (A = U*S*V, m>=n).
*/
nag_lapackeig_zgesvj(order, joba, jobu, jobv, m, n, a, pda, s, mv, v, pdv,
ctol, rwork, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_zgesvj (f08kwc).\n%s\n", fail.message);
exit_status = 2;
goto END;
}
/* Get the machine precision, eps and compute the approximate
* error bound for the computed singular values. Note that for
* the 2-norm, s[0] = norm(A).
*/
eps = nag_machine_precision;
serrbd = s[0];
/* Print solution */
printf("Singular values\n ");
for (j = 0; j < n; j++)
printf("%8.4f", s[j]);
printf("\n\n");
if (fabs(rwork[0] - 1.0) > eps)
printf("Values need scaling by factor = %13.5e\n\n", rwork[0]);
ranka = (Integer)rwork[1];
printf("Rank of A = %5" NAG_IFMT "\n\n", ranka);
x[0].re = 2.0;
if (jobu != Nag_NotLeftVecs) {
/* Normalize left vectors so that largest element is real and positive */
exit_status = normalize_vectors(order, m, ranka, a, x);
if (exit_status > 0) {
exit_status = 3;
goto END;
}
/* nag_file_print_matrix_complex_gen_comp (x04dbc)
* Print complex general matrix (comprehensive)
*/
fflush(stdout);
nag_file_print_matrix_complex_gen_comp(
order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, ranka, a, pda,
Nag_BracketForm, "%7.4f", "Left spanning singular vectors",
Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 0, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_print_matrix_complex_gen_comp (x04dbc).\n"
"%s\n",
fail.message);
exit_status = 4;
goto END;
}
}
if (jobv == Nag_RightVecs) {
/* Normalize V, either using x from U or from scratch */
exit_status = normalize_vectors(order, n, n, v, x);
if (exit_status > 0) {
exit_status = 5;
goto END;
}
printf("\n");
fflush(stdout);
nag_file_print_matrix_complex_gen_comp(
order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, v, pdv,
Nag_BracketForm, "%7.4f", "Right singular vectors", Nag_IntegerLabels,
0, Nag_IntegerLabels, 0, 80, 0, 0, &fail);
} else if (jobv == Nag_RightVecsMV) {
printf("\n");
fflush(stdout);
nag_file_print_matrix_complex_gen_comp(
order, Nag_GeneralMatrix, Nag_NonUnitDiag, mv, n, v, pdv,
Nag_BracketForm, "%7.4f", "Right sing. vectors applied to V",
Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 0, &fail);
}
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_print_matrix_complex_gen_comp (x04dbc).\n"
"%s\n",
fail.message);
exit_status = 6;
goto END;
}
/* nag_lapackeig_ddisna (f08flc)
* Estimate reciprocal condition numbers for the singular vectors.
*/
nag_lapackeig_ddisna(Nag_LeftSingVecs, m, n, s, rcondu, &fail);
nag_lapackeig_ddisna(Nag_RightSingVecs, m, n, s, rcondv, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_ddisna (f08flc).\n%s\n", fail.message);
exit_status = 7;
goto END;
}
/* Print the approximate error bounds for the singular values and vectors. */
printf("\n\nError estimates (as multiples of machine precision)\n");
printf("\n for the singular values\n%4.0f\n", serrbd);
printf("\n for left singular vectors\n");
for (i = 0; i < n; i++)
printf("%4.0f", serrbd / rcondu[i]);
printf("\n\n for right singular vectors\n");
for (i = 0; i < n; i++)
printf("%4.0f", serrbd / rcondv[i]);
printf("\n");
END:
NAG_FREE(a);
NAG_FREE(rcondu);
NAG_FREE(rcondv);
NAG_FREE(s);
NAG_FREE(v);
NAG_FREE(x);
return exit_status;
}
static Integer normalize_vectors(Nag_OrderType order, Integer m, Integer n,
Complex v[], Complex x[]) {
/* Each complex vector v[] is normalized so that the element of largest
* magnitude is scaled to be real and positive
*/
double r, rmax, scal;
Integer colinc, rowinc, i, j, k, l, indv, errors = 0;
Complex alpha, y[1];
NagError fail;
INIT_FAIL(fail);
if (order == Nag_ColMajor) {
rowinc = 1;
colinc = m;
} else {
rowinc = n;
colinc = 1;
}
scal = x[0].re;
indv = 0;
for (j = 0; j < n; j++) {
if (scal > 1.5) {
/* Scaling factors not found yet.
* Find element of eigenvector with largest absolute value.
*/
rmax = nag_complex_abs(v[indv]);
l = indv;
k = l;
for (i = 0; i < m; i++) {
/* nag_complex_abs (a02dbc). Modulus of a complex number. */
r = nag_complex_abs(v[l]);
if (r > rmax) {
rmax = r;
k = l;
}
l += rowinc;
}
/* Normalization factor beta */
x[j].re = v[k].re / rmax;
x[j].im = -v[k].im / rmax;
}
/* Scale current vector v_j by factor x[j] using:
* nag_blast_zaxpby (f16gcc) which performs y := alpha*x + beta*y.
*/
alpha = nag_complex_create(0.0, 0.0);
nag_blast_zaxpby(m, alpha, y, 1, x[j], &v[indv], rowinc, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_blast_zaxpby (f16gcc).\n%s\n", fail.message);
errors = 2;
goto END;
}
indv += colinc;
}
END:
return errors;
}