This chapter describes functions for evaluating and solving polynomials. There are routines for finding real and complex roots of quadratic and cubic equations using analytic methods.  An iterative polynomial solver is also available for finding the roots of general polynomials with real coefficients (of any order).  The functions are declared in the header file gsl_poly.h.

Polynomial Evaluation
=====================

The functions described here evaluate the polynomial $P(x) = c[0] + c[1] x + c[2] x^2 + \dots + c[len-1] x^len-1$ P(x) = c[0] + c[1] x + c[2] x^2 + \dots + c[len-1] x^{len-1} using Horner's method for stability. inlinefns

 -- Function: double gsl_poly_eval (const double C[], const int LEN, const double X)
     This function evaluates a polynomial with real coefficients for the real variable X.

 -- Function: gsl_complex gsl_poly_complex_eval (const double C[], const int LEN, const gsl_complex Z)
     This function evaluates a polynomial with real coefficients for the complex variable Z.

 -- Function: gsl_complex gsl_complex_poly_complex_eval (const gsl_complex C[], const int LEN, const gsl_complex Z)
     This function evaluates a polynomial with complex coefficients for the complex variable Z.

 -- Function: int gsl_poly_eval_derivs (const double C[], const size_t LENC, const double X, double RES[], const size_t LENRES)
     This function evaluates a polynomial and its derivatives storing the results in the array RES of size LENRES.  The output array contains the values of d^k P/d x^k for the specified value of X starting with k = 0.

Divided Difference Representation of Polynomials
================================================

The functions described here manipulate polynomials stored in Newton's divided-difference representation.  The use of divided-differences is described in Abramowitz & Stegun sections 25.1.4 and 25.2.26.

 -- Function: int gsl_poly_dd_init (double DD[], const double XA[], const double YA[], size_t SIZE)
     This function computes a divided-difference representation of the interpolating polynomial for the points (XA, YA) stored in the arrays XA and YA of length SIZE.  On output the divided-differences of (XA,YA) are stored in the array DD, also of length SIZE.

 -- Function: double gsl_poly_dd_eval (const double DD[], const double XA[], const size_t SIZE, const double X)
     This function evaluates the polynomial stored in divided-difference form in the arrays DD and XA of length SIZE at the point X.  inlinefn

 -- Function: int gsl_poly_dd_taylor (double C[], double XP, const double DD[], const double XA[], size_t SIZE, double W[])
     This function converts the divided-difference representation of a polynomial to a Taylor expansion.  The divided-difference representation is supplied in the arrays DD and XA of length SIZE. On output the Taylor coefficients of the polynomial expanded about the point XP are stored in the array C also of length SIZE.  A workspace of length SIZE must be provided in the array W.

Quadratic Equations
===================

 -- Function: int gsl_poly_solve_quadratic (double A, double B, double C, double * X0, double * X1)
     This function finds the real roots of the quadratic equation,

          a x^2 + b x + c = 0

     The number of real roots (either zero, one or two) is returned, and their locations are stored in X0 and X1.  If no real roots are found then X0 and X1 are not modified.  If one real root is found (i.e. if a=0) then it is stored in X0.  When two real roots are found they are stored in X0 and X1 in ascending order.  The case of coincident roots is not considered special.  For example (x-1)^2=0 will have two roots, which happen to have exactly equal values.

     The number of roots found depends on the sign of the discriminant b^2 - 4 a c.  This will be subject to rounding and cancellation errors when computed in double precision, and will also be subject to errors if the coefficients of the polynomial are inexact.  These errors may cause a discrete change in the number of roots.  However, for polynomials with small integer coefficients the discriminant can always be computed exactly.

 -- Function: int gsl_poly_complex_solve_quadratic (double A, double B, double C, gsl_complex * Z0, gsl_complex * Z1)

     This function finds the complex roots of the quadratic equation,

          a z^2 + b z + c = 0

     The number of complex roots is returned (either one or two) and the locations of the roots are stored in Z0 and Z1.  The roots are returned in ascending order, sorted first by their real components and then by their imaginary components.  If only one real root is found (i.e. if a=0) then it is stored in Z0.

