/* nag_zhgeqz (f08xsc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; char *outfile = 0; /* Scalars */ Integer i, ihi, ilo, irows, j, n, pda, pdb; Integer alpha_len, beta_len, scale_len, tau_len; Integer exit_status = 0; NagError fail; Nag_OrderType order; /* Arrays */ Complex *a = 0, *alpha = 0, *b = 0, *beta = 0, *q = 0, *tau = 0; Complex *z = 0; Complex e; double *lscale = 0, *rscale = 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 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_zhgeqz (f08xsc) Example Program Results\n\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%ld%*[^\n] ", &n); #ifdef NAG_COLUMN_MAJOR pda = n; pdb = n; #else pda = n; pdb = n; #endif alpha_len = n; beta_len = n; scale_len = n; tau_len = n; /* Allocate memory */ if (!(a = NAG_ALLOC(n * n, Complex)) || !(alpha = NAG_ALLOC(alpha_len, Complex)) || !(b = NAG_ALLOC(n * n, Complex)) || !(beta = NAG_ALLOC(beta_len, Complex)) || !(q = NAG_ALLOC(1 * 1, Complex)) || !(tau = NAG_ALLOC(tau_len, Complex)) || !(lscale = NAG_ALLOC(scale_len, double)) || !(rscale = NAG_ALLOC(scale_len, double)) || !(z = NAG_ALLOC(1 * 1, Complex))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* READ matrix A from data file */ for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) fscanf(fpin, " ( %lf, %lf ) ", &A(i, j).re, &A(i, j).im); } fscanf(fpin, "%*[^\n] "); /* READ matrix B from data file */ for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) fscanf(fpin, " ( %lf, %lf ) ", &B(i, j).re, &B(i, j).im); } fscanf(fpin, "%*[^\n] "); /* Balance matrix pair (A,B) */ /* nag_zggbal (f08wvc). * Balance a pair of complex general matrices */ nag_zggbal(order, Nag_DoBoth, n, a, pda, b, pdb, &ilo, &ihi, lscale, rscale, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_zggbal (f08wvc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Matrix A after balancing */ /* nag_gen_complx_mat_print_comp (x04dbc). * Print complex general matrix (comprehensive) */ if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, a, pda, Nag_BracketForm, "%7.4f", "Matrix A after balancing", 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"); /* Matrix B after balancing */ /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, b, pdb, Nag_BracketForm, "%7.4f", "Matrix B after balancing", 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"); /* Reduce B to triangular form using QR */ irows = ihi + 1 - ilo; /* nag_zgeqrf (f08asc). * QR factorization of complex general rectangular matrix */ nag_zgeqrf(order, irows, irows, &B(ilo, ilo), pdb, tau, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_zgeqrf (f08asc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Apply the orthogonal transformation to matrix A */ /* nag_zunmqr (f08auc). * Apply unitary transformation determined by nag_zgeqrf * (f08asc) or nag_zgeqpf (f08bsc) */ nag_zunmqr(order, Nag_LeftSide, Nag_ConjTrans, irows, irows, irows, &B(ilo, ilo), pdb, tau, &A(ilo, ilo), pda, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_zunmqr (f08auc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Compute the generalized Hessenberg form of (A,B) */ /* nag_zgghrd (f08wsc). * Unitary reduction of a pair of complex general matrices * to generalized upper Hessenberg form */ nag_zgghrd(order, Nag_NotQ, Nag_NotZ, irows, 1, irows, &A(ilo, ilo), pda, &B(ilo, ilo), pdb, q, 1, z, 1, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_zgghrd (f08wsc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Matrix A in generalized Hessenberg form */ /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, a, pda, Nag_BracketForm, "%7.3f", "Matrix A in Hessenberg form", 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"); /* Matrix B in generalized Hessenberg form */ /* nag_gen_complx_mat_print_comp (x04dbc), see above. */ if (outfile) fclose(fpout); nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, b, pdb, Nag_BracketForm, "%7.3f", "Matrix B is triangular", 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; } /* Compute the generalized Schur form */ /* nag_zhgeqz (f08xsc). * Eigenvalues and generalized Schur factorization of * complex generalized upper Hessenberg form reduced from a * pair of complex general matrices */ nag_zhgeqz(order, Nag_EigVals, Nag_NotQ, Nag_NotZ, n, ilo, ihi, a, pda, b, pdb, alpha, beta, q, 1, z, 1, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_zhgeqz (f08xsc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print the generalized eigenvalues */ fprintf(fpout, "\n Generalized eigenvalues\n"); for (i = 0; i < n; ++i) { if (beta[i].re != 0.0 || beta[i].im != 0.0) { /* nag_complex_divide (a02cdc). * Quotient of two complex numbers */ e = nag_complex_divide(alpha[i], beta[i]); fprintf(fpout, " %4ld (%7.3f,%7.3f)\n", i+1, e.re, e.im); } else fprintf(fpout, " %4ld Infinite eigenvalue\n", i+1); } 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 (lscale) NAG_FREE(lscale); if (q) NAG_FREE(q); if (rscale) NAG_FREE(rscale); if (tau) NAG_FREE(tau); if (z) NAG_FREE(z); return exit_status; }