/* nag_zggsvd (f08vnc) 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 */ char *clabs = 0, *rlabs = 0; Complex *a = 0, *b = 0, *q = 0, *u = 0, *v = 0; double *alpha = 0, *beta = 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_subscr(a_1,a_2) (a_2)*10 + a_1 - 11 #define a_ref(a_1,a_2) a[a_subscr(a_1,a_2)] #define b_subscr(a_1,a_2) (a_2)*10 + a_1 - 11 #define b_ref(a_1,a_2) b[b_subscr(a_1,a_2)] */ /* .. 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_zggsvd (f08vnc) 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 (!(clabs = NAG_ALLOC(2, char)) || !(rlabs = NAG_ALLOC(2, char)) || !(a = NAG_ALLOC(m*n, Complex)) || !(b = NAG_ALLOC(p*n, Complex)) || !(q = NAG_ALLOC(n*n, Complex)) || !(u = NAG_ALLOC(m*m, Complex)) || !(v = NAG_ALLOC(p*p, Complex)) || !(alpha = NAG_ALLOC(n, double)) || !(beta = NAG_ALLOC(n, 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 , %lf )", &A(i, j).re, &A(i, j).im); } } fscanf(fpin, "%*[^\n] "); for (i = 1; i <= p; ++i) { for (j = 1; j <= n; ++j) { fscanf(fpin, " ( %lf , %lf )", &B(i, j).re, &B(i, j).im); } } fscanf(fpin, "%*[^\n] "); /* Compute the generalized singular value decomposition of (A, B) */ /* (A = U*D1*(0 R)*(Q**H), B = V*D2*(0 R)*(Q**H), m.ge.n) */ nag_zggsvd(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, "Number of finite generalized singular values (L)\n"); fprintf(fpout, "%5ld\n", l); fprintf(fpout, "Numerical rank of (A**H B**H)**H (K+L)\n"); fprintf(fpout, "%5ld\n\n", irank); fprintf(fpout, "Finite 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_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m, m, u, pdu, Nag_BracketForm, "%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_complx_mat_print_comp (x04dbc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, p, p, v, pdv, Nag_BracketForm, "%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_complx_mat_print_comp (x04dbc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, q, pdq, Nag_BracketForm, "%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_complx_mat_print_comp (x04dbc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\n"); if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(order, Nag_UpperMatrix, Nag_NonUnitDiag, irank, irank, &A(1, n - irank + 1), pda, Nag_BracketForm, "%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_complx_mat_print_comp (x04dbc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Call ZTRCON (F07TUF) to estimate the reciprocal condition */ /* number of R */ nag_ztrcon(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_ztrcon (f07tuc).\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\n"); fprintf(fpout, "%11.1e\n", serrbd); } else { fprintf(fpout, "(A**H B**H)**H is not of full rank\n"); } } else { fprintf(fpout, "Failure in nag_zggsvd (f08vnc)\n"); } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (clabs) NAG_FREE(clabs); if (rlabs) NAG_FREE(rlabs); if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (q) NAG_FREE(q); if (u) NAG_FREE(u); if (v) NAG_FREE(v); if (alpha) NAG_FREE(alpha); if (beta) NAG_FREE(beta); return exit_status; } #undef B #undef A