/* nag_dtgevc (f08ykc) 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, icols, ihi, ilo, irows, j, m, n, pda, pdb, pdq, pdz; Integer alpha_len, beta_len, scale_len, tau_len, select_len; Integer exit_status = 0; Nag_Boolean ileft, iright; NagError fail; Nag_OrderType order; /* Arrays */ double *a = 0, *alphai = 0, *alphar = 0, *b = 0, *beta = 0; double *lscale = 0, *q = 0, *rscale = 0, *tau = 0, *z = 0; Nag_Boolean *select = 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] #define Q(I, J) q[(J-1)*pdq + I - 1] #define Z(I, J) z[(J-1)*pdz + 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] #define Q(I, J) q[(I-1)*pdq + J - 1] #define Z(I, J) z[(I-1)*pdz + 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_dtgevc (f08ykc) Example Program Results\n\n"); /* ileft is Nag_TRUE if left eigenvectors are required */ /* iright is Nag_TRUE if right eigenvectors are required */ ileft = Nag_TRUE; iright = Nag_TRUE; /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%ld %*[^\n] ", &n); pda = n; pdb = n; pdq = n; pdz = n; alpha_len = n; beta_len = n; scale_len = n; tau_len = n; select_len = n; /* Allocate memory */ if ( !(a = NAG_ALLOC(n * n, double)) || !(alphai = NAG_ALLOC(alpha_len, double)) || !(alphar = NAG_ALLOC(alpha_len, double)) || !(b = NAG_ALLOC(n * n, double)) || !(beta = NAG_ALLOC(beta_len, double)) || !(lscale = NAG_ALLOC(scale_len, double)) || !(rscale = NAG_ALLOC(scale_len, double)) || !(q = NAG_ALLOC(n * n, double)) || !(tau = NAG_ALLOC(tau_len, double)) || !(z = NAG_ALLOC(n * n, double)) || !(select = NAG_ALLOC(select_len, Nag_Boolean))) { 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", &A(i, j)); } fscanf(fpin, "%*[^\n] "); /* READ matrix B from data file */ for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) fscanf(fpin, "%lf", &B(i, j)); } fscanf(fpin, "%*[^\n] "); /* Balance matrix pair (A,B) */ /* nag_dggbal (f08whc). * Balance a pair of real general matrices */ nag_dggbal(order, Nag_DoBoth, n, a, pda, b, pdb, &ilo, &ihi, lscale, rscale, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dggbal (f08whc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Matrix A after balancing */ /* 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, a, pda, "Matrix A after balancing", 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; } /* Matrix B after balancing */ /* nag_gen_real_mat_print (x04cac), see above. */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, b, pdb, "Matrix B after balancing", 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"); /* Reduce B to triangular form using QR */ irows = ihi + 1 - ilo; icols = n + 1 - ilo; /* nag_dgeqrf (f08aec). * QR factorization of real general rectangular matrix */ nag_dgeqrf(order, irows, icols, &B(ilo, ilo), pdb, tau, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dgeqrf (f08aec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Apply the orthogonal transformation to matrix A - nag_dormqr (f08agc) * as determined by nag_dgeqrf (f08aec) or nag_dgeqpf (f08bec). */ nag_dormqr(order, Nag_LeftSide, Nag_Trans, irows, icols, irows, &B(ilo, ilo), pdb, tau, &A(ilo, ilo), pda, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dormqr (f08agc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Initialize Q (if left eigenvectors are required) */ if (ileft) { for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) Q(i, j) = 0.0; Q(i, i) = 1.0; } for (i = ilo+1; i <= ilo+irows-1; ++i) { for (j = ilo; j <= MIN(i, ilo+irows-2); ++j) Q(i, j) = B(i, j); } /* nag_dorgqr (f08afc). * Form all or part of orthogonal Q from QR factorization * determined by nag_dgeqrf (f08aec) or nag_dgeqpf (f08bec) */ nag_dorgqr(order, irows, irows, irows, &Q(ilo, ilo), pdq, tau, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dorgqr (f08afc).\n%s\n", fail.message); exit_status = 1; goto END; } } /* Initialize Z (if right eigenvectors are required) */ if (iright) { for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) Z(i, j) = 0.0; Z(i, i) = 1.0; } } /* Compute the generalized Hessenberg form of (A,B) */ /* nag_dgghrd (f08wec). * Orthogonal reduction of a pair of real general matrices * to generalized upper Hessenberg form */ nag_dgghrd(order, Nag_UpdateSchur, Nag_UpdateZ, n, ilo, ihi, a, pda, b, pdb, q, pdq, z, pdz, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dgghrd (f08wec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Matrix A in generalized Hessenberg form */ /* nag_gen_real_mat_print (x04cac), see above. */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, a, pda, "Matrix A in Hessenberg form", 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"); /* Matrix B in generalized Hessenberg form */ /* nag_gen_real_mat_print (x04cac), see above. */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, b, pdb, "Matrix B in Hessenberg form", 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; } /* nag_dhgeqz (f08xec). * Eigenvalues and generalized Schur factorization of real * generalized upper Hessenberg form reduced from a pair of * real general matrices. */ nag_dhgeqz(order, Nag_Schur, Nag_AccumulateQ, Nag_AccumulateZ, n, ilo, ihi, a, pda, b, pdb, alphar, alphai, beta, q, pdq, z, pdz, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dhgeqz (f08xec).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print the generalized eigenvalue parameters */ fprintf(fpout, "\n Generalized eigenvalues\n"); for (i = 1; i <= n; ++i) { if (beta[i-1] != 0.0) { fprintf(fpout, " %4ld (%7.3f,%7.3f)\n", i, alphar[i-1]/beta[i-1], alphai[i-1]/beta[i-1]); } else fprintf(fpout, " %4ldEigenvalue is infinite\n", i); } fprintf(fpout, "\n"); /* Compute left and right generalized eigenvectors * of the balanced matrix - nag_dtgevc (f08ykc). */ nag_dtgevc(order, Nag_BothSides, Nag_BackTransform, select, n, a, pda, b, pdb, q, pdq, z, pdz, n, &m, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dtgevc (f08ykc).\n%s\n", fail.message); exit_status = 1; goto END; } if (iright) { /* Compute right eigenvectors of the original matrix pair * supplied tonag_dggbal (f08whc) using nag_dggbak (f08wjc). */ nag_dggbak(order, Nag_DoBoth, Nag_RightSide, n, ilo, ihi, lscale, rscale, n, z, pdz, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dggbak (f08wjc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print the right eigenvectors */ /* nag_gen_real_mat_print (x04cac), see above. */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, z, pdz, "Right eigenvectors", 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"); } /* Compute left eigenvectors of the original matrix */ if (ileft) { /* nag_dggbak (f08wjc), see above. */ nag_dggbak(order, Nag_DoBoth, Nag_LeftSide, n, ilo, ihi, lscale, rscale, n, q, pdq, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_dggbak (f08wjc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print the left eigenvectors */ /* nag_gen_real_mat_print (x04cac), see above. */ if (outfile) fclose(fpout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, q, pdq, "Left eigenvectors", 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 (a) NAG_FREE(a); if (alphai) NAG_FREE(alphai); if (alphar) NAG_FREE(alphar); 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); if (select) NAG_FREE(select); return exit_status; }