Cubic Equations
===============

 -- Function: int gsl_poly_solve_cubic (double A, double B, double C, double * X0, double * X1, double * X2)

     This function finds the real roots of the cubic equation,

          x^3 + a x^2 + b x + c = 0

     with a leading coefficient of unity.  The number of real roots (either one or three) is returned, and their locations are stored in X0, X1 and X2.  If one real root is found then only X0 is modified.  When three real roots are found they are stored in X0, X1 and X2 in ascending order.  The case of coincident roots is not considered special.  For example, the equation (x-1)^3=0 will have three roots with exactly equal values.  As in the quadratic case, finite precision may cause equal or closely-spaced real roots to move off the real axis into the complex plane, leading to a discrete change in the number of real roots.

 -- Function: int gsl_poly_complex_solve_cubic (double A, double B, double C, gsl_complex * Z0, gsl_complex * Z1, gsl_complex * Z2)

     This function finds the complex roots of the cubic equation,

          z^3 + a z^2 + b z + c = 0

     The number of complex roots is returned (always three) and the locations of the roots are stored in Z0, Z1 and Z2.  The roots are returned in ascending order, sorted first by their real components and then by their imaginary components.

General Polynomial Equations
============================

The roots of polynomial equations cannot be found analytically beyond the special cases of the quadratic, cubic and quartic equation.  The algorithm described in this section uses an iterative method to find the approximate locations of roots of higher order polynomials.

 -- Function: gsl_poly_complex_workspace * gsl_poly_complex_workspace_alloc (size_t N)
     This function allocates space for a gsl_poly_complex_workspace struct and a workspace suitable for solving a polynomial with N coefficients using the routine gsl_poly_complex_solve.

     The function returns a pointer to the newly allocated gsl_poly_complex_workspace if no errors were detected, and a null pointer in the case of error.

 -- Function: void gsl_poly_complex_workspace_free (gsl_poly_complex_workspace * W)
     This function frees all the memory associated with the workspace W.

 -- Function: int gsl_poly_complex_solve (const double * A, size_t N, gsl_poly_complex_workspace * W, gsl_complex_packed_ptr Z)
     This function computes the roots of the general polynomial $P(x) = a_0 + a_1 x + a_2 x^2 + ... + a_n-1 x^n-1$ P(x) = a_0 + a_1 x + a_2 x^2 + ... + a_{n-1} x^{n-1} using balanced-QR reduction of the companion matrix.  The parameter N specifies the length of the coefficient array.  The coefficient of the highest order term must be non-zero.  The function requires a workspace W of the appropriate size.  The n-1 roots are returned in the packed complex array Z of length 2(n-1), alternating real and imaginary parts.

     The function returns GSL_SUCCESS if all the roots are found. If the QR reduction does not converge, the error handler is invoked with an error code of GSL_EFAILED.  Note that due to finite precision, roots of higher multiplicity are returned as a cluster of simple roots with reduced accuracy.  The solution of polynomials with higher-order roots requires specialized algorithms that take the multiplicity structure into account (see e.g. Z. Zeng, Algorithm 835, ACM Transactions on Mathematical Software, Volume 30, Issue 2 (2004), pp 218--236).

Examples
========

To demonstrate the use of the general polynomial solver we will take the polynomial P(x) = x^5 - 1 which has the following roots,

     1, e^{2\pi i /5}, e^{4\pi i /5}, e^{6\pi i /5}, e^{8\pi i /5}

The following program will find these roots.

          [file polyroots.c not included]

The output of the program is,

     $ ./a.out
          [file polyroots.out not included]

which agrees with the analytic result, z_n = \exp(2 \pi n i/5).

References and Further Reading
==============================

The balanced-QR method and its error analysis are described in the following papers,

  R.S. Martin, G. Peters and J.H. Wilkinson, ``The QR Algorithm for Real Hessenberg Matrices'', Numerische Mathematik, 14 (1970), 219--231.

  B.N. Parlett and C. Reinsch, ``Balancing a Matrix for Calculation of Eigenvalues and Eigenvectors'', Numerische Mathematik, 13 (1969), 293--304.

  A. Edelman and H. Murakami, ``Polynomial roots from companion matrix eigenvalues'', Mathematics of Computation, Vol. 64, No. 210 (1995), 763--776.

The formulas for divided differences are given in Abramowitz and Stegun,

  Abramowitz and Stegun, Handbook of Mathematical Functions, Sections 25.1.4 and 25.2.26.