|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.nrao.sss.math.Polynomial
public class 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) |
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 |
---|
public Polynomial()
Method Detail |
---|
public void set(String polynomialString)
polynomialString
.
See parse(String)
for the expected format of
polynomialString
.
polynomialString
- a string that will be used to configure
this polynomial.
IllegalArgumentException
- if polynomialString
is not in
the expected form.public void add(PolynomialTerm term)
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.
term
- the term to be added to this polynomial. If this
value is null this polynomial is not altered.public void add(Polynomial other)
other
- the polynomial to be added to this one.public void replace(PolynomialTerm newTerm)
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
.
newTerm
- a replacement for the term of this polynomial that
has an exponent of newTerm.getExponent()
.public boolean hasTerm(int 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.
exponent
- the exponent of a term whose existence is sought.
public SortedMap<Integer,PolynomialTerm> getTerms()
public PolynomialTerm getTerm(int 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).
exponent
- the exponent for which a term is sought.
public PolynomialTerm getNonNullTerm(int 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).
exponent
- the exponent for which a term is sought.
public int order()
public int smallestExponent()
public int getNumberOfTerms()
public double calculateFor(double number)
number
as the
value of the independent variable.
number
.public Polynomial createDerivative()
public void clear()
public static Polynomial parse(String polynomialString)
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.
polynomialString
- a string that will be converted into
a polynomial.
IllegalArgumentException
- if polynomialString
is not in
the expected form.public Polynomial clone()
clone
in class Object
public boolean equals(Object o)
o
is equal to this polynomial.
equals
in class Object
public int hashCode()
hashCode
in class Object
public int compareTo(Polynomial other)
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^5For 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^5The 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.
compareTo
in interface Comparable<Polynomial>
other
- the polynomial to which this one is compared.
public String toString()
The individual terms are represented as documented in
PolynomialTerm.toString()
. Each term is separated
by " + " (a single space, a '+' character, and another
single space).
toString
in class Object
public String toString(char variableName)
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).
variableName
- the character to use as the independent variable.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |