edu.nrao.sss.math
Class Polynomial

java.lang.Object
  extended by edu.nrao.sss.math.Polynomial
All Implemented Interfaces:
Serializable, Cloneable, Comparable<Polynomial>

public class Polynomial
extends Object
implements Cloneable, Serializable, Comparable<Polynomial>

A polynomial equation of the form f(x)=c0 + c1x + c2x2 + ... + cnxn.

Version Info:

$Revision: 2151 $
$Date: 2009-04-03 10:26:17 -0600 (Fri, 03 Apr 2009) $
$Author: dharland $ (last person to modify)

Since:
2006-03-24
Author:
David M. Harland
See Also:
Serialized Form

Constructor Summary
Polynomial()
          Creates a new polynomial that has no terms.
 
Method Summary
 void add(Polynomial other)
          Adds the given polynomial to this one.
 void add(PolynomialTerm term)
          Adds a term to this polynomial.
 double calculateFor(double number)
          Returns the value of this polynomial by using number as the value of the independent variable.
 void clear()
          Removes all terms from this polynomial.
 Polynomial clone()
          Returns an polynomial that is equal to this one.
 int compareTo(Polynomial other)
          Compares this polynomial to other for order.
 Polynomial createDerivative()
          Returns a polynomial that is the mathematical derivative of this one.
 boolean equals(Object o)
          Returns true if o is equal to this polynomial.
 PolynomialTerm getNonNullTerm(int exponent)
          Returns a copy of this polynomial's term with the given exponent.
 int getNumberOfTerms()
          Returns the number of terms in this polynomial.
 PolynomialTerm getTerm(int exponent)
          Returns a copy of this polynomial's term with the given exponent.
 SortedMap<Integer,PolynomialTerm> getTerms()
          Returns a set of copies of the terms of this polynomial.
 int hashCode()
          Returns a hash code value for this polynomial.
 boolean hasTerm(int exponent)
          Returns true if this polynomial has a term with the given exponent.
 int order()
          Returns the order of this polynomial.
static Polynomial parse(String polynomialString)
          Returns a new polynomial based on polynomialString.
 void replace(PolynomialTerm newTerm)
          Replaces a term in this polynomial with a copy of newTerm.
 void set(String polynomialString)
          Returns a new polynomial based on polynomialString.
 int smallestExponent()
           
 String toString()
          Returns a text representation of this polynomial.
 String toString(char variableName)
          Returns a text representation of this polynomial.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Polynomial

public Polynomial()
Creates a new polynomial that has no terms.

Method Detail

set

public void set(String polynomialString)
Returns a new polynomial based on polynomialString. See parse(String) for the expected format of polynomialString.

Parameters:
polynomialString - a string that will be used to configure this polynomial.
Throws:
IllegalArgumentException - if polynomialString is not in the expected form.

add

public void add(PolynomialTerm term)
Adds a term to this polynomial.

After this method is called, the coefficient of the term of this polynomial with exponent equal to that of term is equal to what it had been plus term.getCoefficient(). In otherwords, this method truly adds term (in an arithmetical sense, as opposed to a collection of objects sense) to this polynomial. Contrast this with the behavior of replace(PolynomialTerm).

Note that this polynomial will not hold a reference to term as a result of a call to this method. This means that any operations performed on term after this method is called will not have an effect on this polynomial.

Parameters:
term - the term to be added to this polynomial. If this value is null this polynomial is not altered.

add

public void add(Polynomial other)
Adds the given polynomial to this one.

Parameters:
other - the polynomial to be added to this one.

replace

public void replace(PolynomialTerm newTerm)
Replaces a term in this polynomial with a copy of newTerm.

If this polynomial already contained a term with the same exponent as newTerm, it will be replaced. Note that it is a copy of newTerm that will become part of this polynomial. This polynomial will not maintain a reference to newTerm.

Parameters:
newTerm - a replacement for the term of this polynomial that has an exponent of newTerm.getExponent().

hasTerm

public boolean hasTerm(int exponent)
Returns true if this polynomial has a term with the given exponent.

If this method returns true, getTerm(int) will not return null. It is possible, though, that the returned term will have a coefficient of zero.

Parameters:
exponent - the exponent of a term whose existence is sought.
Returns:
true if this polynomial has a term with the given exponent.

getTerms

public SortedMap<Integer,PolynomialTerm> getTerms()
Returns a set of copies of the terms of this polynomial.

Returns:
a set of copies of the terms of this polynomial.

getTerm

public PolynomialTerm getTerm(int exponent)
Returns a copy of this polynomial's term with the given exponent.

