edu.nrao.sss.math
Class PolynomialTerm

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

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

A single term in 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
PolynomialTerm()
          Creates a new term with a coefficient of 0.0 and an exponent of 0.
PolynomialTerm(double coefficient, int exponent)
          Creates a new term with the given coefficient and exponent.
 
Method Summary
 double add(PolynomialTerm otherTerm)
          Adds otherTerm to this one, if and only if its exponent is the same as this term's.
 double calculateFor(double number)
          Returns the value of this term calculated for the given number.
 PolynomialTerm clone()
          Returns an polynomial term that is equal to this one.
 int compareTo(PolynomialTerm otherTerm)
          Compares this term with the otherTerm for order.
 PolynomialTerm createDerivativeTerm()
          Returns a term that is a derivative of this term.
 boolean equals(Object o)
          Returns true if o is equal to this term.
 double getCoefficient()
          Returns the coefficient of this term.
 int getExponent()
          Returns the exponent of this term.
 int hashCode()
          Returns a hash code value for this term.
static PolynomialTerm parse(String termString)
          Returns a new term based on termString.
 void set(double newCoefficient, int newExponent)
          Sets both the coefficient and exponent of this term.
 void set(String termString)
          Returns a new term based on termString.
 void setCoefficient(double newCoefficient)
          Sets the coefficient of this term to newCoefficient.
 void setExponent(int newExponent)
          Sets the exponent of this term to newExponent.
 String toString()
          Returns a string of form #.#t^#.
 String toString(char variableName)
          Returns a string of form #.#variableName^#.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PolynomialTerm

public PolynomialTerm()
Creates a new term with a coefficient of 0.0 and an exponent of 0.


PolynomialTerm

public PolynomialTerm(double coefficient,
                      int exponent)
Creates a new term with the given coefficient and exponent.

Method Detail

set

public void set(double newCoefficient,
                int newExponent)
Sets both the coefficient and exponent of this term.

Parameters:
newCoefficient - the new coefficient of this term.
newExponent - the new exponent of this term.

set

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

Parameters:
termString - a string that will be used to set this term.
Throws:
IllegalArgumentException - if termString is not in the expected form.

setCoefficient

public void setCoefficient(double newCoefficient)
Sets the coefficient of this term to newCoefficient.

Parameters:
newCoefficient - the new coefficient of this term.
See Also:
getCoefficient()

getCoefficient

public double getCoefficient()
Returns the coefficient of this term. For term cixi, ci is the coefficient.

Returns:
the coefficient of this term.

setExponent

public void setExponent(int newExponent)
Sets the exponent of this term to newExponent.

Parameters:
newExponent - the new exponent of this term.
See Also:
getExponent()

getExponent

public int getExponent()
Returns the exponent of this term. For term cixi, i is the exponent.

Returns:
the exponent of this term.

createDerivativeTerm

public PolynomialTerm createDerivativeTerm()
Returns a term that is a derivative of this term. That is, for term cixi, the returned term has a coefficient of i * ci and an exponent of of xi-1.

For the special case where the exponent is zero, the returned term will have a coefficient of 0.0 and an exponent of 0.

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

calculateFor

public double calculateFor(double number)
Returns the value of this term calculated for the given number. That is the result, r, is calculated as:
   r = getCoefficient() * (number ^ getExponent())
 

Parameters:
number - the value of the independent variable of this term. For term cixi, x is the independent variable.
Returns:
getCoefficient() * (number ^ getExponent()).

add

public double add(PolynomialTerm otherTerm)
Adds otherTerm to this one, if and only if its exponent is the same as this term's. If the exponents are not identical, this method does nothing.

Parameters:
otherTerm - the term to be added to this one.
Returns:
the new value of this term's coefficient. If otherTerm does not have the same exponent as this term, the value returned will be the coefficient of this term before this method was called.

parse

public static PolynomialTerm parse(String termString)
Returns a new term based on termString.

The format of termString is [+-]#.#a^[+-]#, where
  '#' is any numeric character repeated one or more times,
  'a' any alpha character [a-zA-Z] repeated one or more times,
  '^' is the literal caret character, and
  '[+-]'indicates that a '+' character, a '-' character, or neither is permitted before the coefficient and the exponent.

Examples of typical terms are: 12.34x^56, +12.34x^-56, and -12.34x^+56. Constant terms, such as 42 or 867.5309 are also legal, as are terms that have an implied exponent of one, such as 12.34x.

Special Cases
A termString of null or "" (the empty string) will not result in an IllegalArgumentException, but will instead return a term whose coefficient and exponent are both zero.

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

clone

public PolynomialTerm clone()
Returns an polynomial term 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 term.

Overrides:
equals in class Object

hashCode

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

Overrides:
hashCode in class Object

compareTo

public int compareTo(PolynomialTerm otherTerm)
Compares this term with the otherTerm for order.

Specified by:
compareTo in interface Comparable<PolynomialTerm>

toString

public String toString()
Returns a string of form #.#t^#. For example, the following are all possible return values:

Overrides:
toString in class Object

toString

public String toString(char variableName)
Returns a string of form #.#variableName^#. For example, the following are all possible return values if variableName is 'x':

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


Copyright © 2009. All Rights Reserved.