/* nag_dgeevx (f08nbc) Example Program. * * Copyright 2011 Numerical Algorithms Group. * * Mark 23, 2011. */ #include #include #include #include #include #include int main(void) { /* Scalars */ double abnrm, eps, rcnd, tol; Integer exit_status = 0, i, ihi, ilo, j, n, pda, pdvr, pdvl; Complex eig; /* Arrays */ double *a = 0, *rconde = 0, *rcondv = 0, *scale = 0, *vl = 0, *vr = 0; double *wi = 0, *wr = 0; /* Nag Types */ NagError fail; Nag_OrderType order; #ifdef NAG_COLUMN_MAJOR #define A(I, J) a[(J - 1) * pda + I - 1] #define VR(I, J) vr[(J) * pdvr + I] order = Nag_ColMajor; #else #define A(I, J) a[(I - 1) * pda + J - 1] #define VR(I, J) vr[(I) * pdvr + J] order = Nag_RowMajor; #endif INIT_FAIL(fail); printf("nag_dgeevx (f08nbc) Example Program Results\n"); /* Skip heading in data file */ scanf("%*[^\n]"); scanf("%ld%*[^\n]", &n); pda = n; pdvr = n; pdvl = n; /* Allocate memory */ if (!(a = NAG_ALLOC(n * n, double)) || !(rconde = NAG_ALLOC(n, double)) || !(rcondv = NAG_ALLOC(n, double)) || !(scale = NAG_ALLOC(n, double)) || !(vl = NAG_ALLOC(n * n, double)) || !(vr = NAG_ALLOC(n * n, double)) || !(wi = NAG_ALLOC(n, double)) || !(wr = NAG_ALLOC(n, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } /* Read the matrix A from data file */ for (i = 1; i <= n; ++i) for (j = 1; j <= n; ++j) scanf("%lf", &A(i, j)); scanf("%*[^\n]"); /* Solve the eigenvalue problem using nag_dgeevx (f08nbc). */ nag_dgeevx(order, Nag_BalanceBoth, Nag_LeftVecs, Nag_RightVecs, Nag_RCondBoth, n, a, pda, wr, wi, vl, pdvl, vr, pdvr, &ilo, &ihi, scale, &abnrm, rconde, rcondv, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_dgeevx (f08nbc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Compute the machine precision */ eps = nag_machine_precision; tol = eps * abnrm; /* Print the eigenvalues/vectors, associated condition number and bounds. */ for (j = 0; j < n; ++j) { /* Print information on jth eigenvalue */ printf("\n\nEigenvalue %3ld%14s= ", j+1, ""); if (wi[j] == 0.) printf("%12.4e\n", wr[j]); else printf("(%11.4e, %11.4e)\n", wr[j], wi[j]); rcnd = rconde[j]; printf("\nReciprocal condition number = %9.1e\n", rcnd); if (rcnd > 0.0) printf("Error bound = %9.1e\n", tol/rcnd); else printf("Error bound is infinite\n"); /* Normalize and print information on jth eigenvector */ printf("\nEigenvector %2ld\n", j+1); if (wi[j] == 0.0) for (i = 0; i < n; ++i) printf("%29s%13.4e\n", "", VR(i, j)/ VR(n-1, j)); else if (wi[j] > 0.) for (i = 0; i < n; ++i) { eig = nag_complex_divide(nag_complex(VR(i, j), VR(i, j + 1)), nag_complex(VR(n-1, j), VR(n-1, j + 1))); printf("%30s(%11.4e, %11.4e)\n", "", eig.re, eig.im); } else for (i = 0; i < n; ++i) { eig = nag_complex_divide(nag_complex(VR(i, j - 1), VR(i, j)), nag_complex(VR(n-1, j - 1), VR(n-1, j))); printf("%30s(%11.4e, %11.4e)\n", "", eig.re, -eig.im); } rcnd = rcondv[j]; printf("\nReciprocal condition number = %9.1e\n", rcnd); if (rcnd > 0.0) printf("Error bound = %9.1e\n", tol/rcnd); else printf("Error bound is infinite\n"); } END: if (a) NAG_FREE(a); if (rconde) NAG_FREE(rconde); if (rcondv) NAG_FREE(rcondv); if (scale) NAG_FREE(scale); if (vl) NAG_FREE(vl); if (vr) NAG_FREE(vr); if (wi) NAG_FREE(wi); if (wr) NAG_FREE(wr); return exit_status; } #undef A #undef VR