/* nag_dbdsqr (f08mec) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include int main(void) { /* 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 uplo_char[2]; double *c=0, *d=0, *e=0, *u=0, *vt=0; INIT_FAIL(fail); Vprintf("nag_dbdsqr (f08mec) Example Program Results\n\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); Vscanf("%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)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } /* Read B from data file */ for (i = 1; i <= n; ++i) Vscanf("%lf", &d[i-1]); Vscanf("%*[^\n] "); for (i = 1; i <= n-1; ++i) Vscanf("%lf", &e[i-1]); Vscanf("%*[^\n] "); Vscanf(" ' %1s '%*[^\n] ", uplo_char); if (*(unsigned char *)uplo_char == 'L') uplo = Nag_Lower; else if (*(unsigned char *)uplo_char == 'U') uplo = Nag_Upper; else { Vprintf("Unrecognised character for Nag_UploType type\n"); exit_status = -1; goto END; } /* 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) { Vprintf("Error from nag_dbdsqr (f08mec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print singular values, left & right singular vectors */ Vprintf("\nSingular values\n"); for (i = 1; i <= n; ++i) Vprintf("%8.4f%s", d[i-1], i%8==0 ?"\n":" "); Vprintf("\n\n"); /* nag_gen_real_mat_print (x04cac). * Print real general matrix (easy-to-use) */ nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, vt, pdvt, "Right singular vectors, by row", 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n"); /* nag_gen_real_mat_print (x04cac), see above. */ nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, u, pdu, "Left singular vectors, by column", 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } END: 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; }