Class PolynomialFunction
- java.lang.Object
-
- uk.ac.starlink.dpac.math.PolynomialFunction
-
public class PolynomialFunction extends java.lang.Object
Immutable representation of a real polynomial function with real coefficients.Horner's Method is used to evaluate the function.
-
-
Constructor Summary
Constructors Constructor Description PolynomialFunction(double[] c)
Construct a polynomial with the given coefficients.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
degree()
Returns the degree of the polynomial.boolean
equals(java.lang.Object obj)
protected static double
evaluate(double[] coefficients, double argument)
Uses Horner's Method to evaluate the polynomial with the given coefficients at the argument.double[]
getCoefficients()
Returns a copy of the coefficients array.int
hashCode()
java.lang.String
toString()
Returns a string representation of the polynomial.double
value(double x)
Compute the value of the function for the given argument.
-
-
-
Constructor Detail
-
PolynomialFunction
public PolynomialFunction(double[] c)
Construct a polynomial with the given coefficients. The first element of the coefficients array is the constant term. Higher degree coefficients follow in sequence. The degree of the resulting polynomial is the index of the last non-null element of the array, or 0 if all elements are null.The constructor makes a copy of the input array and assigns the copy to the coefficients property.
- Parameters:
c
- Polynomial coefficients.- Throws:
NullArgumentException
- ifc
isnull
.NoDataException
- ifc
is empty.
-
-
Method Detail
-
value
public double value(double x)
Compute the value of the function for the given argument.The value returned is
coefficients[n] * x^n + ... + coefficients[1] * x + coefficients[0]
- Parameters:
x
- Argument for which the function value should be computed.- Returns:
- the value of the polynomial at the given point.
-
degree
public int degree()
Returns the degree of the polynomial.- Returns:
- the degree of the polynomial.
-
getCoefficients
public double[] getCoefficients()
Returns a copy of the coefficients array.Changes made to the returned copy will not affect the coefficients of the polynomial.
- Returns:
- a fresh copy of the coefficients array.
-
evaluate
protected static double evaluate(double[] coefficients, double argument)
Uses Horner's Method to evaluate the polynomial with the given coefficients at the argument.- Parameters:
coefficients
- Coefficients of the polynomial to evaluate.argument
- Input value.- Returns:
- the value of the polynomial.
- Throws:
NoDataException
- ifcoefficients
is empty.NullArgumentException
- ifcoefficients
isnull
.
-
toString
public java.lang.String toString()
Returns a string representation of the polynomial.The representation is user oriented. Terms are displayed lowest degrees first. The multiplications signs, coefficients equals to one and null terms are not displayed (except if the polynomial is 0, in which case the 0 constant term is displayed). Addition of terms with negative coefficients are replaced by subtraction of terms with positive coefficients except for the first displayed term (i.e. we display
-3
for a constant negative polynomial, but1 - 3 x + x^2
if the negative coefficient is not the first one displayed).- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of the polynomial.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
-