/* nag_zungbr (f08ktc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include 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); Vprintf("nag_zungbr (f08ktc) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); for (ic = 1; ic <= 2; ++ic) { Vscanf("%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)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } /* Read A from data file */ for (i = 1; i <= m; ++i) { for (j = 1; j <= n; ++j) Vscanf(" ( %lf , %lf )", &A(i,j).re, &A(i,j).im); } Vscanf("%*[^\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) { Vprintf("Error from nag_zgebrd (f08ksc).\n%s\n", fail.message); exit_status = 1; goto END; } 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) { Vprintf("Error from nag_zungbr (f08ktc).\n%s\n", fail.message); exit_status = 1; goto END; } /* 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) { Vprintf("Error from nag_zungbr (f08ktc).\n%s\n", fail.message); exit_status = 1; goto END; } /* 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) { Vprintf("Error from nag_zbdsqr (f08msc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print singular values, left & right singular vectors */ Vprintf("\nExample 1: singular values\n"); for (i = 1; i <= n; ++i) Vprintf("%8.4f%s", d[i-1], i%8==0?"\n":" "); Vprintf("\n\n"); /* nag_gen_complx_mat_print_comp (x04dbc). * Print complex general matrix (comprehensive) */ 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) { Vprintf("Error from nag_gen_complx_mat_print_comp (x04dbc)." "\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n"); /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ 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) { Vprintf("Error from nag_gen_complx_mat_print_comp (x04dbc)." "\n%s\n", fail.message); exit_status = 1; goto END; } } 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) { Vprintf("Error from nag_zungbr (f08ktc).\n%s\n", fail.message); exit_status = 1; goto END; } /* 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) { Vprintf("Error from nag_zungbr (f08ktc).\n%s\n", fail.message); exit_status = 1; goto END; } /* 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) { Vprintf("Error from nag_zbdsqr (f08msc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print singular values, left & right singular vectors */ Vprintf("\nExample 2: singular values\n"); for (i = 1; i <= m; ++i) Vprintf("%8.4f%s", d[i-1], i%8==0 ?"\n":" "); Vprintf("\n\n"); /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ 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) { Vprintf("Error from nag_gen_complx_mat_print_comp (x04dbc)." "\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n"); /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ 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) { Vprintf("Error from nag_gen_complx_mat_print_comp (x04dbc)." "\n%s\n", fail.message); exit_status = 1; goto END; } } END: if (a) NAG_FREE(a); if (c) NAG_FREE(c); if (taup) NAG_FREE(taup); if (tauq) NAG_FREE(tauq); if (u) NAG_FREE(u); if (vt) NAG_FREE(vt); if (d) NAG_FREE(d); if (e) NAG_FREE(e); } return exit_status; }