/* 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; Nag_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("nag_dgebal (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, Nag_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 */ /* nag_dgebal (f08nhc). * Balance real general matrix */ nag_dgebal(order, Nag_DoBoth, n, a, pda, &ilo, &ihi, scale, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dgebal (f08nhc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Reduce A to upper Hessenberg form H = (Q**T)*A*Q */ /* nag_dgehrd (f08nec). * Orthogonal reduction of real general matrix to upper * Hessenberg form */ nag_dgehrd(order, n, ilo, ihi, a, pda, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dgehrd (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 */ /* nag_dorghr (f08nfc). * Generate orthogonal transformation matrix from reduction * to Hessenberg form determined by nag_dgehrd (f08nec) */ nag_dorghr(order, n, 1, n, vr, pdvr, tau, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dorghr (f08nfc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Calculate the eigenvalues and Schur factorization of A */ /* nag_dhseqr (f08pec). * Eigenvalues and Schur factorization of real upper * Hessenberg matrix reduced from real general matrix */ nag_dhseqr(order, Nag_Schur, Nag_UpdateZ, n, ilo, ihi, h, pdh, wr, wi, vr, pdvr, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dhseqr (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 */ /* nag_dtrevc (f08qkc). * Left and right eigenvectors of real upper * quasi-triangular matrix */ nag_dtrevc(order, Nag_RightSide, Nag_BackTransform, select, n, h, pdh, vl, 1, vr, pdvr, n, &m, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dtrevc (f08qkc).\n%s\n", fail.message); exit_status = 1; goto END; } /* nag_dgebak (f08njc). * Transform eigenvectors of real balanced matrix to those * of original matrix supplied to nag_dgebal (f08nhc) */ nag_dgebak(order, Nag_DoBoth, Nag_RightSide, n, ilo, ihi, scale, m, vr, pdvr, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_dgebak (f08njc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print eigenvectors */ Vprintf("\n"); /* nag_gen_real_mat_print (x04cac). * Print real general matrix (easy-to-use) */ nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, m, vr, pdvr, "Contents of array VR", 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_gen_real_mat_print (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; }