/* nag_dbdsqr (f08mec) 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, j, n, pdvt, pdu, d_len, e_len; Integer exit_status = 0; NagError fail; Nag_UploType uplo; Nag_OrderType order; /* Arrays */ char nag_enum_arg[40]; double *c = 0, *d = 0, *e = 0, *u = 0, *vt = 0; 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_dbdsqr (f08mec) Example Program Results\n\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%ld%*[^\n] ", &n); #ifdef NAG_COLUMN_MAJOR #define U(I, J) u[(J-1)*pdu + I - 1] #define VT(I, J) vt[(J-1)*pdvt + I - 1] order = Nag_ColMajor; pdu = n; pdvt = n; #else #define U(I, J) u[(I-1)*pdu + J - 1] #define VT(I, J) vt[(I-1)*pdvt + J - 1] order = Nag_RowMajor; pdu = n; pdvt = n; #endif d_len = n; e_len = n - 1; /* Allocate memory */ if (!(c = NAG_ALLOC(1 * 1, double)) || !(d = NAG_ALLOC(d_len, double)) || !(e = NAG_ALLOC(e_len, double)) || !(u = NAG_ALLOC(n * n, double)) || !(vt = NAG_ALLOC(n * n, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read B from data file */ for (i = 1; i <= n; ++i) fscanf(fpin, "%lf", &d[i - 1]); fscanf(fpin, "%*[^\n] "); for (i = 1; i <= n - 1; ++i) fscanf(fpin, "%lf", &e[i - 1]); fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%s%*[^\n] ", nag_enum_arg); /* nag_enum_name_to_value(x04nac). * Converts NAG enum member name to value */ uplo = (Nag_UploType) nag_enum_name_to_value(nag_enum_arg); /* Initialise U and VT to be the unit matrix */ for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) { U(i, j) = 0.0; VT(i, j) = 0.0; } U(i, i) = 1.0; VT(i, i) = 1.0; } /* Calculate the SVD of B */ /* nag_dbdsqr (f08mec). * SVD of real bidiagonal matrix reduced from real general * matrix */ nag_dbdsqr(order, uplo, n, n, n, 0, d, e, vt, pdvt, u, pdu, c, 1, &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, "\nSingular 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 real general matrix (easy-to-use) */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, vt, pdvt, "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), see above. */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, u, pdu, "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 (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (c) NAG_FREE(c); if (d) NAG_FREE(d); if (e) NAG_FREE(e); if (u) NAG_FREE(u); if (vt) NAG_FREE(vt); return exit_status; }