Extreme Optimization > User's Guide > Mathematics Library > Curves > Polynomials

Extreme Optimization User's Guide

User's Guide

Up: Curves Next: Chebyshev Approximations Previous: Simple Curves Contents

Polynomials

A polynomial is a linear function that is a combination of the argument raised to different integer powers. Polynomials are among the most common mathematical objects. The Extreme Optimization Mathematics Library for .NET has extensive support for common polynomials and Chebyshev polynomials.

The PolynomialBase class

All polynomial classes are derived from PolynomialBase This class defines a number of properties shared by all polynomial classes. PolynomialBase is itself derived from LinearCombination.

The parameters of a polynomial are the coefficients of the polynomial.

The Degree of a polynomial is the highest power that appears in the sum. The number of parameters of the polynomial equals the degree plus one.

General polynomials are implemented by the Polynomial class. The Constant, Line, and Quadratic classes all inherit from Polynomial. They represent polynomials of fixed degree 0, 1, and 2, respectively. An instance of one of these specialized classes can be substituted whenever a Polynomial instance is used in an expression.

Constructing polynomials

The Polynomial class has three constructors: The first constructor takes the degree of the polynomial as its only argument. All coefficients are initially set to zero. The second constructor takes an array of  Double values containing the coefficients. The third constructor takes a single Vector argument. This Vector should contain the coefficients of the polynomial. The following example illustrates three different ways of constructing the polynomial.

x3 - 5x2 + 9x + 2
C# CopyCode imageCopy Code
Polynomial p1 = new Polynomial(3);
p1.Coefficients[3] = 1;
p1.Coefficients[2] = -5;
p1.Coefficients[1] = 9;
p1.Coefficients[0] = 2;
Polynomial p2 = new Polynomial(new double[] {2, 9, -5, 1});
Vector coefficients = new Vector(2, 9, -5, 1);
Polynomial p3 = new Polynomial(coefficients);
Visual Basic CopyCode imageCopy Code
Dim p1 As Polynomial = new Polynomial(3);
p1.Coefficients(3) = 1
p1.Coefficients(2) = -5
p1.Coefficients(1) = 9
p1.Coefficients(0) = 2
Dim p2 As Polynomial = New Polynomial(New Double() {1, -5, 9, 2})
Dim coefficients As Vector = New GeneralVector(2, 9, -5, 1)
Dim p3 As Polynomial = New Polynomial(coefficients)

Notice that the coefficients in the constructors for p2 and p3 are listed in order of degree. The nth element of the array or Vector is the coefficient of degree n.

You can also create a polynomial using one of several static methods.

The GetInterpolatingPolynomial method returns the polynomial that interpolates a set of points. The x-coordinates of the points must be distinct. The x and y-coordinates must be given as two separate Double arrays. The following creates the interpolating polynomial through a set of four points:

C# CopyCode imageCopy Code
double[] xValues = new double[] {1, 2, 3, 4};
double[] yValues = new double[] {1, 4, 10, 8};
Polynomial p4;
p4 = Polynomial.GetInterpolatingPolynomial(xValues, yValues);
Visual Basic CopyCode imageCopy Code
Dim xValues As Double() = New Double() {1, 2, 3, 4}
Dim yValues As Double() = New Double() {1, 4, 10, 8}
Dim p4 As Polynomial
p4 = Polynomial.GetInterpolatingPolynomial(xValues, yValues);

The FromRoots method constructs a Polynomial whose roots are given by the arguments passed to the method. The roots are provided as a Double array. They can be in any order. The same root can appear multiple times. The following constructs a polynomial with roots 1, 2 (twice), and 4:

C# CopyCode imageCopy Code
double[] roots = new double[] {1, 2, 2, 4};
Polynomial p5 = Polynomial.FromRoots(roots);
Visual Basic CopyCode imageCopy Code
Dim roots As Double() = New Double() {1, 2, 3, 4}
Dim p5 As Polynomial = Polynomial.FromRoots(roots);

The LeastSquaresFit method constructs a Polynomial that is the least squares fit of a specified degree through a given set of points.

C# CopyCode imageCopy Code
double[] xValues = new double[7];
double[] yValues = new double[7];
double angle = 0;
for(int index = 0; index < 7; index++)
{
    xValues[index] = Math.Cos(angle);
    yValues[index] = -Math.Sin(angle);
    angle = angle + Constants.Pi / 6;
}
Polynomial p6 = Polynomial.LeastSquaresFit(xValues, yValues, 4);
Visual Basic CopyCode imageCopy Code
Dim xValues As Double() = New Double(7)
Dim yValues As Double() = New Double(7)
Dim angle As Double = 0
Dim index As Integer
For index = 0 to 6
    xValues(index) = Math.Cos(angle)
    yValues(index) = -Math.Sin(angle)
    angle = angle + Constants.Pi / 6
