Polynomial is a JavaScript object used to manipulate polynomials. This object was created to encapsulate root finding functions needed by the intersection methods in the Intersection object.
getCubicRoots() and getQuarticRoots() was derived from David Eberly's excellent game engine source which can be found at his site. If you are at all interested in 3D gaming algorithms, then I highly recommend his "3D Game Engine Design" book (ISBN 1-55860-593-2).
This code is used in the 2D Geometry section of this site.
constructor - new Polynomial(<coefficient>+);
This method creates a new Polynomial object. All initialization is handled by the init() method. All parameters for this method are described below
init() initialized all properties for this object.
One or more coefficients initialize the Polynomial. The coefficients are in order by highest degree monomial first. For example, the following example initializes a Polynomial object for: 3x^4 + 2x^2 + 5:
var poly = new Polynomial(3, 0, 2, 0, 5);
All coefficients from highest degree to degree 0 must be provided. A zero is used for monomials that are not present in the polynomial.
NOTE: The polynomial coefficients are stored in an array in the reverse order to how they were specified. This has the benefit that the coefficient's position in the array corresponds to the degree of the monomial to which it belongs.
eval() evalutes the polynomial at the specified x value.
x is a number that is "plugged into" the polynomial to evaluate it.
multiply() multiplies two Polynomial objects together. A new polynomial is returned.
that is the Polynomial with which to multiply the current polynomial.
simplify() reduces the degree of the polynomial. Each monomial, highest degree first, is visited. If the absolute value of the monomial's coefficient is below a certain value, then the monomial is removed from the polynomial. This method is called by the getRoots() method.
bisection() attempts to locate a root within min and max for the current polynomial. The interval is successively divided in half until a root is found. If successful, the root is return; otherwise, null is returned.
min is the minimum value of the interval to check for a root.
max is the maximum value of the interval to check for a root.
toString() converts the Polynomial object to a string representation.
getDerivative() returns the derivative of the current polynomial.
getRoots() attempts to find the roots of the current polynomial. This method will attempt to decrease the degree of the polynomial using the simplify() method. Once the degree is determined, getRoots() dispatches the appropriate root-finding method for the degree of the polynomial.
NOTE: At this time, polynomials above the 4'th degree are not supported.
getRootsInInterval(min, max) : Array;
getRootsInInterval() finds the roots of the current polynomial within the closed interval [min, max]. This method makes use of getDerivative() and bisection().
getLinearRoot() : Array of Numbers;
getLinearRoot() returns the root of a linear polynomial (degree equals one).
getQuadraticRoots() : Array of Numbers;
getQuadraticRoots() returns the roots of a quadratic polynomial (degree equals two).
getCubicRoots() : Array of Numbers;
getCubicRoots() returns the roots of a cubic polynomial (degree equals three).
getQuarticRoots() : Array of Numbers;
getQuarticRoots() returns the roots of a quartic polynomial (degree equals four).
TOLERANCE is a number used to determine if a number is close enough to zero to be considered zero.
ACCURACY is an integer that is used to determine the minimum number of digits of accuracy to be returned by a result.
coefs is an array of numbers. Each number corresponds to a monomial in the polynomial. The position of the coefficient in the array corresponds to the degree of the monomial to which it belongs.
degree is "virtual variable" that returns the degree of the polynomial represented by the Polynomial object.
This example creates 4 monomials. These are multiplied together, creating successively higher degree polynomials.
Each polynomial is plotted (red markers) using the eval() method. The toString() methods creates the text representation of the polynomial that is displayed to the right of the x-axis (the gray line). The getRoots() method is used to display the roots of each polynomial (blue markers).
Polynomial.js - the Intersection object only
2D.js.gz - all objects needed to use this object and other 2D geometry objects