/* nag_ztgevc (f08yxc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include #include int main(void) { /* 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; Complex e; Boolean ileft, iright; NagError fail; Nag_OrderType order; /* Arrays */ Complex *a=0, *alpha=0, *b=0, *beta=0, *q=0, *tau=0, *z=0; double *lscale=0, *rscale=0; 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); Vprintf("f08yxc Example Program Results\n\n"); /* ILEFT is TRUE if left eigenvectors are required */ /* IRIGHT is TRUE if right eigenvectors are required */ ileft = TRUE; iright = TRUE; /* Skip heading in data file */ Vscanf("%*[^\n] "); Vscanf("%ld%*[^\n] ", &n); #ifdef NAG_COLUMN_MAJOR pda = n; pdb = n; pdq = n; pdz = n; #else pda = n; pdb = n; pdq = n; pdz = n; #endif alpha_len = n; beta_len = n; scale_len = n; tau_len = n; select_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)) || !(lscale = NAG_ALLOC(scale_len, double)) || !(rscale = NAG_ALLOC(scale_len, double)) || !(q = NAG_ALLOC(n * n, Complex)) || !(tau = NAG_ALLOC(tau_len, Complex)) || !(z = NAG_ALLOC(n * n, Complex)) || !(select = NAG_ALLOC(select_len, Boolean)) ) { Vprintf("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) Vscanf(" ( %lf , %lf )", &A(i,j).re, &A(i,j).im); } Vscanf("%*[^\n] "); /* READ matrix B from data file */ for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) Vscanf(" ( %lf , %lf )", &B(i,j).re, &B(i,j).im); } Vscanf("%*[^\n] "); /* Balance matrix pair (A,B) */ f08wvc(order, Nag_DoBoth, n, a, pda, b, pdb, &ilo, &ihi, lscale, rscale, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08wvc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Matrix A after balancing */ x04dbc(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, 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from x04dbc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Matrix B after balancing */ x04dbc(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, 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from x04dbc.\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n"); /* Reduce B to triangular form using QR */ irows = ihi + 1 - ilo; icols = n + 1 - ilo; f08asc(order, irows, icols, &B(ilo, ilo), pdb, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08asc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Apply the orthogonal transformation to matrix A */ f08auc(order, Nag_LeftSide, Nag_ConjTrans, irows, icols, irows, &B(ilo, ilo), pdb, tau, &A(ilo, ilo), pda, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08auc.\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).re = 0.0; Q(i,j).im = 0.0; } Q(i,i).re = 1.0; } for (i = ilo+1; i <= ilo+irows-1; ++i) { for (j = ilo; j <= MIN(i,ilo+irows-2); ++j) { Q(i,j).re = B(i,j).re; Q(i,j).im = B(i,j).im; } } f08atc(order, irows, irows, irows, &Q(ilo, ilo), pdq, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08atc.\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).re = 0.0; Z(i,j).im = 0.0; } Z(i,i).re = 1.0; } } /* Compute the generalized Hessenberg form of (A,B) */ f08wsc(order, Nag_UpdateSchur, Nag_UpdateZ, n, ilo, ihi, a, pda, b, pdb, q, pdq, z, pdz, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08wsc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Matrix A in generalized Hessenberg form */ x04dbc(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, 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from x04dbc.\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n"); /* Matrix B in generalized Hessenberg form */ x04dbc(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, b, pdb, Nag_BracketForm, "%7.3f", "Matrix B in Hessenberg form", Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from x04dbc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Compute the generalized Schur form */ /* The Schur form also gives parameters */ /* required to compute generalized eigenvalues */ f08xsc(order, Nag_Schur, Nag_AccumulateQ, Nag_AccumulateZ, n, ilo, ihi, a, pda, b, pdb, alpha, beta, q, pdq, z, pdz, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08xsc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Print the generalized eigenvalue parameters */ Vprintf("\n Generalized eigenvalues\n"); for (i = 1; i <= n; ++i) { if (beta[i-1].re != 0.0 || beta[i-1].im != 0.0) { e = a02cdc(alpha[i - 1], beta[i - 1]); Vprintf(" %4ld (%7.3f,%7.3f)\n", i, e.re, e.im); } else Vprintf(" %4ldEigenvalue is infinite\n", i); } Vprintf("\n"); /* Compute left and right generalized eigenvectors */ /* of the balanced matrix */ f08yxc(order, Nag_BothSides, Nag_BackTransform, select, n, a, pda, b, pdb, q, pdq, z, pdz, n, &m, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08yxc.\n%s\n", fail.message); exit_status = 1; goto END; } if (iright) { /* Compute right eigenvectors of the original matrix */ f08wwc(order, Nag_DoBoth, Nag_RightSide, n, ilo, ihi, lscale, rscale, n, z, pdz, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08wwc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Print the right eigenvectors */ x04dbc(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, z, pdz, Nag_BracketForm, "%7.4f", "Right eigenvectors", Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from x04dbc.\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n"); } /* Compute left eigenvectors of the original matrix */ if (ileft) { f08wwc(order, Nag_DoBoth, Nag_LeftSide, n, ilo, ihi, lscale, rscale, n, q, pdq, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08wwc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Print the left eigenvectors */ x04dbc(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, q, pdq, Nag_BracketForm, "%7.4f", "Left eigenvectors", Nag_IntegerLabels, 0, Nag_IntegerLabels, 0, 80, 0, 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from x04dbc.\n%s\n", fail.message); exit_status = 1; goto END; } } END: 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); if (select) NAG_FREE(select); return exit_status; }