Next
Dim p6 As Polynomial = Polynomial.LeastSquaresFit(xValues, yValues, 4);

Working with polynomials

Polynomial implements all methods and properties of the Curve class.

The ValueAt method returns the value of the polynomial at a specified point. SlopeAt returns the derivative. These methods are overloaded to take both real and complex arguments. The complex version returns a complex number.

C# CopyCode imageCopy Code
Console.WriteLine("p1.ValueAt(2) = {0}", p1.ValueAt(2));
Console.WriteLine("p1.SlopeAt(2) = {0}", p1.SlopeAt(2));
DoubleComplex z = new DoubleComplex(1, -2);
Console.WriteLine("p1.ValueAt(1-2i) = {0}", p1.ValueAt(z));
Visual Basic CopyCode imageCopy Code
Console.WriteLine("p1.ValueAt(2) = {0}", p1.ValueAt(2))
Console.WriteLine("p1.SlopeAt(2) = {0}", p1.SlopeAt(2))
Dim z As DoubleComplex = New DoubleComplex(1, -2)
Console.WriteLine("p1.ValueAt(1-2i) = {0}", p1.ValueAt(z))

The GetDerivative method returns the polynomial that is the derivative of a polynomial. If the derivative is a constant, a line, or a quadratic curve, an instance of the corresponding type is returned.

C# CopyCode imageCopy Code
Curve derivative = p1.GetDerivative();
Console.WriteLine("Slope at 2 (derivative) = {0}", 
    derivative.ValueAt(2));
Console.WriteLine("Type of derivative: {0}",
    derivative.GetType().FullName);
Visual Basic CopyCode imageCopy Code
Dim derivative As Curve = p1.GetDerivative()
Console.WriteLine("Slope at 2 (derivative) = {0}", _
    derivative.ValueAt(2))
Console.WriteLine("Type of derivative: {0}", _
    derivative.GetType().FullName)

Integral evaluates the definite integral over a specified interval. The integral is calculated directly using the coefficients of the polynomial. No numerical approximations are used.

The parameters of a polynomial correspond to the coefficients of the polynomial. To make it easier to work with polynomial coefficients, an indexer property has been defined, which can be used to get and set individual coefficients directly. In Visual Basic .NET and other languages that don't support indexers, the indexed property Coefficient should be used instead.

C# CopyCode imageCopy Code
// We create the polynomial x^3 + x^2 - x - 2:
Polynomial p = new Polynomial(1, 1, -1, -2);
// The coefficient of x^1=x is '-1':
Console.WriteLine(p[1]);
// We can change the value of this coefficient.
p[1] = 0;
Visual Basic CopyCode imageCopy Code
' We create the polynomial x^3 + x^2 - x - 2:
Dim p As Polynomial = New Polynomial(1, 1, -1, -2)
' The coefficient of x^1=x is '-1':
Console.WriteLine(p.Coefficient(1))
' We can change the value of this coefficient.
p.Coefficient(1) = 0

Operations on polynomials

The result of adding or subtracting two polynomials is a new polynomial. Similarly, the result of multiplying two polynomials is a new polynomial.

The Polynomial class implements these operations through overloaded operators. Static methods are available for languages that don't support operator overloading. The following table summarizes these operations:

Operator Static method equivalent Description
+p (no equivalent) Returns the polynomial p.
-p Polynomial.Negate(p) Returns the negation of the polynomial p.
p1 + p2 Polynomial.Add(p1, p2) Adds the polynomials p1 and p2.
p1 - p2 Polynomial.Subtract(p1, p2) Subtracts the polynomials p1 and p2.
p1 * p2 Polynomial.Multiply(p1, p2) Multiplies the polynomials p1 and p2.
a * p Polynomial.Multiply(a, p) Multiplies the polynomial p by the real number a.
p1 / p2 Polynomial.Divide(p1, p2) Divides the polynomial p1 by p2.
p / a Polynomial.Divide(p, a) Divides the polynomial p by the real number a.
p1 % p2 Polynomial.Modulus(p1, p2) Returns the remainder when dividing the polynomial p1 by p2.
Table 2. Polynomial operators and their static (Shared) method equivalents.

The following example shows how to use these operators:

