/* nag_zeros_real_poly (c02agc) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * * Mark 8 revised, 2004. */ #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; Nag_Boolean scale; Complex *z = 0; Integer exit_status = 0, i, n, nroot; NagError fail; double *a = 0; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_zeros_real_poly (c02agc) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld", &n); if (n > 0) { scale = Nag_TRUE; if (!(a = NAG_ALLOC(n+1, double)) || !(z = NAG_ALLOC(n, Complex))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { fprintf(fpout, "Invalid n.\n"); exit_status = 1; return exit_status; } for (i = 0; i <= n; i++) fscanf(fpin, "%lf", &a[i]); fprintf(fpout, "\nDegree of polynomial = %4ld\n\n", n); /* nag_zeros_real_poly (c02agc). * Zeros of a polynomial with real coefficients */ nag_zeros_real_poly(n, a, scale, z, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_zeros_real_poly (c02agc).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "Roots of polynomial\n\n"); nroot = 1; while (nroot <= n) { if (z[nroot-1].im == 0.0) { fprintf(fpout, "z = %13.4e\n", z[nroot-1].re); nroot += 1; } else { fprintf(fpout, "z = %13.4e +/- %14.4e\n", z[nroot-1].re, fabs(z[nroot-1].im)); nroot += 2; } } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (a) NAG_FREE(a); if (z) NAG_FREE(z); return exit_status; }