SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.function.polynomial
Class Polynomial

java.lang.Object
  extended by com.numericalmethod.suanshu.analysis.function.rn2r1.UnivariateRealFunction
      extended by com.numericalmethod.suanshu.analysis.function.polynomial.Polynomial
All Implemented Interfaces:
Function, RealScalarFunction, AbelianGroup<Polynomial>, Monoid<Polynomial>, Ring<Polynomial>, VectorSpace<Polynomial,Real>
Direct Known Subclasses:
DPolynomial

public class Polynomial
extends UnivariateRealFunction
implements Ring<Polynomial>, VectorSpace<Polynomial,Real>

Polynomial is a UnivariateRealFunction that represents a finite length expression constructed from variables and constants, using the operations of addition, subtraction, multiplication, and constant non-negative whole number exponents. Specifically, it has the form

anxn + an-1xn-1 + ... + a1x1 + a0

This implementation

See Also:
Wikipedia: Polynomial

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.numericalmethod.suanshu.analysis.function.Function
Function.EvaluationException
 
Field Summary
 int degree
          the degree of this polynomial It is equal to the largest exponent of the variable.
static Polynomial ONE
          a polynomial representing 1
static Polynomial ZERO
          a polynomial representing 0
 
Constructor Summary
Polynomial(double... coefficients)
          Construct a polynomial from an array of coefficients.
 
Method Summary
 Polynomial add(Polynomial that)
          + : G × G → G
 double coefficient(int i)
          Get the coefficient of xn-i, namely, an-i.
 double[] coefficients()
          Get a copy of the coefficients.
 boolean equals(java.lang.Object obj)
           
 Complex evaluate(Complex z)
          Evaluate this polynomial for a Complex number input.
 double evaluate(double x)
          Evaluate this polynomial for a real number, i.e., double input.
 Complex evaluate(java.lang.Number x)
          Evaluate this polynomial for a Number input.
 int hashCode()
           
 Polynomial minus(Polynomial that)
          - : G × G → G - is not in the definition of of an additive group but can be deduced.
 Polynomial multiply(Polynomial that)
          · : G × G → G
 Polynomial normalize()
          Normalize this polynomial so that the leading coefficient is 1.
 Polynomial ONE()
          The multiplicative element 1 in the group such that for any elements a in the group, the equation 1 × a = a × 1 = a holds.
 Polynomial opposite()
          For each a in G, there exists an element b in G such that a + b = b + a = 0 That is, it is the object such as this.add(this.opposite()) == this.ZERO
 Polynomial scaled(double scalar)
           
 Polynomial scaled(Real scalar)
          Deprecated. Not supported yet.
 java.lang.String toString()
           
 Polynomial ZERO()
          The additive element 0 in the group, such that for all elements a in the group, the equation 0 + a = a + 0 = a holds.
 
Methods inherited from class com.numericalmethod.suanshu.analysis.function.rn2r1.UnivariateRealFunction
dimension4Domain, dimension4Range, evaluate
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Polynomial ZERO
a polynomial representing 0


ONE

public static final Polynomial ONE
a polynomial representing 1


degree

public final int degree
the degree of this polynomial

It is equal to the largest exponent of the variable.

For example,

x4 + 1
has a degree of 4.

Constructor Detail

Polynomial

public Polynomial(double... coefficients)
Construct a polynomial from an array of coefficients. The 0-th entry corresponds to the xn term. The n-th entry corresponds to the constant term.

The degree of the polynomial is n.

For example,

new Polynomial(1, -2, 3, 2)
will create an instance of Polynomial representing
x3 - 2x2 + 3x + 2

Parameters:
coefficients - the polynomial coefficients in (double)
Method Detail

coefficients

public double[] coefficients()
Get a copy of the coefficients.

Given a polynomial of degree n, the i-th element correspond to the coefficient of xn-i. The 0-th entry corresponds to the xn term, which cannot be zero; The n-th entry corresponds to the constant term.

Returns:
a copy of the coefficients

coefficient

public double coefficient(int i)
Get the coefficient of xn-i, namely, an-i.

Parameters:
i - indicates the i-th coefficient in this polynomial
Returns:
an-i
See Also:
coefficients()

normalize

public Polynomial normalize()
Normalize this polynomial so that the leading coefficient is 1. The result is independent, and can be modified anyhow.

Returns:
a scaled version of the polynomial which has a leading coefficient 1

evaluate

public Complex evaluate(java.lang.Number x)
Evaluate this polynomial for a Number input.

Parameters:
x - the argument
Returns:
p(x), the result in Complex

evaluate

public double evaluate(double x)
Evaluate this polynomial for a real number, i.e., double input.

Specified by:
evaluate in class UnivariateRealFunction
Parameters:
x - the argument
Returns:
p(x), the result in Double

evaluate

public Complex evaluate(Complex z)
Evaluate this polynomial for a Complex number input.

Parameters:
z - the argument
Returns:
p(z), the result in Complex

add

public Polynomial add(Polynomial that)
Description copied from interface: AbelianGroup
+ : G × G → G

Specified by:
add in interface AbelianGroup<Polynomial>
Parameters:
that - the object to be added
Returns:
this + that

minus

public Polynomial minus(Polynomial that)
Description copied from interface: AbelianGroup
- : G × G → G

- is not in the definition of of an additive group but can be deduced. This function is provided for convenience purpose. It is equivalent to

this.add(that.opposite())

Specified by:
minus in interface AbelianGroup<Polynomial>
Parameters:
that - the object to be subtracted (subtrahend)
Returns:
this - that

multiply

public Polynomial multiply(Polynomial that)
Description copied from interface: Monoid
· : G × G → G

Specified by:
multiply in interface Monoid<Polynomial>
Parameters:
that - the multiplicand
Returns:
this × that

scaled

@Deprecated
public Polynomial scaled(Real scalar)
Deprecated. Not supported yet.

Description copied from interface: VectorSpace
* : F × V → V

The result of applying this function to scalar, c, in F and v in V is denoted cv.

Specified by:
scaled in interface VectorSpace<Polynomial,Real>
Parameters:
scalar - a multiplier
Returns:
scalar * this
See Also:
Wikipedia: Scalar multiplication

scaled

public Polynomial scaled(double scalar)

opposite

public Polynomial opposite()
Description copied from interface: AbelianGroup
For each a in G, there exists an element b in G such that
a + b = b + a = 0

That is, it is the object such as

this.add(this.opposite()) == this.ZERO

Specified by:
opposite in interface AbelianGroup<Polynomial>
Returns:
-this
See Also:
Wikipedia: Additive inverse

ZERO

public Polynomial ZERO()
Description copied from interface: AbelianGroup
The additive element 0 in the group, such that for all elements a in the group, the equation
0 + a = a + 0 = a
holds.

Specified by:
ZERO in interface AbelianGroup<Polynomial>
Returns:
0

ONE

public Polynomial ONE()
Description copied from interface: Monoid
The multiplicative element 1 in the group such that for any elements a in the group, the equation
1 × a = a × 1 = a
holds.

Specified by:
ONE in interface Monoid<Polynomial>
Returns:
1

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.