C# CopyCode imageCopy Code
Polynomial a = new Polynomial(2, -2, 4);
Polynomial b = new Polynomial(1, -3);
Polynomial c;
Console.WriteLine("a = {0}", a.ToString());
Console.WriteLine("b = {0}", b.ToString());
c = a + b;
Console.WriteLine("a + b = {0}", c.ToString());
c = a - b;
Console.WriteLine("a - b = {0}", c.ToString());
c = a * b;
Console.WriteLine("a * b = {0}", c.ToString());
c = a / b;
Console.WriteLine("a / b = {0}", c.ToString());
c = a % b;
Console.WriteLine("a % b = {0}", c.ToString());
Visual Basic CopyCode imageCopy Code
Dim a As Polynomial = New Polynomial(2, -2, 4)
Dim b As Polynomial = New Polynomial(1, -3)
Dim c As Polynomial = New Polynomial
Console.WriteLine("a = {0}", a.ToString())
Console.WriteLine("b = {0}", b.ToString())
c = Polynomial.Add(a, b)
Console.WriteLine("a + b = {0}", c.ToString())
c = Polynomial.Subtract(a, b)
Console.WriteLine("a - b = {0}", c.ToString())
c = Polynomial.Multiply(a, b)
Console.WriteLine("a * b = {0}", c.ToString())
c = Polynomial.Divide(a, b)
Console.WriteLine("a / b = {0}", c.ToString())
c = Polynomial.Modulus(a, b)
Console.WriteLine("a % b = {0}", c.ToString())

As with integers, division of polynomials is not always exact. The division operator returns a polynomial such that the degree of the remainder of the division is less than the degree of the divisor.

The Divide method has an overload that allows you to obtain both the quotient and remainder at the same time. This method takes a third out parameter that will hold the remainder.

C# CopyCode imageCopy Code
Polynomial d;
c = Polynomial.Divide(a, b, out d);
Console.WriteLine("Using Divide method:");
Console.WriteLine("  a / b = {0}", c.ToString());
Console.WriteLine("  a % b = {0}", d.ToString());
Visual Basic CopyCode imageCopy Code
Dim d As Polynomial
c = Polynomial.Divide(a, b, out d)
Console.WriteLine("Using Divide method:")
Console.WriteLine("  a / b = {0}", c.ToString())
Console.WriteLine("  a % b = {0}", d.ToString())

Note also that Constant, Line, and Quadratic objects can be used in polynomial expressions, as they inherit from Polynomial.

Finding roots of polynomials

A polynomial of degree n has at most n real roots, some of which may occur several times. The FindRoots method attempts to find all the real roots of a polynomial. For example, the polynomial x3 + x2 - 2 has one real root at x = 1:

C# CopyCode imageCopy Code
Polynomial polynomial1 = new Polynomial(1, 1, 0, -2);
double[] roots = polynomial1.FindRoots();
Console.WriteLine("Number of roots of p: {0}", roots.Length);
Console.WriteLine("Value of root 1 = {0}", roots[0]);
Visual Basic CopyCode imageCopy Code
Dim p As Polynomial = New Polynomial(1, 1, 0, -2)
Dim roots As Double() = polynomial1.FindRoots()
Console.WriteLine("Number of roots of p: {0}", roots.Length)
Console.WriteLine("Value of root 1 = {0}", roots(0))

In the complex domain, a polynomial of degree n always has n roots. The Polynomial class defines the FindComplexRoots method, which returns an array of DoubleComplex numbers that are the approximate roots of the polynomial.

C# CopyCode imageCopy Code
Polynomial polynomial2 = new Polynomial(1, 1, 0, -2);
DoubleComplex[] roots = polynomial2.FindComplexRoots();
Console.WriteLine("Number of roots of p: {0}", roots.Length);
Console.WriteLine("Value of root 1 = {0}", roots[0]);
Console.WriteLine("Value of root 2 = {0}", roots[1]);
Console.WriteLine("Value of root 3 = {0}", roots[2]);
Visual Basic CopyCode imageCopy Code
Dim polynomial2 As Polynomial = New Polynomial(1, 1, 0, -2)
Dim roots As DoubleComplex() = polynomial2.FindComplexRoots()
Console.WriteLine("Number of roots of p: {0}", roots.Length)
Console.WriteLine("Value of root 1 = {0}", roots(0))
Console.WriteLine("Value of root 2 = {0}", roots(1))
Console.WriteLine("Value of root 3 = {0}", roots(2))

The roots are returned in order of increasing real part, and, if the real parts are equal, increasing imaginary part.

Up: Curves Next: Chebyshev Approximations Previous: Simple Curves Contents

Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army