Class RatPoly
- java.lang.Object
-
- hw4.RatPoly
-
public final class RatPoly extends java.lang.ObjectRatPoly represents an immutable single-variate polynomial expression. RatPolys are sums of RatTerms with non-negative exponents.Examples of RatPolys include "0", "x-10", and "x^3-2*x^2+5/3*x+3", and "NaN".
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RatPolyadd(RatPoly p)Addition operation.RatPolyantiDifferentiate(RatNum integrationConstant)Returns the antiderivative of this RatPoly.intdegree()Returns the degree of this RatPoly.RatPolydifferentiate()Return the derivative of this RatPoly.RatPolydiv(RatPoly p)Division operation (truncating).booleanequals(java.lang.Object obj)Standard equality operation.doubleeval(double d)Returns the value of this RatPoly, evaluated at d.RatTermgetTerm(int deg)Gets the RatTerm associated with degree 'deg'inthashCode()Standard hashCode function.doubleintegrate(double lowerBound, double upperBound)Returns the integral of this RatPoly, integrated from lowerBound to upperBound.booleanisNaN()Returns true if this RatPoly is not-a-number.booleanisZero()Returns whether this is the zero polynomial.RatNumleadingCoeff()Returns the coefficient of the largest degree term.RatPolymul(RatPoly p)Multiplication operation.RatPolynegate()Return the additive inverse of this RatPoly.RatPolysub(RatPoly p)Subtraction operation.java.lang.StringtoString()Returns a string representation of this RatPoly.static RatPolyvalueOf(java.lang.String polyStr)Builds a new RatPoly, given a descriptive String.
-
-
-
Constructor Detail
-
RatPoly
public RatPoly()
- Spec.effects:
- Constructs a new Poly, "0".
-
RatPoly
public RatPoly(RatTerm rt)
- Parameters:
rt- The single term which the new RatPoly equals.- Spec.effects:
- Constructs a new Poly equal to "rt". If rt.getCoeff() is zero, constructs a "0" polynomial.
- Spec.requires:
- rt.getExpt() >= 0
-
RatPoly
public RatPoly(int c, int e)- Parameters:
c- The constant in the term which the new RatPoly equals.e- The exponent in the term which the new RatPoly equals.- Spec.effects:
- Constructs a new Poly equal to "c*x^e". If c is zero, constructs a "0" polynomial.
- Spec.requires:
- e >= 0
-
-
Method Detail
-
degree
public int degree()
Returns the degree of this RatPoly.- Returns:
- the largest exponent with a non-zero coefficient, or 0 if this is "0".
- Spec.requires:
- !this.isNaN()
-
leadingCoeff
public RatNum leadingCoeff()
Returns the coefficient of the largest degree term.- Returns:
- the coefficient on the term with largest degree
-
getTerm
public RatTerm getTerm(int deg)
Gets the RatTerm associated with degree 'deg'- Parameters:
deg- The degree for which to find the corresponding RatTerm.- Returns:
- the RatTerm of degree 'deg'. If there is no term of degree 'deg' in this poly, then returns the zero RatTerm.
- Spec.requires:
- !this.isNaN()
-
isNaN
public boolean isNaN()
Returns true if this RatPoly is not-a-number.- Returns:
- true if and only if this has some coefficient = "NaN".
-
isZero
public boolean isZero()
Returns whether this is the zero polynomial.- Returns:
- true iff this is the zero polynomial
-
negate
public RatPoly negate()
Return the additive inverse of this RatPoly.- Returns:
- a RatPoly equal to "0 - this"; if this.isNaN(), returns some r such that r.isNaN()
-
add
public RatPoly add(RatPoly p)
Addition operation.- Parameters:
p- The other value to be added.- Returns:
- a RatPoly, r, such that r = "this + p"; if this.isNaN() or p.isNaN(), returns some r such that r.isNaN()
- Spec.requires:
- p != null
-
sub
public RatPoly sub(RatPoly p)
Subtraction operation.- Parameters:
p- The value to be subtracted.- Returns:
- a RatPoly, r, such that r = "this - p"; if this.isNaN() or p.isNaN(), returns some r such that r.isNaN()
- Spec.requires:
- p != null
-
mul
public RatPoly mul(RatPoly p)
Multiplication operation.- Parameters:
p- The other value to be multiplied.- Returns:
- a RatPoly, r, such that r = "this * p"; if this.isNaN() or p.isNaN(), returns some r such that r.isNaN()
- Spec.requires:
- p != null
-
div
public RatPoly div(RatPoly p)
Division operation (truncating).- Parameters:
p- The divisor.- Returns:
- a RatPoly, q, such that q = "this / p"; if p = 0 or this.isNaN()
or p.isNaN(), returns some q such that q.isNaN()
Division of polynomials is defined as follows: Given two polynomials u and v, with v != "0", we can divide u by v to obtain a quotient polynomial q and a remainder polynomial r satisfying the condition u = "q * v + r", where the degree of r is strictly less than the degree of v, the degree of q is no greater than the degree of u, and r and q have no negative exponents.
For the purposes of this class, the operation "u / v" returns q as defined above.
The following are examples of div's behavior: "x^3-2*x+3" / "3*x^2" = "1/3*x" (with r = "-2*x+3"). "x^2+2*x+15 / 2*x^3" = "0" (with r = "x^2+2*x+15"). "x^3+x-1 / x+1 = x^2-x+2 (with r = "-3").
Note that this truncating behavior is similar to the behavior of integer division on computers.
- Spec.requires:
- p != null
-
differentiate
public RatPoly differentiate()
Return the derivative of this RatPoly.- Returns:
- a RatPoly, q, such that q = dy/dx, where this == y. In other
words, q is the derivative of this. If this.isNaN(), then return
some q such that q.isNaN()
The derivative of a polynomial is the sum of the derivative of each term.
-
antiDifferentiate
public RatPoly antiDifferentiate(RatNum integrationConstant)
Returns the antiderivative of this RatPoly.- Parameters:
integrationConstant- The constant of integration to use when computating the antiderivative.- Returns:
- a RatPoly, q, such that dq/dx = this and the constant of
integration is "integrationConstant" In other words, q is the
antiderivative of this. If this.isNaN() or
integrationConstant.isNaN(), then return some q such that
q.isNaN()
The antiderivative of a polynomial is the sum of the antiderivative of each term plus some constant.
- Spec.requires:
- integrationConstant != null
-
integrate
public double integrate(double lowerBound, double upperBound)Returns the integral of this RatPoly, integrated from lowerBound to upperBound.The Fundamental Theorem of Calculus states that the definite integral of f(x) with bounds a to b is F(b) - F(a) where dF/dx = f(x) NOTE: Remember that the lowerBound can be higher than the upperBound.
- Parameters:
lowerBound- The lower bound of integration.upperBound- The upper bound of integration.- Returns:
- a double that is the definite integral of this with bounds of integration between lowerBound and upperBound. If this.isNaN(), or either lowerBound or upperBound is Double.NaN, return Double.NaN.
-
eval
public double eval(double d)
Returns the value of this RatPoly, evaluated at d.- Parameters:
d- The value at which to evaluate this polynomial.- Returns:
- the value of this polynomial when evaluated at 'd'. For example, "x+2" evaluated at 3 is 5, and "x^2-x" evaluated at 3 is 6. if (this.isNaN() == true), return Double.NaN
-
toString
public java.lang.String toString()
Returns a string representation of this RatPoly.- Overrides:
toStringin classjava.lang.Object- Returns:
- A String representation of the expression represented by this,
with the terms sorted in order of degree from highest to lowest.
There is no whitespace in the returned string.
If the polynomial 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 poly is in the form "(-)T(+|-)T(+|-)...", where "(-)" refers to a possible minus sign, if needed, and "(+|-)" refer to either a plus or minus sign, as needed. For each term, T takes the form "C*x^E" or "C*x" where C > 0, UNLESS: (1) the exponent E is zero, in which case T takes the form "C", or (2) the coefficient C is one, in which case T takes the form "x^E" or "x". In cases were both (1) and (2) apply, (1) is used.
Valid example outputs include "x^17-3/2*x^2+1", "-x+1", "-1/2", and "0".
-
valueOf
public static RatPoly valueOf(java.lang.String polyStr)
Builds a new RatPoly, given a descriptive String.- Parameters:
polyStr- A string of the format described in the @spec.requires clause.- Returns:
- a RatPoly p such that p.toString() = polyStr
- Spec.requires:
- 'polyStr' is an instance of a string with no spaces that expresses a poly in the form defined in the toString() method, except that the ordering of the terms by the degrees is not necessary. Valid inputs include "0", "x-10", and "x^3-2*x^2+5/3*x+3", and "NaN".
-
hashCode
public int hashCode()
Standard hashCode function.- Overrides:
hashCodein classjava.lang.Object- Returns:
- an int that all objects equal to this will also return.
-
equals
public boolean equals(java.lang.Object obj)
Standard equality operation.- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- The object to be compared for equality.- Returns:
- true if and only if 'obj' is an instance of a RatPoly and 'this' and 'obj' represent the same rational polynomial. Note that all NaN RatPolys are equal.
-
-