public final class RatTerm extends Object
A RatTerm, t, can be notated by the pair (C . E), where C is the coefficient of t, and E is the exponent of t.
The zero RatTerm, (0 . 0), is the only RatTerm that may have a zero coefficient. For example, (0 . 7) is an invalid RatTerm and an attempt to construct such a RatTerm (through the constructor or arithmetic operations on existing RatTerms) will return the semantically equivalent RatTerm (0 . 0). For example, (1 . 7) + (-1 . 7) = (0 . 0).
(0 . 0), (1 . 0), (1 . 1), (1 . 3), (3/4 . 17), (7/2 . -1), and (NaN . 74) are all valid RatTerms, corresponding to the polynomial terms "0", "1", "x", "x^3", "3/4*x^17", "7/2*x^-1" and "NaN*x^74", respectively.
Modifier and Type | Field and Description |
---|---|
static RatTerm |
NaN
A constant holding a Not-a-Number (NaN) value of type RatTerm
|
static RatTerm |
ZERO
A constant holding a zero value of type RatTerm
|
Modifier and Type | Method and Description |
---|---|
RatTerm |
add(RatTerm p)
Addition operation.
|
RatTerm |
antiDifferentiate()
Returns the antiderivative of this RatTerm.
|
RatTerm |
differentiate()
Return the derivative of this RatTerm.
|
RatTerm |
div(RatTerm p)
Division operation.
|
boolean |
equals(Object obj)
Standard equality operation.
|
double |
eval(double d)
Returns the value of this RatTerm, evaluated at d.
|
RatNum |
getCoeff()
Gets the coefficient of this RatTerm.
|
int |
getExpt()
Gets the exponent of this RatTerm.
|
int |
hashCode()
Standard hashCode function.
|
boolean |
isNaN()
Returns true if this RatTerm is not-a-number.
|
boolean |
isZero()
Returns true if this RatTerm is equal to 0.
|
RatTerm |
mul(RatTerm p)
Multiplication operation.
|
RatTerm |
negate()
Negation operation.
|
RatTerm |
sub(RatTerm p)
Subtraction operation.
|
String |
toString()
Returns a string representation of this RatTerm.
|
static RatTerm |
valueOf(String termStr)
Builds a new RatTerm, given a descriptive String.
|
public RatNum getCoeff()
public int getExpt()
public boolean isNaN()
public boolean isZero()
public double eval(double d)
d
- The value at which to evaluate this term.public RatTerm negate()
public RatTerm add(RatTerm p)
p
- The other value to be added.IllegalArgumentException
- if (this.expt != arg.expt) and neither argument is zero or
NaN.public RatTerm sub(RatTerm p)
p
- The value to be subtracted.IllegalArgumentException
- if (this.expt != arg.expt) and neither argument is zero or
NaN.public RatTerm mul(RatTerm p)
p
- The other value to be multiplied.public RatTerm div(RatTerm p)
p
- The divisor.public RatTerm differentiate()
Given a term, a*x^b, the derivative of the term is: (a*b)*x^(b-1) for b > 0 and 0 for b == 0 (Do not worry about the case when b < 0. The caller of this function, RatPoly, contains a rep. invariant stating that b is never less than 0.)
public RatTerm antiDifferentiate()
Given a term, a*x^b, (where b >= 0) the antiderivative of the term is: a/(b+1)*x^(b+1) (Do not worry about the case when b < 0. The caller of this function, RatPoly, contains a rep. invariant stating that b is never less than 0.)
public String toString()
toString
in class Object
There is no whitespace in the returned string.
If the term is itself zero, the returned string will just be "0".
If this.isNaN(), then the returned string will be just "NaN"
The string for a non-zero, non-NaN RatTerm is in the form "C*x^E" where C
is a valid string representation of a RatNum (see RatNum
's
toString method) and E is an integer. UNLESS: (1) the exponent E is zero,
in which case T takes the form "C" (2) the exponent E is one, in which
case T takes the form "C*x" (3) the coefficient C is one, in which case T
takes the form "x^E" or "x" (if E is one) or "1" (if E is zero).
Valid example outputs include "3/2*x^2", "-1/2", "0", and "NaN".
public static RatTerm valueOf(String termStr)
termStr
- A string of the format described in the @requires clause.Valid inputs include "0", "x", and "-5/3*x^3", and "NaN".
public int hashCode()