If this polynomial has no such term, null is returned. Contrast this with the behavior of getNonNullTerm(int).

Note that this method never returns a reference to one of its terms, but always returns a copy (or null).

Parameters:
exponent - the exponent for which a term is sought.
Returns:
a copy of this polynomial's term with the given exponent, or null if this polynomial has no such term.

getNonNullTerm

public PolynomialTerm getNonNullTerm(int exponent)
Returns a copy of this polynomial's term with the given exponent.

If this polynomial has no such term, a new term with a coefficient of zero is created and returned. This means that this method will never return a value of null, even when it has no term for the given exponent. Contrast this with the behavior of getTerm(int).

Note that this method never returns a reference to one of its terms, but always returns a copy (or a new null-like term).

Parameters:
exponent - the exponent for which a term is sought.
Returns:
a copy of this polynomial's term with the given exponent, or a new term with coefficient of zero, if this polynomial has no such term. The value returned will never be null.

order

public int order()
Returns the order of this polynomial. The order is the largest exponent of this polynomial's terms.

Returns:
the order of this polynomial.

smallestExponent

public int smallestExponent()

getNumberOfTerms

public int getNumberOfTerms()
Returns the number of terms in this polynomial.

Returns:
the number of terms in this polynomial.

calculateFor

public double calculateFor(double number)
Returns the value of this polynomial by using number as the value of the independent variable.

Returns:
the value of this polynomial evaluated for number.

createDerivative

public Polynomial createDerivative()
Returns a polynomial that is the mathematical derivative of this one. That is, for each term cixi of this polynomial, the derivative polynomial has a term with a coefficient of i * ci and an exponent of of xi-1. Note: the exception to the above rule is that if this polynomial has a term with an exponent of zero, the derivative will not contain a term derived from it.

Returns:
a polynomial that is the mathematical derivative of this one.

clear

public void clear()
Removes all terms from this polynomial.


parse

public static Polynomial parse(String polynomialString)
Returns a new polynomial based on polynomialString.

Each term of the parsed string must comply with the form documented in PolynomialTerm.parse(String). Each term may be separated by either a '+' or '-' character. White space between the terms and their separators is legal but not necessary.

Examples:

The terms are not required to be in any particular order, and the number of terms with a given exponent is not restricted.

Special Cases
A polynomialString of null or "" (the empty string) will not result in an IllegalArgumentException, but will instead return a polynomial that contains no terms.

Parameters:
polynomialString - a string that will be converted into a polynomial.
Throws:
IllegalArgumentException - if polynomialString is not in the expected form.

clone

public Polynomial clone()
Returns an polynomial that is equal to this one.

Overrides:
clone in class Object

equals

public boolean equals(Object o)
Returns true if o is equal to this polynomial.

Overrides:
equals in class Object

hashCode

public int hashCode()
Returns a hash code value for this polynomial.

Overrides:
hashCode in class Object

compareTo

public int compareTo(Polynomial other)
Compares this polynomial to other for order.

The polynomials are ordered by comparing the coefficients of their terms of equal exponents. Of two terms with equal exponents, the one with the smaller coefficient is considered to be the lesser. When two polynomials do not have a complete set of matching terms, those that are missing are considered to have coefficients of zero.

Example
Consider these two polynomials:

   A. 1 + 2x + 4x^3 + 6x^5
   B. 1 + 2x + 3x^2 + 6x^5
For purposes of comparison, these are expanded to:
   A. 1 + 2x + 0x^2 + 4x^3 + 0x^4 + 6x^5
   B. 1 + 2x + 3x^2 + 0x^3 + 0x^4 + 6x^5
The comparison then looks at the x^0 terms, then the x^1 terms, and so on, until it comes to a pair that does not have equal coefficients. In this example, A precedes B because of the coefficients of the x^2 term.

Specified by:
compareTo in interface Comparable<Polynomial>
Parameters:
other - the polynomial to which this one is compared.
Returns:
a negative integer, zero, or a positive integer as this interval is less than, equal to, or greater than the other interval.

toString

public String toString()
Returns a text representation of this polynomial.

The individual terms are represented as documented in PolynomialTerm.toString(). Each term is separated by " + " (a single space, a '+' character, and another single space).

Overrides:
toString in class Object
Returns:
a text representation of this polynomial.

toString

public String toString(char variableName)
Returns a text representation of this polynomial.

The individual terms are represented as documented in PolynomialTerm.toString(char). Each term is separated by " + " (a single space, a '+' character, and another single space).

Parameters:
variableName - the character to use as the independent variable.
Returns:
a text representation of this polynomial.


Copyright © 2009. All Rights Reserved.