/* nag_dormhr (f08ngc) Example Program. * * Copyright 2001 Numerical Algorithms Group. * * Mark 7, 2001. */ #include #include #include #include #include int main(void) { /* Scalars */ Integer i, j, m, n, pda, pdh, pdvl, pdvr, pdz; Integer tau_len, ifaill_len, ifailr_len, select_len, w_len; Integer exit_status=0; double thresh; NagError fail; Nag_OrderType order; /* Arrays */ double *a=0, *h=0, *vl=0, *vr=0, *z=0, *wi=0, *wr=0, *tau=0; Integer *ifaill=0, *ifailr=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] order = Nag_ColMajor; #else #define A(I,J) a[(I-1)*pda + J - 1] #define H(I,J) h[(I-1)*pdh + J - 1] order = Nag_RowMajor; #endif INIT_FAIL(fail); Vprintf("f08ngc Example Program Results\n\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); Vscanf("%ld%*[^\n] ", &n); #ifdef NAG_COLUMN_MAJOR pda = n; pdh = n; pdvl = n; pdvr = n; pdz = 1; #else pda = n; pdh = n; pdvl = n; pdvr = n; pdz = 1; #endif tau_len = n; w_len = n; ifaill_len = n; ifailr_len = n; select_len = n; /* Allocate memory */ if ( !(a = NAG_ALLOC(n * n, double)) || !(h = NAG_ALLOC(n * n, double)) || !(vl = NAG_ALLOC(n * n, double)) || !(vr = NAG_ALLOC(n * n, double)) || !(z = NAG_ALLOC(1 * 1, double)) || !(wi = NAG_ALLOC(w_len, double)) || !(wr = NAG_ALLOC(w_len, double)) || !(ifaill = NAG_ALLOC(ifaill_len, Integer)) || !(ifailr = NAG_ALLOC(ifaill_len, Integer)) || !(select = NAG_ALLOC(select_len, Boolean)) || !(tau = NAG_ALLOC(tau_len, double)) ) { 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] "); Vscanf("%lf%*[^\n] ", &thresh); /* Reduce A to upper Hessenberg form */ f08nec(order, n, 1, n, 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 */ for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) H(i,j) = A(i,j); } /* Calculate the eigenvalues of H (same as A) */ f08pec(order, Nag_EigVals, Nag_NotZ, n, 1, n, h, pdh, wr, wi, z, pdz, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08pec.\n%s\n", fail.message); exit_status = 1; goto END; } /* Print eigenvalues */ Vprintf(" Eigenvalues\n"); for (i = 0; i < n; ++i) Vprintf(" (%8.4f,%8.4f)\n", wr[i], wi[i]); Vprintf("\n"); for (i = 0; i < n; ++i) select[i] = (wr[i] < thresh); /* Calculate the eigenvectors of H (as specified by SELECT), */ /* storing the result in VR */ f08pkc(order, Nag_RightSide, Nag_HSEQRSource, Nag_NoVec, select, n, a, pda, wr, wi, vl, pdvl, vr, pdvr, n, &m, ifaill, ifailr, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08pkc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Calculate the eigenvectors of A = Q * VR */ f08ngc(order, Nag_LeftSide, Nag_NoTrans, n, m, 1, n, a, pda, tau, vr, pdvr, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from f08ngc.\n%s\n", fail.message); exit_status = 1; goto END; } /* Print Eigenvectors */ 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 (vl) NAG_FREE(vl); if (vr) NAG_FREE(vr); if (z) NAG_FREE(z); if (wi) NAG_FREE(wi); if (wr) NAG_FREE(wr); if (ifaill) NAG_FREE(ifaill); if (ifailr) NAG_FREE(ifailr); if (select) NAG_FREE(select); if (tau) NAG_FREE(tau); return exit_status; }