Class PolynomialFunction
- All Implemented Interfaces:
Serializable
,UnivariateDifferentiableFunction
,FieldUnivariateFunction
,UnivariateFunction
- Direct Known Subclasses:
SmoothStepFactory.SmoothStepFunction
Horner's Method is used to evaluate the function.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Dedicated parametric polynomial class. -
Constructor Summary
ConstructorDescriptionPolynomialFunction
(double... c) Construct a polynomial with the given coefficients. -
Method Summary
Modifier and TypeMethodDescriptionAdd a polynomial to the instance.Returns an anti-derivative of this polynomial, with 0 constant term.int
degree()
Returns the degree of the polynomial.protected static double[]
differentiate
(double[] coefficients) Returns the coefficients of the derivative of the polynomial with the given coefficients.boolean
protected static double
evaluate
(double[] coefficients, double argument) Uses Horner's Method to evaluate the polynomial with the given coefficients at the argument.double[]
Returns a copy of the coefficients array.int
hashCode()
double
integrate
(double lower, double upper) Returns the definite integral of this polymomial over the given interval.Multiply the instance by a polynomial.negate()
Negate the instance.Returns the derivative as aPolynomialFunction
.Subtract a polynomial from the instance.toString()
Returns a string representation of the polynomial.double
value
(double x) Compute the value of the function for the given argument.<T extends Derivative<T>>
Tvalue
(T t) Compute the value for the function.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.hipparchus.analysis.FieldUnivariateFunction
toCalculusFieldUnivariateFunction
-
Constructor Details
-
PolynomialFunction
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
.MathIllegalArgumentException
- ifc
is empty.
-
-
Method Details
-
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]
- Specified by:
value
in interfaceUnivariateFunction
- Parameters:
x
- Argument for which the function value should be computed.- Returns:
- the value of the polynomial at the given point.
- See Also:
-
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) throws MathIllegalArgumentException, NullArgumentException 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:
MathIllegalArgumentException
- ifcoefficients
is empty.NullArgumentException
- ifcoefficients
isnull
.
-
value
public <T extends Derivative<T>> T value(T t) throws MathIllegalArgumentException, NullArgumentException Compute the value for the function.- Specified by:
value
in interfaceUnivariateDifferentiableFunction
- Type Parameters:
T
- the type of the field elements- Parameters:
t
- the point for which the function value should be computed- Returns:
- the value
- Throws:
MathIllegalArgumentException
- ifcoefficients
is empty.NullArgumentException
- ifcoefficients
isnull
.
-
value
public <T extends CalculusFieldElement<T>> T value(T t) throws MathIllegalArgumentException, NullArgumentException Compute the value of the function.- Specified by:
value
in interfaceFieldUnivariateFunction
- Type Parameters:
T
- the type of the field elements- Parameters:
t
- Point at which the function value should be computed.- Returns:
- the value of the function.
- Throws:
MathIllegalArgumentException
- ifcoefficients
is empty.NullArgumentException
- ifcoefficients
isnull
.- Since:
- 1.3
-
add
Add a polynomial to the instance.- Parameters:
p
- Polynomial to add.- Returns:
- a new polynomial which is the sum of the instance and
p
.
-
subtract
Subtract a polynomial from the instance.- Parameters:
p
- Polynomial to subtract.- Returns:
- a new polynomial which is the instance minus
p
.
-
negate
Negate the instance.- Returns:
- a new polynomial with all coefficients negated
-
multiply
Multiply the instance by a polynomial.- Parameters:
p
- Polynomial to multiply by.- Returns:
- a new polynomial equal to this times
p
-
differentiate
protected static double[] differentiate(double[] coefficients) throws MathIllegalArgumentException, NullArgumentException Returns the coefficients of the derivative of the polynomial with the given coefficients.- Parameters:
coefficients
- Coefficients of the polynomial to differentiate.- Returns:
- the coefficients of the derivative or
null
if coefficients has length 1. - Throws:
MathIllegalArgumentException
- ifcoefficients
is empty.NullArgumentException
- ifcoefficients
isnull
.
-
antiDerivative
Returns an anti-derivative of this polynomial, with 0 constant term.- Returns:
- a polynomial whose derivative has the same coefficients as this polynomial
-
integrate
public double integrate(double lower, double upper) Returns the definite integral of this polymomial over the given interval.[lower, upper] must describe a finite interval (neither can be infinite and lower must be less than or equal to upper).
- Parameters:
lower
- lower bound for the integrationupper
- upper bound for the integration- Returns:
- the integral of this polymomial over the given interval
- Throws:
MathIllegalArgumentException
- if the bounds do not describe a finite interval
-
polynomialDerivative
Returns the derivative as aPolynomialFunction
.- Returns:
- the derivative polynomial.
-
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). -
hashCode
public int hashCode() -
equals
-