Package org.dalton.polyfun
Class Polynomial
- java.lang.Object
-
- org.dalton.polyfun.Polynomial
-
public class Polynomial extends java.lang.Object
A Polynomial is a polynomial in X with a certain degree and a set of coefficients. These coefficients may be specific values (doubles) or abstract variables depending on the situation. In the documentation for this class, "term" refers to a term of a polynomial in the mathematical sense, while "Term" refers to the java object. The coefficients, whether numbers or letters or a combination, are always stored as Coefs in an array of Coefs called "coeffs." The index of the array corresponds to the degree of the term for which that Coef belongs.Example: mathematical object
p(x) = (3a+1)x^3 + (ab-ac)x + c_2
Java object degree = 3 Coefs = {c_2, ab-ac, 0, 3a+1} (an array of Coefs. Note the order of the Coefs in the array)
Students will use this code and expand on it when developing the VDM method(s) and other Polynomial manipulation methods.
- Author:
- David Gomprecht (wrote the original Polynomial object), Katie Jergens (wrote refactored version based on Dr. Gomprecht's object, maintaining the interface)
-
-
Constructor Summary
Constructors Constructor Description Polynomial()
Default constructor.Polynomial(char letter, int degree)
Construct a polynomial with generic coefficients.Polynomial(double constant)
Constructs a zeroth degree or constant polynomial equal to the constant parameter.Polynomial(double[] numericalCoefficients)
Constructor where you pass an array of numerical coefficients used to initialize Terms, which creates the Coef array.Polynomial(double numericalCoefficient, int degree)
Constructs a polynomial with one term and a specific degree.Polynomial(int degree)
Construct a Polynomial by setting the degree.Polynomial(org.dalton.polyfun.Atom atom)
Constructs a zeroth degree polynomial which consists of an abstract constant.Polynomial(org.dalton.polyfun.Atom atom, int degree)
Constructs a polynomial of a certain degree with only one term whose coefficient is an abstract constant.Polynomial(org.dalton.polyfun.Coef coef)
Constructs a zeroth degree polynomial which consists of one Coef.Polynomial(org.dalton.polyfun.Coef[] coefs)
Construct a Polynomial by specifying an array of CoefsPolynomial(org.dalton.polyfun.Coef coef, int degree)
Constructs a polynomial of a certain degree with only one term whose coefficient is a Coef.Polynomial(org.dalton.polyfun.Term term)
Constructs a zeroth degree polynomial which consists of an abstract constant(s) and a double (i.e.Polynomial(org.dalton.polyfun.Term term, int degree)
Constructs a polynomial of a certain degree with only one term whose coefficient is an abstract constant(s) and a double (i.e.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Polynomial
addTangent()
Adds the Polynomial p(x) = mx + b to a Polynomialdouble
eval(double x)
Think of this method as plugging in a numeric value into a polynomial function.org.dalton.polyfun.Coef
evaluateToCoef(double value)
Think of this method as plugging in a numeric value into a polynomial function.double
evaluateWith(double x)
Deprecated.Use {eval(double)
} instead.org.dalton.polyfun.Coef
getCoefAt(int index)
Retrieve a specific coefficient by specifying the x which corresponds with that Coef.double[]
getCoefficientArray()
Return all the numerical coefficient of this polynomial, as long as all the coefficients are numbers.double
getCoefficientAtTerm(int termNumber)
Return the numerical coefficient of the x term at the given degree, as long as that coefficient is a number.org.dalton.polyfun.Coef[]
getCoefficients()
Deprecated.usegetCoefs()
instead.org.dalton.polyfun.Coef[]
getCoefs()
Get the Coef array.int
getDegree()
Gets the degree of the Polynomial.boolean
isPlottable()
Determines if all the coefficients are constant, meaning the polynomial can be represented in a graph.Polynomial
minus(Polynomial polynomial)
Subtracts two GenPolynomials.Polynomial
of(Polynomial polynomial)
Composes two GenPolynomials.Polynomial
plus(Polynomial polynomial)
Add two GenPolynomials by adding the coefficients of the corresponding terms.Polynomial
raiseTo(int power)
void
setCoefficients(double[] nums)
Deprecated.usesetCoefs(double[])
instead.void
setCoefficients(org.dalton.polyfun.Coef[] coefs)
Deprecated.usesetCoefs(Coef[])
()} instead.void
setCoefs(double[] coefficients)
Set the Coefs array of the polynomial.void
setCoefs(org.dalton.polyfun.Coef[] coefs)
Set the Coefs array of the polynomial.void
setDegree(int degree)
Sets the degree of the Polynomial.Polynomial
times(double scalar)
Multiply a polynomial by a scalar by multiplying all the Coefs by the scalar.Polynomial
times(org.dalton.polyfun.Coef coef)
Multiply a polynomial by a Coef by multiplying all the Coefs by the Coef.Polynomial
times(Polynomial polynomial)
Multiply a polynomial by a polynomial.Polynomial
to(int power)
Raise to a power.java.lang.String
toString()
Returns a printable string.
-
-
-
Constructor Detail
-
Polynomial
public Polynomial()
Default constructor.- Since:
- 1.0.0
-
Polynomial
public Polynomial(org.dalton.polyfun.Coef[] coefs)
Construct a Polynomial by specifying an array of Coefs- Parameters:
coefs
- The array of Coefs.- Since:
- 1.0.0
-
Polynomial
public Polynomial(double[] numericalCoefficients)
Constructor where you pass an array of numerical coefficients used to initialize Terms, which creates the Coef array.- Parameters:
numericalCoefficients
- array of numerical coefficients- Since:
- 1.0.0
-
Polynomial
public Polynomial(int degree)
Construct a Polynomial by setting the degree.- Parameters:
degree
- The degree of the polynomial- Since:
- 1.0.0
-
Polynomial
public Polynomial(double constant)
Constructs a zeroth degree or constant polynomial equal to the constant parameter. Note: This has a very different behavior than Polynomial(int degree)- Parameters:
constant
- The constant term of the zeroth degree polynomial.- Since:
- 1.0.0
-
Polynomial
public Polynomial(double numericalCoefficient, int degree)
Constructs a polynomial with one term and a specific degree. Example: coefficient = 2.0 deg = 3 creates p(x) = 2.0x^3.- Parameters:
numericalCoefficient
- The coefficientdegree
- The degree of the term and the polynomial- Since:
- 1.0.0
-
Polynomial
public Polynomial(org.dalton.polyfun.Atom atom)
Constructs a zeroth degree polynomial which consists of an abstract constant. For example: with atom = a_0 this creates p(x) = a_0.- Parameters:
atom
- The Atom which becomes the polynomial- Since:
- 1.0.0
-
Polynomial
public Polynomial(org.dalton.polyfun.Atom atom, int degree)
Constructs a polynomial of a certain degree with only one term whose coefficient is an abstract constant. For example: with atom = a_2 and degree = 2 this creates p(x) = (a_2)x^2- Parameters:
atom
- The Atom which becomes the coefficientdegree
- The degree of the polynomial- Since:
- 1.0.0
-
Polynomial
public Polynomial(org.dalton.polyfun.Term term)
Constructs a zeroth degree polynomial which consists of an abstract constant(s) and a double (i.e. a Term). For example: with term = 3ab this creates p(x) = 3ab.- Parameters:
term
- Term is an abstract constant and a coefficient.- Since:
- 1.0.0
-
Polynomial
public Polynomial(org.dalton.polyfun.Term term, int degree)
Constructs a polynomial of a certain degree with only one term whose coefficient is an abstract constant(s) and a double (i.e. a Term). For example: with term = 3ab and deg = 2 this creates p(x) = (3ab)x^2- Parameters:
term
-degree
-- Since:
- 1.0.0
-
Polynomial
public Polynomial(org.dalton.polyfun.Coef coef)
Constructs a zeroth degree polynomial which consists of one Coef. For example: with coef = 3ab+c this creates p(x) = 3ab+c.- Parameters:
coef
- A Coef object is an array of Terms, which becomes the coefficient.- Since:
- 1.0.0
-
Polynomial
public Polynomial(org.dalton.polyfun.Coef coef, int degree)
Constructs a polynomial of a certain degree with only one term whose coefficient is a Coef. For example: with newcoef = 3ab+c and deg = 2 this creates p(x) = (3ab+c)x^2- Parameters:
coef
- A Coef object is an array of Terms, which becomes the coefficient.degree
- The degree of the polynomial- Since:
- 1.0.0
-
Polynomial
public Polynomial(char letter, int degree)
Construct a polynomial with generic coefficients. Example: letter = a, deg = 3 creates p(x) = (a_3)x^3 + (a_2)x^2 + (a_1)x + (a_0)- Parameters:
letter
- The letter for the base of each coefficient. Subscripts are automatically applied.degree
- The degree of the polynomial- Since:
- 1.0.0
-
-
Method Detail
-
getDegree
public int getDegree()
Gets the degree of the Polynomial.- Returns:
- degree The degree of the polynomial
- Since:
- 1.0.0
-
getCoefAt
public org.dalton.polyfun.Coef getCoefAt(int index)
Retrieve a specific coefficient by specifying the x which corresponds with that Coef. Returns a Coef.- Parameters:
index
-- Returns:
- The Coef object at the given degree.
- Since:
- 1.1.0
-
getCoefficientAtTerm
public double getCoefficientAtTerm(int termNumber)
Return the numerical coefficient of the x term at the given degree, as long as that coefficient is a number. Example,for the polynomial: (3.0)X^4 + (5.0a_1)X^2 + 8 getCoefficientFor(4) returns 3.0 because x^4 has a coefficient of 3.0 getCoefficientFor(2) throws an Exception because x^4 has coefficient that is not a double.- Parameters:
termNumber
-- Returns:
- The numerical coefficient of the x term at the given degree.
- Since:
- 1.1.1
-
getCoefficientArray
public double[] getCoefficientArray()
Return all the numerical coefficient of this polynomial, as long as all the coefficients are numbers. Example,for the polynomial: (3.0)X^4 + (5.0)X^2 + 8.0 getCoefficients returns new double[]{8.0, 0.0, 5.0, 0.0, 3.0} - the x^0 coefficient is 8 - the x^1 coefficient is 0 - the x^2 coefficient is 5 - the x^3 coefficient is 0 - the x^4 coefficient is 3 Example 2,for the polynomial: (3.0)X^4 + (5.0q_1 + q_0)X^2 + 8.0 getCoefficients throws an Exception because x^2 term has a coefficient that is not a double.- Returns:
- The numerical coefficients of the polynomial.
- Since:
- 1.1.1
-
getCoefficients
@Deprecated public org.dalton.polyfun.Coef[] getCoefficients()
Deprecated.usegetCoefs()
instead.Doesn't follow naming convention.- Since:
- 1.0.0
-
getCoefs
public org.dalton.polyfun.Coef[] getCoefs()
Get the Coef array.- Returns:
- the array of Coefs.
- Since:
- 1.0.0
-
setDegree
public void setDegree(int degree)
Sets the degree of the Polynomial.- Parameters:
degree
- The degree to set (also specifies length of coeffs array)- Since:
- 1.0.0
-
setCoefficients
@Deprecated public void setCoefficients(org.dalton.polyfun.Coef[] coefs)
Deprecated.usesetCoefs(Coef[])
()} instead.Doesn't follow naming convention.- Since:
- 1.0.0
-
setCoefficients
@Deprecated public void setCoefficients(double[] nums)
Deprecated.usesetCoefs(double[])
instead.Doesn't follow naming convention.- Since:
- 1.0.0
-
setCoefs
public void setCoefs(org.dalton.polyfun.Coef[] coefs)
Set the Coefs array of the polynomial.- Parameters:
coefs
- Coef array to copy into the polynomial. Also changes the degree.- Since:
- 1.1.0
-
setCoefs
public void setCoefs(double[] coefficients)
Set the Coefs array of the polynomial.- Parameters:
coefficients
- Constants to make Coef objects out of.- Since:
- 1.1.0
-
plus
public Polynomial plus(Polynomial polynomial)
Add two GenPolynomials by adding the coefficients of the corresponding terms.- Parameters:
polynomial
- Polynomial to add- Returns:
- the sum
- Since:
- 1.0.0
-
minus
public Polynomial minus(Polynomial polynomial)
Subtracts two GenPolynomials.- Parameters:
polynomial
- Polynomial to substract- Returns:
- The difference
- Since:
- 1.0.0
-
times
public Polynomial times(double scalar)
Multiply a polynomial by a scalar by multiplying all the Coefs by the scalar.- Parameters:
scalar
- to multiply- Returns:
- the product
- Since:
- 1.0.0
-
times
public Polynomial times(org.dalton.polyfun.Coef coef)
Multiply a polynomial by a Coef by multiplying all the Coefs by the Coef.- Parameters:
coef
- to multiply- Returns:
- the product
- Since:
- 1.0.0
-
times
public Polynomial times(Polynomial polynomial)
Multiply a polynomial by a polynomial.- Parameters:
polynomial
- to multiply- Returns:
- the product
- Since:
- 1.0.0
-
to
public Polynomial to(int power)
Raise to a power.- Parameters:
power
- to raise by- Returns:
- Polynomial the result.
- Since:
- 1.0.0
-
raiseTo
public Polynomial raiseTo(int power)
-
addTangent
public Polynomial addTangent()
Adds the Polynomial p(x) = mx + b to a Polynomial- Returns:
- The new GenPoynomial with mx + b added to it.
- Since:
- 1.0.0
-
of
public Polynomial of(Polynomial polynomial)
Composes two GenPolynomials. Example: if this = p(x) and poly = q(x), this.of(poly) returns p[q(x)]- Parameters:
polynomial
- The inner polynomial- Returns:
- The new polynomial which is the composition
- Since:
- 1.0.0
-
evaluateToCoef
public org.dalton.polyfun.Coef evaluateToCoef(double value)
Think of this method as plugging in a numeric value into a polynomial function. For example, if p(x) = x2 + 5, and x = 2, then p.evaluate(2) would essentially evaluate p(2) = 22 + 5 = 9. Although the answer will in many cases be a number, a double, it is possible to have other variables as coefficients. This forces the .evaluate(double x) to return a Coef. Use methods for Coefs to extract the double from it.- Parameters:
value
- The value to plug into the polynomial- Returns:
- Coef the result
- Since:
- 1.1.0
-
eval
public double eval(double x)
Think of this method as plugging in a numeric value into a polynomial function. For example, if p(x) = x2 + 5, and x = 2, then p.evaluate(2) would essentially evaluate p(2) = 22 + 5 = 9.- Parameters:
x
- The value to plug into the polynomial- Returns:
- double the result
- Since:
- 1.2.0
-
evaluateWith
@Deprecated public double evaluateWith(double x)
Deprecated.Use {eval(double)
} instead.Think of this method as plugging in a numeric value into a polynomial function. For example, if p(x) = x2 + 5, and x = 2, then p.evaluate(2) would essentially evaluate p(2) = 22 + 5 = 9.- Parameters:
x
- The value to plug into the polynomial- Returns:
- double the result
- Since:
- 1.1.0
-
isPlottable
public boolean isPlottable()
Determines if all the coefficients are constant, meaning the polynomial can be represented in a graph.- Returns:
- true if plottable
- Since:
- 1.0.0
-
toString
public java.lang.String toString()
Returns a printable string.- Overrides:
toString
in classjava.lang.Object
- Returns:
- String representing the polynomial.
- Since:
- 1.1.0
-
-