/* nag_dgebal (f08nhc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include int main(void) { /* Scalars */ Integer i, ihi, ilo, j, m, n, pda, pdh, pdvr; Integer scale_len, tau_len, wi_len, wr_len; Integer exit_status=0; NagError fail; Nag_OrderType order; /* Arrays */ double *a=0, *h=0, *scale=0, *tau=0, *vl=0, *vr=0, *wi=0, *wr=0; Boolean *select=0; #ifdef NAG_COLUMN_MAJOR #define A(I,J) a[(J-1)*pda + I - 1] #define H(I,J) h[(J-1)*pdh + I - 1] #define VR(I,J) vr[(J-1)*pdvr + I - 1] order = Nag_ColMajor; #else #define A(I,J) a[(I-1)*pda + J - 1] #define H(I,J) h[(I-1)*pdh + J - 1] #define VR(I,J) vr[(I-1)*pdvr + J - 1] order = Nag_RowMajor; #endif INIT_FAIL(fail); Vprintf("f08nhc Example Program Results\n\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); Vscanf("%ld%*[^\n] ", &n); #ifdef NAG_COLUMN_MAJOR pda = n; pdh = n; pdvr = n; #else pda = n; pdh = n; pdvr = n; #endif scale_len = n; tau_len = n; wi_len = n; wr_len = n; /* Allocate memory */ if ( !(a = NAG_ALLOC(n * n, double)) || !(h = NAG_ALLOC(n * n, double)) || !(scale = NAG_ALLOC(scale_len, double)) || !(tau = NAG_ALLOC(tau_len, double)) || !(vl = NAG_ALLOC(1 * 1, double)) || !(vr = NAG_ALLOC(n * n, double)) || !(wi = NAG_ALLOC(wi_len, double)) || !(wr = NAG_ALLOC(wr_len, double)) || !(select = NAG_ALLOC(1, Boolean)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } /* Read A from data file */ for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) Vscanf("%lf", &A(i,j)); } Vscanf("%*[^\n] "); /* Balance A */ f08nhc(order, Nag_DoBoth, n, a, pda, &ilo, &ihi, scale, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08nhc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Reduce A to upper Hessenberg form H = (Q**T)*A*Q */ f08nec(order, n, ilo, ihi, a, pda, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08nec.\n%s\n", fail.message); exit_status = 1; goto END; } /* Copy A to H and VR */ for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) { H(i,j) = A(i,j); VR(i,j) = A(i,j); } } /* Form Q explicitly, storing the result in VR */ f08nfc(order, n, 1, n, vr, pdvr, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08nfc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Calculate the eigenvalues and Schur factorization of A */ f08pec(order, Nag_Schur, Nag_UpdateZ, n, ilo, ihi, h, pdh, wr, wi, vr, pdvr, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08pec.\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf(" Eigenvalues\n"); for (i = 1; i <= n; ++i) Vprintf("(%8.4f,%8.4f)\n", wr[i-1], wi[i-1]); /* Calculate the eigenvectors of A, storing the result in VR */ f08qkc(order, Nag_RightSide, Nag_BackTransform, select, n, h, pdh, vl, 1, vr, pdvr, n, &m, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08qkc.\n%s\n", fail.message); exit_status = 1; goto END; } f08njc(order, Nag_DoBoth, Nag_RightSide, n, ilo, ihi, scale, m, vr, pdvr, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08njc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Print eigenvectors */ Vprintf("\n"); x04cac(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m, vr, pdvr, "Contents of array VR", 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from x04cac.\n%s\n", fail.message); exit_status = 1; goto END; } END: if (a) NAG_FREE(a); if (h) NAG_FREE(h); if (scale) NAG_FREE(scale); if (tau) NAG_FREE(tau); if (vl) NAG_FREE(vl); if (vr) NAG_FREE(vr); if (wi) NAG_FREE(wi); if (wr) NAG_FREE(wr); if (select) NAG_FREE(select); return exit_status; }