public class Polynomial extends java.lang.Object implements java.lang.Comparable<Polynomial>
Polynomial
class represents a polynomial of a single
indeterminate with double coefficients. A polynomial is an an expression
consisting of variables with non-negative integer exponents and real
coefficients. The set of operations supported by the Polynomial
class
includes: addition, subtraction, multiplication, function composition,
polynomial derivative, and evaluation.
Examples of polynomials with a single indeterminate, x, are:
p(x) = x^2 - 5.0x + 1.0 p(x) = x^6 + 4.1x^4 + 0.5x - 7.2 p(x) = 1.0
Modifier and Type | Field and Description |
---|---|
private double[] |
cf
Array of the polynomial coefficients.
|
private int |
degree
The degree of the polynomial.
|
Constructor and Description |
---|
Polynomial()
Creates a polynomial with zero degree and one zero coefficient.
|
Polynomial(double[] cf)
Creates a new polynomial from array of coefficients.
|
Polynomial(double a,
int i)
Creates a new polynomial
a * x^i |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Polynomial q)
Compares this polynomial
p and the other polynomial q by
their degree. |
Polynomial |
compose(Polynomial q)
Returns the composition of this polynomial
p and the specified
polynomial q . |
Polynomial |
derive()
Returns the result of deriving this polynomial
p . |
boolean |
equals(java.lang.Object obj)
Compares this polynomial to the other object
obj . |
double |
evaluate(double x)
Returns the result of evaluating this polynomial
p at the value x. |
private void |
findDegree()
Finds the polynomial
degree , which is the largest exponent with non
zero coefficient. |
int |
getDegree()
Returns the degree of the polynomial.
|
int |
hashCode()
Returns a hash code for this polynomial.
|
Polynomial |
minus(Polynomial q)
Returns the result of subtracting the specified polynomial
q from
this polynomial p . |
Polynomial |
plus(Polynomial q)
Returns the sum of this polynomial
p } and the other polynomial
q the argument. |
Polynomial |
times(Polynomial q)
Returns the product of this polynomial
p and the specified polynomial
q . |
java.lang.String |
toString()
Return a string representation of this polynomial in the standard form.
|
private double[] cf
p(x) = cf[0] * x^0 + ... + cf[n-1] * x^(n-1)
private int degree
degree
, implement and call findDegree()
method every
time the polynomial is created or an operation is performedpublic Polynomial(double a, int i)
a * x^i
a
- the leading coefficienti
- the exponentjava.lang.IllegalArgumentException
- if i
is negativepublic Polynomial(double[] cf)
cf[0] * x^0 + ... + cf[n-1] * x^(n-1)where
n
is the length of the coefficients array.cf
- the coefficients arrayjava.lang.IllegalArgumentException
- if cf
is emptypublic Polynomial()
p(x) = 0
private void findDegree()
degree
, which is the largest exponent with non
zero coefficient. If all the coefficients are zero, the degree is zero.public int getDegree()
public Polynomial plus(Polynomial q)
p
} and the other polynomial
q
the argument.q
- the other polynomial(p(x) + q(x))
public Polynomial minus(Polynomial q)
q
from
this polynomial p
.q
- the other polynomial(p(x) - q(x))
public Polynomial times(Polynomial q)
p
and the specified polynomial
q
.q
- the other polynomial(p(x) * q(x))
public Polynomial compose(Polynomial q)
p
and the specified
polynomial q
.q
- the other polynomial(p(q(x)))
public Polynomial derive()
p
.p'(x)
public double evaluate(double x)
p
at the value x.x
- the value at which to evaluate the polynomial(p(x))
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
obj
. The result is true
if and only if the argument is a polynomial object having the same degree and
coefficients as this polynomial.equals
in class java.lang.Object
obj
- an object to comparetrue
if this polynomial equals to the specified object; and
false
otherwisepublic int compareTo(Polynomial q)
p
and the other polynomial q
by
their degree. If the degrees are equal, the polynomial coefficients are
compared, and so on.compareTo
in interface java.lang.Comparable<Polynomial>
q
- the other polynomial-1
if this polynomial is less than the argument
polynomial; +1
if this polynomial is greater than the
argument polynomial; and 0
if this polynomial is equal to the
argument polynomial.public java.lang.String toString()
new Polynomial(); //0.0 new Polynomial(1,0) //1.0 new Polynomial(1,1); //1.0x new Polynomial(new double[] {1,1}); //1.0x + 1.0 new Polynomial(new double[] {0,0,2}); //2.0x^2 new Polynomial(new double[] {0,1,2}); //2.0x^2 + 1.0x new Polynomial(new double[] {1,0,2}); //2.0x^2 + 1.0 new Polynomial(new double[] {0.0001213,1,2}); //2.0x^2 + 1.0x + 1.213E-4
toString
in class java.lang.Object