/* nag_zungbr (f08ktc) Example Program.
*
* Copyright 2014 Numerical Algorithms Group.
*
* Mark 7, 2001.
*/
#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf08.h>
#include <nagx04.h>
int main(void)
{
/* Scalars */
Integer i, ic, j, m, n, pda, pdc, pdu, pdvt, d_len;
Integer e_len, tauq_len, taup_len;
Integer exit_status = 0;
NagError fail;
Nag_OrderType order;
/* Arrays */
Complex *a = 0, *c = 0, *taup = 0, *tauq = 0, *u = 0, *vt = 0;
double *d = 0, *e = 0;
#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J-1)*pda + I - 1]
#define VT(I, J) vt[(J-1)*pdvt + I - 1]
#define U(I, J) u[(J-1)*pdu + I - 1]
order = Nag_ColMajor;
#else
#define A(I, J) a[(I-1)*pda + J - 1]
#define VT(I, J) vt[(I-1)*pdvt + J - 1]
#define U(I, J) u[(I-1)*pdu + J - 1]
order = Nag_RowMajor;
#endif
INIT_FAIL(fail);
printf("nag_zungbr (f08ktc) Example Program Results\n");
/* Skip heading in data file */
scanf("%*[^\n] ");
for (ic = 1; ic <= 2; ++ic)
{
scanf("%ld%ld%*[^\n] ", &m, &n);
d_len = n;
#ifdef NAG_COLUMN_MAJOR
pda = m;
pdc = n;
pdu = m;
pdvt = m;
e_len = n-1;
tauq_len = n;
taup_len = n;
#else
pda = n;
pdc = n;
pdu = n;
pdvt = n;
e_len = n-1;
tauq_len = n;
taup_len = n;
#endif
/* Allocate memory */
if (!(a = NAG_ALLOC(m * n, Complex)) ||
!(c = NAG_ALLOC(n * n, Complex)) ||
!(taup = NAG_ALLOC(taup_len, Complex)) ||
!(tauq = NAG_ALLOC(tauq_len, Complex)) ||
!(u = NAG_ALLOC(m * n, Complex)) ||
!(vt = NAG_ALLOC(m * n, Complex)) ||
!(d = NAG_ALLOC(d_len, double)) ||
!(e = NAG_ALLOC(e_len, double)))
{
printf("Allocation failure\n");
exit_status = -1;
goto ENDL;
}
/* Read A from data file */
for (i = 1; i <= m; ++i)
{
for (j = 1; j <= n; ++j)
scanf(" ( %lf , %lf )", &A(i, j).re, &A(i, j).im);
}
scanf("%*[^\n] ");
/* Reduce A to bidiagonal form */
/* nag_zgebrd (f08ksc).
* Unitary reduction of complex general rectangular matrix
* to bidiagonal form
*/
nag_zgebrd(order, m, n, a, pda, d, e, tauq, taup, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_zgebrd (f08ksc).\n%s\n", fail.message);
exit_status = 1;
goto ENDL;
}
if (m >= n)
{
/* Copy A to VT and U */
for (i = 1; i <= n; ++i)
{
for (j = i; j <= n; ++j)
{
VT(i, j).re = A(i, j).re;
VT(i, j).im = A(i, j).im;
}
}
for (i = 1; i <= m; ++i)
{
for (j = 1; j <= MIN(i, n); ++j)
{
U(i, j).re = A(i, j).re;
U(i, j).im = A(i, j).im;
}
}
/* Form P**H explicitly, storing the result in VT */
/* nag_zungbr (f08ktc).
* Generate unitary transformation matrices from reduction
* to bidiagonal form determined by nag_zgebrd (f08ksc)
*/
nag_zungbr(order, Nag_FormP, n, n, m, vt, pdvt, taup, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_zungbr (f08ktc).\n%s\n",
fail.message);
exit_status = 1;
goto ENDL;
}
/* Form Q explicitly, storing the result in U */
/* nag_zungbr (f08ktc), see above. */
nag_zungbr(order, Nag_FormQ, m, n, n, u, pdu, tauq, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_zungbr (f08ktc).\n%s\n",
fail.message);
exit_status = 1;
goto ENDL;
}
/* Compute the SVD of A */
/* nag_zbdsqr (f08msc).
* SVD of real bidiagonal matrix reduced from complex
* general matrix
*/
nag_zbdsqr(order, Nag_Upper, n, n, m, 0, d, e, vt, pdvt, u,
pdu, c, pdc, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_zbdsqr (f08msc).\n%s\n",
fail.message);
exit_status = 1;
goto ENDL;
}
/* Print singular values, left & right singular vectors */
printf("\nExample 1: singular values\n");
for (i = 1; i <= n; ++i)
printf("%8.4f%s", d[i-1], i%8 == 0?"\n":" ");
printf("\n\n");
/* nag_gen_complx_mat_print_comp (x04dbc).
* Print complex general matrix (comprehensive)
*/
fflush(stdout);
nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix,
Nag_NonUnitDiag, n, n, vt, pdvt,
Nag_BracketForm, "%7.4f",
"Example 1: right singular vectors, "
"by row", Nag_IntegerLabels, 0,
Nag_IntegerLabels, 0, 80, 0,
0, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_gen_complx_mat_print_comp (x04dbc)."
"\n%s\n", fail.message);
exit_status = 1;
goto ENDL;
}
printf("\n");
/* nag_gen_complx_mat_print_comp (x04dbc), see above. */
fflush(stdout);
nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix,
Nag_NonUnitDiag, m, n, u, pdu,
Nag_BracketForm, "%7.4f",
"Example 1: left singular vectors, "
"by column", Nag_IntegerLabels, 0,
Nag_IntegerLabels, 0, 80, 0,
0, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_gen_complx_mat_print_comp (x04dbc)."
"\n%s\n", fail.message);
exit_status = 1;
goto ENDL;
}
}
else
{
/* Copy A to VT and U */
for (i = 1; i <= m; ++i)
{
for (j = i; j <= n; ++j)
{
VT(i, j).re = A(i, j).re;
VT(i, j).im = A(i, j).im;
}
}
for (i = 1; i <= m; ++i)
{
for (j = 1; j <= i; ++j)
{
U(i, j).re = A(i, j).re;
U(i, j).im = A(i, j).im;
}
}
/* Form P**H explicitly, storing the result in VT */
/* nag_zungbr (f08ktc), see above. */
nag_zungbr(order, Nag_FormP, m, n, m, vt, pdvt, taup, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_zungbr (f08ktc).\n%s\n",
fail.message);
exit_status = 1;
goto ENDL;
}
/* Form Q explicitly, storing the result in U */
/* nag_zungbr (f08ktc), see above. */
nag_zungbr(order, Nag_FormQ, m, m, n, u, pdu, tauq, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_zungbr (f08ktc).\n%s\n",
fail.message);
exit_status = 1;
goto ENDL;
}
/* Compute the SVD of A */
/* nag_zbdsqr (f08msc), see above. */
nag_zbdsqr(order, Nag_Lower, m, n, m, 0, d, e, vt, pdvt, u,
pdu, c, pdc, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_zbdsqr (f08msc).\n%s\n",
fail.message);
exit_status = 1;
goto ENDL;
}
/* Print singular values, left & right singular vectors */
printf("\nExample 2: singular values\n");
for (i = 1; i <= m; ++i)
printf("%8.4f%s", d[i-1], i%8 == 0?"\n":" ");
printf("\n\n");
/* nag_gen_complx_mat_print_comp (x04dbc), see above. */
fflush(stdout);
nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix,
Nag_NonUnitDiag, m, n, vt, pdvt,
Nag_BracketForm, "%7.4f",
"Example 2: right singular vectors, "
"by row", Nag_IntegerLabels, 0,
Nag_IntegerLabels, 0, 80, 0,
0, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_gen_complx_mat_print_comp (x04dbc)."
"\n%s\n", fail.message);
exit_status = 1;
goto ENDL;
}
printf("\n");
/* nag_gen_complx_mat_print_comp (x04dbc), see above. */
fflush(stdout);
nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix,
Nag_NonUnitDiag, m, m, u, pdu,
Nag_BracketForm, "%7.4f",
"Example 2: left singular vectors, "
"by column", Nag_IntegerLabels, 0,
Nag_IntegerLabels, 0, 80, 0,
0, &fail);
if (fail.code != NE_NOERROR)
{
printf("Error from nag_gen_complx_mat_print_comp (x04dbc)."
"\n%s\n", fail.message);
exit_status = 1;
goto ENDL;
}
}
ENDL:
NAG_FREE(a);
NAG_FREE(c);
NAG_FREE(taup);
NAG_FREE(tauq);
NAG_FREE(u);
NAG_FREE(vt);
NAG_FREE(d);
NAG_FREE(e);
}
NAG_FREE(a);
NAG_FREE(c);
NAG_FREE(taup);
NAG_FREE(tauq);
NAG_FREE(u);
NAG_FREE(vt);
NAG_FREE(d);
NAG_FREE(e);
return exit_status;
}