The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Math::Polynomial::Solve - Find the roots of polynomial equations.

SYNOPSIS

  use Math::Complex;  # The roots may be complex numbers.
  use Math::Polynomial::Solve qw(poly_roots);

  my @x = poly_roots(@coefficients);

or

  use Math::Complex;  # The roots may be complex numbers.
  use Math::Polynomial::Solve
        qw(linear_roots quadratic_roots cubic_roots quartic_roots);

  # Find the roots of ax + b
  my @x1 = linear_roots($a, $b);

  # Find the roots of ax**2 + bx +c
  my @x2 = quadratic_roots($a, $b, $c);

  # Find the roots of ax**3 + bx**2 +cx + d
  my @x3 = cubic_roots($a, $b, $c, $d);

  # Find the roots of ax**4 + bx**3 +cx**2 + dx + e
  my @x4 = quartic_roots($a, $b, $c, $d, $e);

DESCRIPTION

This package supplies a set of functions that find the roots of polynomials up to the quartic. There are no solutions for powers higher than that at this time (partly because there are no general solutions for fifth and higher powers).

The linear, quadratic, cubic, and quartic *_roots() functions all expect to have a non-zero value for the $a term.

Passing a zero constant term means that the first value returned from the function will always be zero, for all functions.

poly_roots()

A generic function that calls one of the other root-finding functions, depending on the degree of the polynomial. Returns the solution for polynomials of degree 1 to degree 4.

Unlike the other root-finding functions, it will check for coefficients of zero for the highest power, and 'step down' the degree of the polynomial to the appropriate case. Additionally, it will check for coefficients of zero for the lowest power terms, and add zeros to its root list before calling one of the root-finding functions. Therefore, it is possible to solve a polynomial of degree higher than 4, as long as it meets these rather specialized conditions.

linear_roots()

Here for completeness's sake more than anything else. Returns the solution for

    ax + b = 0

by returning -b/a. This may be in either a scalar or an array context.

quadratic_roots()

Gives the roots of the quadratic equation

    ax**2 + bx + c = 0

using the well-known quadratic formula. A two-element list is returned.

cubic_roots()

Gives the roots of the cubic equation

    ax**3 + bx**2 + cx + d = 0

by the method described by R. W. D. Nickalls (see the Acknowledgments section below). A three-element list is returned. The first element will always be real. The next two values will either be both real or both complex numbers.

quartic_roots()

Gives the roots of the quartic equation

    ax**4 + bx**3 + cx**2 + dx + e = 0

using Ferrari's method (see the Acknowledgments section below). A four-element list is returned. The first two elements will be either both real or both complex. The next two elements will also be alike in type.

EXPORT

There are no default exports. The functions may be named in an export list.

Acknowledgments

The cubic

The cubic is solved by the method described by R. W. D. Nickalls, "A New Approach to solving the cubic: Cardan's solution revealed," The Mathematical Gazette, 77, 354-359, 1993. This article is available on the web at http://www.m-a.org.uk/eb/mg/mg077ch.pdf.

Dr. Nickalls was kind enough to send me his article, with notes and revisions, and directed me to a Matlab script that was based on that article, written by Herman Bruyninckx, of the Dept. Mechanical Eng., Div. PMA, Katholieke Universiteit Leuven, Belgium. This function is an almost direct translation of that script, and I owe Herman Bruyninckx for creating it in the first place. It may be found on the web at http://www.mech.kuleuven.ac.be/~bruyninc/matlab/cubic.ml

Dick Nickalls, dicknickalls@compuserve.com

Herman Bruyninckx, Herman.Bruyninckx@mech.kuleuven.ac.be, http://www.mech.kuleuven.ac.be/~bruyninc

The quartic

The method for quartic solution is Ferrari's, as described in the web page Karl's Calculus Tutor, http://www.netsrq.com/~hahn/quartic.html. I also made use of some short cuts mentioned in web page Ask Dr. Math FAQ, http://forum.swarthmore.edu/dr.math/faq/faq.cubic.equations.html.

Other functionality

Matz Kindahl, the author of Math::Polynomial, suggested the poly_roots() function.

SEE ALSO

Forsyth, George E., Michael A. Malcolm, and Cleve B. Moler (1977), Computer Methods for Mathematical Computations, Prentice-Hall.

AUTHOR

John M. Gamble, jgamble@ripco.com