/* nag_dstev (f08jac) Example Program. * * Copyright 2011 Numerical Algorithms Group. * * Mark 23, 2011. */ #include #include #include #include #include #include #include int main(void) { /* Scalars */ double eerrbd, eps; Integer exit_status = 0, i, j, n, pdz; /* Arrays */ double *d = 0, *e = 0, *rcondz = 0, *z = 0, *zerrbd = 0; /* Nag Types */ Nag_OrderType order; NagError fail; #ifdef NAG_COLUMN_MAJOR #define Z(I, J) z[(J - 1) * pdz + I - 1] order = Nag_ColMajor; #else #define Z(I, J) z[(I - 1) * pdz + J - 1] order = Nag_RowMajor; #endif INIT_FAIL(fail); printf("nag_dstev (f08jac) Example Program Results\n\n"); /* Skip heading in data file */ scanf("%*[^\n]"); scanf("%ld%*[^\n]", &n); /* Allocate memory */ if (!(d = NAG_ALLOC(n, double)) || !(e = NAG_ALLOC(n, double)) || !(rcondz = NAG_ALLOC(n, double)) || !(z = NAG_ALLOC(n*n, double)) || !(zerrbd = NAG_ALLOC(n, double))) { printf("Allocation failure\n"); exit_status = -1; goto END; } pdz = n; /* Read the diagonal and off-diagonal elements of the matrix A * from data file. */ for (i = 0; i < n; ++i) scanf("%lf", &d[i]); scanf("%*[^\n]"); for (i = 0; i < n - 1; ++i) scanf("%lf", &e[i]); scanf("%*[^\n]"); /* nag_dstev (f08jac). * Solve the symmetric tridiagonal eigenvalue problem. */ nag_dstev(order, Nag_DoBoth, n, d, e, z, pdz, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_dstev (f08jac).\n%s\n", fail.message); exit_status = 1; goto END; } /* Normalize the eigenvectors */ for(j=1; j<=n; j++) for(i=n; i>=1; i--) Z(i, j) = Z(i, j) / Z(1,j); /* Print solution */ printf("Eigenvalues\n"); for (i = 0; i < n; ++i) printf("%8.4f%s", d[i], (i+1)%8 == 0?"\n":" "); printf("\n"); /* nag_gen_real_mat_print (x04cac). * Print eigenvectors. */ fflush(stdout); nag_gen_real_mat_print(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, z, pdz, "Eigenvectors", 0, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_gen_real_mat_print (x04cac).\n%s\n", fail.message); exit_status = 1; goto END; } /* Get the machine precision, eps, using nag_machine_precision (X02AJC) * and compute the approximate error bound for the computed eigenvalues. * Note that for the 2-norm, ||A|| = max {|d[i]|, i=0..n-1}, and since * the eigenvalues are in ascending order ||A|| = max( |d[0]|, |d[n-1]|). */ eps = nag_machine_precision; eerrbd = eps * MAX(fabs(d[0]), fabs(d[n-1])); /* nag_ddisna (f08flc). * Estimate reciprocal condition numbers for the eigenvectors. */ nag_ddisna(Nag_EigVecs, n, n, d, rcondz, &fail); if (fail.code != NE_NOERROR) { printf("Error from nag_ddisna (f08flc).\n%s\n", fail.message); exit_status = 1; goto END; } /* Compute the error estimates for the eigenvectors */ for (i = 0; i < n; ++i) zerrbd[i] = eerrbd / rcondz[i]; /* Print the approximate error bounds for the eigenvalues and vectors */ printf("\nError estimate for the eigenvalues\n"); printf("%11.1e\n", eerrbd); printf("\nError estimates for the eigenvectors\n"); for (i = 0; i < n; ++i) printf("%11.1e%s", zerrbd[i], (i+1)%6 == 0?"\n":" "); END: if (d) NAG_FREE(d); if (e) NAG_FREE(e); if (rcondz) NAG_FREE(rcondz); if (z) NAG_FREE(z); if (zerrbd) NAG_FREE(zerrbd); return exit_status; } #undef Z