/* nag_dorgbr (f08kfc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; char *outfile = 0; /* 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 */ double *a = 0, *c = 0, *d = 0, *e = 0, *taup = 0, *tauq = 0, *u = 0; double *vt = 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); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); (void) nag_example_file_io(argc, argv, "-nag_write", &outfile); fprintf(fpout, "nag_dorgbr (f08kfc) Example Program Results\n\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); for (ic = 1; ic <= 2; ++ic) { fscanf(fpin, "%ld%ld%*[^\n] ", &m, &n); #ifdef NAG_COLUMN_MAJOR pda = m; pdu = m; pdvt = m; #else pda = n; pdu = n; pdvt = n; #endif pdc = n; d_len = n; e_len = n-1; tauq_len = n; taup_len = n; /* Allocate memory */ if (!(a = NAG_ALLOC(m * n, double)) || !(c = NAG_ALLOC(n * n, double)) || !(d = NAG_ALLOC(d_len, double)) || !(e = NAG_ALLOC(e_len, double)) || !(taup = NAG_ALLOC(taup_len, double)) || !(tauq = NAG_ALLOC(tauq_len, double)) || !(u = NAG_ALLOC(m * n, double)) || !(vt = NAG_ALLOC(m * n, double))) { fprintf(fpout, "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) fscanf(fpin, "%lf", &A(i, j)); } fscanf(fpin, "%*[^\n] "); /* Reduce A to bidiagonal form using nag_dgebrd (f08kec). */ nag_dgebrd(order, m, n, a, pda, d, e, tauq, taup, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dgebrd (f08kec).\n%s\n", fail.message); exit_status = 1; goto END; } if (m >= n) { /* Example 1 */ /* Copy A to VT and U */ for (i = 1; i <= n; ++i) { for (j = i; j <= n; ++j) VT(i, j) = A(i, j); } for (i = 1; i <= m; ++i) { for (j = 1; j <= MIN(i, n); ++j) U(i, j) = A(i, j); } /* nag_dorgbr (f08kfc): */ /* Form P**T explicitly, storing the result in VT */ nag_dorgbr(order, Nag_FormP, n, n, m, vt, pdvt, taup, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dorgbr (f08kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* nag_dorgbr (f08kfc): */ /* Form Q explicitly, storing the result in U */ nag_dorgbr(order, Nag_FormQ, m, n, n, u, pdu, tauq, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dorgbr (f08kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* nag_dbdsqr (f08mec): Compute the SVD of A. */ nag_dbdsqr(order, Nag_Upper, n, n, m, 0, d, e, vt, pdvt, u, pdu, c, pdc, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dbdsqr (f08mec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print singular values, left & right singular vectors */ fprintf(fpout, "\n Example 1: singular values\n"); for (i = 1; i <= n; ++i) fprintf(fpout, "%8.4f%s", d[i-1], i%8 == 0?"\n":" "); fprintf(fpout, "\n\n"); /* nag_gen_real_mat_print (x04cac): Print VT. */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, vt, pdvt, "Example 1: right singular vectors, by row", outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); /* nag_gen_real_mat_print (x04cac): Print U. */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, n, u, pdu, "Example 1: left singular vectors, by column", outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } } else { /* Example 2 */ /* Copy A to VT and U */ for (i = 1; i <= m; ++i) { for (j = i; j <= n; ++j) VT(i, j) = A(i, j); } for (i = 1; i <= m; ++i) { for (j = 1; j <= i; ++j) U(i, j) = A(i, j); } /* nag_dorgbr (f08kfc): */ /* Form P**T explicitly, storing the result in VT */ nag_dorgbr(order, Nag_FormP, m, n, m, vt, pdvt, taup, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dorgbr (f08kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* nag_dorgbr (f08kfc): */ /* Form Q explicitly, storing the result in U */ nag_dorgbr(order, Nag_FormQ, m, m, n, u, pdu, tauq, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dorgbr (f08kfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* nag_dbdsqr (f08mec): Compute the SVD of A */ nag_dbdsqr(order, Nag_Lower, m, n, m, 0, d, e, vt, pdvt, u, pdu, c, pdc, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dbdsqr (f08mec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print singular values, left & right singular vectors */ fprintf(fpout, "\n Example 2: singular values\n"); for (i = 1; i <= m; ++i) fprintf(fpout, "%8.4f%s", d[i-1], i%8 == 0?"\n":" "); fprintf(fpout, "\n\n"); /* nag_gen_real_mat_print (x04cac): Print VT */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, n, vt, pdvt, "Example 2: right singular vectors, by row", outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); if (outfile) fclose(fpout); /* nag_gen_real_mat_print (x04cac): print U */ nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, m, u, pdu, "Example 2: left singular vectors, by column", outfile, &fail); if (outfile && !(fpout = fopen(outfile, "a"))) { exit_status = 2; goto END; } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } } END: if (a) NAG_FREE(a); if (c) NAG_FREE(c); if (d) NAG_FREE(d); if (e) NAG_FREE(e); if (taup) NAG_FREE(taup); if (tauq) NAG_FREE(tauq); if (u) NAG_FREE(u); if (vt) NAG_FREE(vt); } if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); return exit_status; } #undef A #undef U #undef VT