/* nag_dggsvd (f08vac) Example Program. * * Copyright 2004 Numerical Algorithms Group. * * Mark 8, 2008. */ #include #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; char *outfile = 0; /* System generated locals */ double d__1; /* Scalars */ double eps, rcond, serrbd; Integer exit_status = 0, i, irank, j, k, l, m, n, p, pda, pdb, pdq, pdu, pdv; NagError fail; Nag_OrderType order; /* Arrays */ double *a = 0, *alpha = 0, *b = 0, *beta = 0, *q = 0, *u = 0, *v = 0; #ifdef NAG_COLUMN_MAJOR #define A(I, J) a[(J-1)*pda + I - 1] #define B(I, J) b[(J-1)*pdb + I - 1] order = Nag_ColMajor; #else #define A(I, J) a[(I-1)*pda + J - 1] #define B(I, J) b[(I-1)*pdb + J - 1] order = Nag_RowMajor; #endif /*#define a_ref(a_1,a_2) a[(a_2)*10 + a_1 - 11] #define b_ref(a_1,a_2) b[(a_2)*10 + a_1 - 11] */ /* .. External Functions .. */ 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_dggsvd (f08vac) Example Program Results\n\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%ld%ld%ld%*[^\n] ", &m, &n, &p); if (m <= 10 && n <= 10 && p <= 10) { /* Allocate memory */ if (!(a = NAG_ALLOC(m*n, double)) || !(alpha = NAG_ALLOC(n, double)) || !(b = NAG_ALLOC(p*n, double)) || !(beta = NAG_ALLOC(n, double)) || !(q = NAG_ALLOC(n*n, double)) || !(u = NAG_ALLOC(m*m, double)) || !(v = NAG_ALLOC(p*p, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } #ifdef NAG_COLUMN_MAJOR pda = m; pdb = p; pdq = n; pdu = m; pdv = p; #else pda = n; pdb = n; pdq = n; pdu = m; pdv = p; #endif } else { fprintf(fpout, "%s\n", "MMAX and/or NMAX too small"); } /* Read the m by n matrix A and p by n matrix B from data file */ for (i = 1; i <= m; ++i) { for (j = 1; j <= n; ++j) { fscanf(fpin, "%lf", &A(i, j)); } } fscanf(fpin, "%*[^\n] "); for (i = 1; i <= p; ++i) { for (j = 1; j <= n; ++j) { fscanf(fpin, "%lf", &B(i, j)); } } fscanf(fpin, "%*[^\n] "); /* Compute the generalized singular value decomposition of (A, B) */ /* (A = U*D1*(0 R)*(Q**T), B = V*D2*(0 R)*(Q**T), m.ge.n) */ nag_dggsvd(order, Nag_AllU, Nag_ComputeV, Nag_ComputeQ, m, n, p, &k, &l, a, pda, b, pdb, alpha, beta, u, pdu, v, pdv, q, pdq, &fail); if (fail.code == NE_NOERROR) { /* Print solution */ irank = k + l; fprintf(fpout, "Number of infinite generalized singular values (K)\n"); fprintf(fpout, "%5ld\n", k); fprintf(fpout, "\nNumber of finite generalized singular values (L)\n"); fprintf(fpout, "%5ld\n", l); fprintf(fpout, "Numerical rank of (A**T B**T)**T (K+L)\n"); fprintf(fpout, "%5ld\n", irank); fprintf(fpout, "\nFinite generalized singular values\n"); for (j = k + 1; j <= irank; ++j) { d__1 = alpha[j - 1] / beta[j - 1]; fprintf(fpout, "%13.4e%s", d__1, j%8 == 0 || j == irank?"\n":" "); } fprintf(fpout, "\n"); if (outfile) fclose(fpout); nag_gen_real_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, m, u, pdu, "%13.4e", "Orthogonal matrix U", Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 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_comp (x04cbc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); if (outfile) fclose(fpout); nag_gen_real_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, p, p, v, pdv, "%13.4e", "Orthogonal matrix V", Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 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_comp (x04cbc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); if (outfile) fclose(fpout); nag_gen_real_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, q, pdq, "%13.4e", "Orthogonal matrix Q", Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 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_comp (x04cbc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); if (outfile) fclose(fpout); nag_gen_real_mat_print_comp(order, Nag_UpperMatrix, Nag_NonUnitDiag, irank, irank, &A(1, n - irank + 1), pda, "%13.4e", "Non singular upper triangular matrix R", Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 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_comp (x04cbc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Call DTRCON (F07TGF) to estimate the reciprocal condition */ /* number of R */ nag_dtrcon(order, Nag_InfNorm, Nag_Upper, Nag_NonUnitDiag, irank, &A(1, n - irank + 1), pda, &rcond, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dtrcon (f07tgc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\nEstimate of reciprocal condition number for R\n"); fprintf(fpout, "%11.1e\n\n", rcond); /* So long as IRANK = N, get the machine precision, EPS, and */ /* compute the approximate error bound for the computed */ /* generalized singular values */ if (irank == n) { eps = nag_machine_precision; serrbd = eps / rcond; fprintf(fpout, "Error estimate for the generalized singular values"); fprintf(fpout, "\n%11.1e\n", serrbd); } else { fprintf(fpout, "(A**T B**T)**T is not of full rank\n"); } } else { fprintf(fpout, "Failure in nag_dggsvd (f08vac)\n"); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (a) NAG_FREE(a); if (alpha) NAG_FREE(alpha); if (b) NAG_FREE(b); if (beta) NAG_FREE(beta); if (q) NAG_FREE(q); if (u) NAG_FREE(u); if (v) NAG_FREE(v); return exit_status; } #undef B #undef A