Class Polynomial
java.lang.Object
com.irurueta.numerical.polynomials.Polynomial
- All Implemented Interfaces:
Serializable
Contains a polynomial and common operations done with polynomials.
This implementation only supports polynomials with real parameters.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final double
Constant defining machine precisionprivate static final int
Minimum derivative / integration order.static final int
Minimum allowed length in polynomial parameters.private double[]
Array containing parameters defining a polynomial. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor.Polynomial
(double... polyParams) Constructor.Polynomial
(int numberOfParameters) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(Polynomial other) Adds another polynomial to this polynomial.void
add
(Polynomial other, Polynomial result) Adds this polynomial to another one and stores the result into provided instance.addAndReturnNew
(Polynomial other) Adds this polynomial to another one and returns a new polynomial as a result.void
Replaces this instance by its derivative.void
derivative
(Polynomial result) Computes derivative of polynomial.Computes derivative of polynomial.double
evaluate
(double x) Evaluates polynomial at provided value.double
evaluateDerivative
(double x) Evaluates derivative of polynomial at provided value.double
evaluateNthDerivative
(double x, int order) Evaluates nth-derivative of polynomial at provided value.double
evaluateSecondDerivative
(double x) Evaluates second derivative of polynomial at provided value.int
Gets degree of polynomial.double[]
Gets location of minima or maxima (i.e.double[]
getExtrema
(double threshold) Gets location of minima or maxima (i.e.double[]
Gets location of maxima in this polynomial.double[]
getMaxima
(double threshold) Gets location of maxima in this polynomial.double[]
Gets location of minima in this polynomial.double[]
getMinima
(double threshold) Gets location of minima in this polynomial.double[]
Gets array defining parameters of polynomial.com.irurueta.algebra.Complex[]
getRoots()
Gets roots of polynomial.double
integrateInterval
(double startX, double endX) Integrate polynomial within provided interval.void
Updates this instance to contain its integration using a zero constant term.void
integration
(double constant) Updates this instance to contain its integration.void
integration
(Polynomial result) Computes polynomial containing the integration of current one and assuming a zero constant term.void
integration
(Polynomial result, double constant) Computes polynomial containing the integration of current one.Computes polynomial containing the integration of current one and assuming a zero constant term.integrationAndReturnNew
(double constant) Computes polynomial containing the integration of current one.void
multiply
(Polynomial other) Multiplies this polynomial with another one.void
multiply
(Polynomial other, Polynomial result) Multiplies two polynomials.multiplyAndReturnNew
(Polynomial other) Multiplies two polynomials and returns a new instance containing result.void
multiplyByScalar
(double scalar) Multiplies all parameters of this polynomial by provided scalar.void
multiplyByScalar
(double scalar, Polynomial result) Multiplies all parameters of this polynomial by a scalar and stores the result into provided polynomial instance.multiplyByScalarAndReturnNew
(double scalar) Multiplies all parameters of this polynomial by a scalar and returns a new polynomial containing the result.void
Normalizes this polynomial so that the array of parameters has unitary norm.void
normalize
(Polynomial result) Normalizes parameters of this polynomial so that the array of parameters has unitary norm and stores result into provided instance.Normalizes parameters of this polynomial so that the array of parameters has unitary norm and returns result as a new polynomial instance.void
Normalizes parameters of this polynomial so that the highest degree term becomes 1.0.void
normalizeHighestDegreeTerm
(Polynomial result) Normalizes parameters of this polynomial so that the highest degree term becomes 1.0 and stores result into provided instance.Normalizes parameters of this polynomial so that the highest degree term becomes 1.0 and returns the result as a new instance.void
nthDerivative
(int order) Replaces this instance by its nth-order derivative.void
nthDerivative
(int order, Polynomial result) Computes nth-order derivative of polynomial.nthDerivativeAndReturnNew
(int order) Computes nth-order derivative of polynomial.void
nthIntegration
(int order) Computes polynomial containing the nth-order integration of current one.void
nthIntegration
(int order, double[] constants) Computes polynomial containing the nth-order integration of current one.void
nthIntegration
(int order, Polynomial result) Computes polynomial containing the nth-order integration of current one.void
nthIntegration
(int order, Polynomial result, double[] constants) Computes polynomial containing the nth-order integration of current one.nthIntegrationAndReturnNew
(int order) Computes polynomial containing the nth-order integration of current one.nthIntegrationAndReturnNew
(int order, double[] constants) Computes polynomial containing the nth-order integration of current one.double
nthOrderIntegrateInterval
(double startX, double endX, int order) Computes nth-integration over provided interval.double
nthOrderIntegrateInterval
(double startX, double endX, int order, double[] constants) Computes nth-integration over provided interval.void
Replaces this instance by its second derivative.void
secondDerivative
(Polynomial result) Computes second derivative of polynomial.Computes second derivative of polynomial.final void
setPolyParams
(double... polyParams) Sets array defining parameters of polynomial.void
subtract
(Polynomial other) Subtracts another polynomial form this one.void
subtract
(Polynomial other, Polynomial result) Subtract other polynomial from this one and stores the result into provided instance.subtractAndReturnNew
(Polynomial other) Subtract other polynomial from this one and returns a new polynomial as a result.void
trim()
Trims this polynomial to remove all terms above degree that can be neglected.void
trim
(Polynomial result) Trims polynomial to remove all terms above degree that can be neglected.Trims this polynomial to remove all terms above degree that can be neglected and returns the result as a new polynomial.
-
Field Details
-
MIN_ORDER
private static final int MIN_ORDERMinimum derivative / integration order.- See Also:
-
MIN_VALID_POLY_PARAMS_LENGTH
public static final int MIN_VALID_POLY_PARAMS_LENGTHMinimum allowed length in polynomial parameters.- See Also:
-
EPS
public static final double EPSConstant defining machine precision- See Also:
-
polyParams
private double[] polyParamsArray containing parameters defining a polynomial. For a polynomial having the expression p(x) = a + b*x + c*x^2 + ... provided array must be [a, b, c, ...]
-
-
Constructor Details
-
Polynomial
public Polynomial()Constructor. Creates a polynomial initialized to zero. -
Polynomial
public Polynomial(int numberOfParameters) Constructor.- Parameters:
numberOfParameters
- number of parameters of polynomial to create.- Throws:
IllegalArgumentException
- if number of parameters is less than 1.
-
Polynomial
public Polynomial(double... polyParams) Constructor. For a polynomial having the expression p(x) = a + b*x + c*x^2 + ... provided array must be [a, b, c, ...]- Parameters:
polyParams
- parameters defining a polynomial.- Throws:
IllegalArgumentException
- if provided array does not have at least length 2.
-
-
Method Details
-
getPolyParams
public double[] getPolyParams()Gets array defining parameters of polynomial. A polynomial having the expression p(x) = a + b*x + c*x^2 + ... has an array of the form [a, b, c, ...].- Returns:
- parameters defining a polynomial.
-
setPolyParams
public final void setPolyParams(double... polyParams) Sets array defining parameters of polynomial. A polynomial having the expression p(x) = a + b*x + c*x^2 + ... has an array of the form [a, b, c, ...].- Parameters:
polyParams
- array defining parameters of polynomial. Must have at least length 2.- Throws:
IllegalArgumentException
- if provided array does not have at least length 2.
-
getDegree
public int getDegree()Gets degree of polynomial.- Returns:
- degree of polynomial.
-
add
Adds this polynomial to another one and stores the result into provided instance.- Parameters:
other
- other polynomial to be added.result
- instance where result will be stored.
-
add
Adds another polynomial to this polynomial.- Parameters:
other
- other polynomial to be added.
-
addAndReturnNew
Adds this polynomial to another one and returns a new polynomial as a result.- Parameters:
other
- other polynomial to be added.- Returns:
- a new polynomial containing the sum.
-
subtract
Subtract other polynomial from this one and stores the result into provided instance.- Parameters:
other
- other polynomial to be subtracted from this one.result
- instance where result will be stored.
-
subtract
Subtracts another polynomial form this one.- Parameters:
other
- other polynomial to be subtracted from this one.
-
subtractAndReturnNew
Subtract other polynomial from this one and returns a new polynomial as a result.- Parameters:
other
- other polynomial to be subtracted from this one.- Returns:
- a new polynomial containing result of subtraction.
-
multiply
Multiplies two polynomials.- Parameters:
other
- other polynomial to multiply with.result
- instance where resulting polynomial will be stored.
-
multiply
Multiplies this polynomial with another one.- Parameters:
other
- other polynomial to multiply with.
-
multiplyAndReturnNew
Multiplies two polynomials and returns a new instance containing result.- Parameters:
other
- other polynomial to multiply with.- Returns:
- a new polynomial containing result of multiplication.
-
multiplyByScalar
Multiplies all parameters of this polynomial by a scalar and stores the result into provided polynomial instance.- Parameters:
scalar
- scalar to multiply parameters with.result
- instance where result will be stored.
-
multiplyByScalar
public void multiplyByScalar(double scalar) Multiplies all parameters of this polynomial by provided scalar.- Parameters:
scalar
- scalar to multiply parameters with.
-
multiplyByScalarAndReturnNew
Multiplies all parameters of this polynomial by a scalar and returns a new polynomial containing the result.- Parameters:
scalar
- scalar to multiply parameters with.- Returns:
- a new polynomial containing the result of the operation.
-
getRoots
Gets roots of polynomial.- Returns:
- estimated roots of this polynomial
- Throws:
NumericalException
- if roots estimation fails.
-
evaluate
public double evaluate(double x) Evaluates polynomial at provided value.- Parameters:
x
- value to evaluate polynomial at.- Returns:
- result of polynomial evaluation.
-
derivative
Computes derivative of polynomial.- Parameters:
result
- instance where derivative will be stored.
-
derivative
public void derivative()Replaces this instance by its derivative. -
derivativeAndReturnNew
Computes derivative of polynomial.- Returns:
- a new instance containing derivative.
-
evaluateDerivative
public double evaluateDerivative(double x) Evaluates derivative of polynomial at provided value.- Parameters:
x
- value to evaluate derivative of polynomial at.- Returns:
- result of evaluation of derivative.
-
secondDerivative
Computes second derivative of polynomial.- Parameters:
result
- instance where second derivative will be stored.
-
secondDerivative
public void secondDerivative()Replaces this instance by its second derivative. -
secondDerivativeAndReturnNew
Computes second derivative of polynomial.- Returns:
- a new instance containing second derivative.
-
evaluateSecondDerivative
public double evaluateSecondDerivative(double x) Evaluates second derivative of polynomial at provided value.- Parameters:
x
- value to evaluate second derivative of polynomial at.- Returns:
- result of evaluation of second derivative.
-
nthDerivative
Computes nth-order derivative of polynomial.- Parameters:
order
- order of derivative to compute. Must be at least 1.result
- instance where nth-order derivative will be stored.- Throws:
IllegalArgumentException
- if provided order is less than 1.
-
nthDerivative
public void nthDerivative(int order) Replaces this instance by its nth-order derivative.- Parameters:
order
- order of derivative to compute. Must be at least 1.- Throws:
IllegalArgumentException
- if provided order is less than 1.
-
nthDerivativeAndReturnNew
Computes nth-order derivative of polynomial.- Parameters:
order
- order of derivative to compute. Must be at least 1.- Returns:
- a new instance containing nth-order derivative.
- Throws:
IllegalArgumentException
- if provided order is less than 1.
-
evaluateNthDerivative
public double evaluateNthDerivative(double x, int order) Evaluates nth-derivative of polynomial at provided value.- Parameters:
x
- value to evaluate nth-derivative of polynomial at.order
- order of derivative to evaluate. Must be at least 1.- Returns:
- result of evaluation of nth-derivative.
- Throws:
IllegalArgumentException
- if provided order is less than 1.
-
integration
Computes polynomial containing the integration of current one. Because infinite polynomials exist with different constant values, constant term can be provided as well.- Parameters:
result
- instance where resulting polynomial will be stored.constant
- constant term.
-
integration
Computes polynomial containing the integration of current one and assuming a zero constant term.- Parameters:
result
- instance where resulting polynomial will be stored.
-
integration
public void integration(double constant) Updates this instance to contain its integration.- Parameters:
constant
- constant term.
-
integration
public void integration()Updates this instance to contain its integration using a zero constant term. -
integrationAndReturnNew
Computes polynomial containing the integration of current one. Because infinite polynomials exist with different constant values, constant term can be provided as well.- Parameters:
constant
- constant term.- Returns:
- a new instance containing integration polynomial.
-
integrationAndReturnNew
Computes polynomial containing the integration of current one and assuming a zero constant term.- Returns:
- a new instance containing integration polynomial.
-
integrateInterval
public double integrateInterval(double startX, double endX) Integrate polynomial within provided interval.- Parameters:
startX
- start of integration interval.endX
- end of integration interval.- Returns:
- result of integration.
-
nthIntegration
Computes polynomial containing the nth-order integration of current one. Because infinite polynomials exist with different constant values, constant terms for each integration order can be provided as well.- Parameters:
order
- order of integration to compute. Must be at least 1.result
- instance where resulting polynomial will be stored.constants
- constant terms for each integration order. Must have a length equal to order if provided.- Throws:
IllegalArgumentException
- if provided order is less than 1 or if constants does not have length equal to order.
-
nthIntegration
Computes polynomial containing the nth-order integration of current one.- Parameters:
order
- order of integration to compute. Must be at least 1.result
- instance where resulting polynomial will be stored.- Throws:
IllegalArgumentException
- if provided order is less than 1.
-
nthIntegration
public void nthIntegration(int order, double[] constants) Computes polynomial containing the nth-order integration of current one. Because infinite polynomials exist with different constant values, constant terms for each integration order can be provided as well.- Parameters:
order
- order of integration to compute. Must be at least 1.constants
- constant terms for each integration order. Must have a length equal to order if provided.- Throws:
IllegalArgumentException
- if provided order is less than 1 or if constants does not have length equal to order.
-
nthIntegration
public void nthIntegration(int order) Computes polynomial containing the nth-order integration of current one.- Parameters:
order
- order of integration to compute. Must be at least 1.
-
nthIntegrationAndReturnNew
Computes polynomial containing the nth-order integration of current one. Because infinite polynomials exist with different constant values, constant terms for each integration order can be provided as well.- Parameters:
order
- order of integration to compute. Must be at least 1.constants
- constant terms for each integration order. Must have a length equal to order if provided.- Returns:
- a new polynomial containing the nth-order integration.
- Throws:
IllegalArgumentException
- if provided order is less than 1 or if constants does not have length equal to order.
-
nthIntegrationAndReturnNew
Computes polynomial containing the nth-order integration of current one.- Parameters:
order
- order of integration to compute. Must be at least 1.- Returns:
- a new polynomial containing the nth-order integration.
- Throws:
IllegalArgumentException
- if provided order is less than 1 or if constants does not have length equal to order.
-
nthOrderIntegrateInterval
public double nthOrderIntegrateInterval(double startX, double endX, int order, double[] constants) Computes nth-integration over provided interval.- Parameters:
startX
- start of integration interval.endX
- end of integration interval.order
- order of integration. Must be at least 1.constants
- constant terms for each integration order. Must have a length equal to order if provided.- Returns:
- result of integration.
- Throws:
IllegalArgumentException
- if provided order is less than 1 or if constants does not have length equal to order.
-
nthOrderIntegrateInterval
public double nthOrderIntegrateInterval(double startX, double endX, int order) Computes nth-integration over provided interval.- Parameters:
startX
- start of integration interval.endX
- end of integration interval.order
- order of integration. Must be at least 1.- Returns:
- result of integration.
- Throws:
IllegalArgumentException
- if provided order is less than 1.
-
trim
Trims polynomial to remove all terms above degree that can be neglected.- Parameters:
result
- instance where result will be stored.
-
trim
public void trim()Trims this polynomial to remove all terms above degree that can be neglected. -
trimAndReturnNew
Trims this polynomial to remove all terms above degree that can be neglected and returns the result as a new polynomial.- Returns:
- a new trimmed polynomial.
-
normalize
Normalizes parameters of this polynomial so that the array of parameters has unitary norm and stores result into provided instance. Normalization keeps location of real roots, but other roots or properties of polynomials might change.- Parameters:
result
- instance where normalized polynomial will be stored.
-
normalize
public void normalize()Normalizes this polynomial so that the array of parameters has unitary norm. Normalization keeps location of real roots, but other roots or properties of polynomials might change. -
normalizeAndReturnNew
Normalizes parameters of this polynomial so that the array of parameters has unitary norm and returns result as a new polynomial instance. Normalization keeps location of real roots, but other roots or properties of polynomials might change.- Returns:
- a new normalized polynomial instance.
-
normalizeHighestDegreeTerm
Normalizes parameters of this polynomial so that the highest degree term becomes 1.0 and stores result into provided instance.- Parameters:
result
- instance where result of normalization will be stored.
-
normalizeHighestDegreeTerm
public void normalizeHighestDegreeTerm()Normalizes parameters of this polynomial so that the highest degree term becomes 1.0. -
normalizeHighestDegreeTermAndReturnNew
Normalizes parameters of this polynomial so that the highest degree term becomes 1.0 and returns the result as a new instance.- Returns:
- a new normalized polynomial.
-
getMaxima
Gets location of maxima in this polynomial.- Returns:
- location of maxima or null if polynomial has no maxima.
- Throws:
NumericalException
- if maxima cannot be determined due to numerical instabilities.
-
getMaxima
Gets location of maxima in this polynomial.- Parameters:
threshold
- threshold to allow possible small deviations in first derivative respect to pure real roots. This should be a very small positive value.- Returns:
- location of maxima or null if polynomial has no maxima.
- Throws:
NumericalException
- if maxima cannot be determined due to numerical instabilities.IllegalArgumentException
- if provided threshold is negative.
-
getMinima
Gets location of minima in this polynomial.- Returns:
- location of minima or null if polynomial has no minima.
- Throws:
NumericalException
- if minima cannot be determined due to numerical instabilities.
-
getMinima
Gets location of minima in this polynomial.- Parameters:
threshold
- threshold to allow possible small deviations in first derivative respect to pure real roots. This should be a very small positive value.- Returns:
- location of minima or null if polynomial has no minima.
- Throws:
NumericalException
- if minima cannot be determined due to numerical instabilities.IllegalArgumentException
- if provided threshold is negative.
-
getExtrema
Gets location of minima or maxima (i.e. extrema) in this polynomial.- Returns:
- location of minima or maxima, or null if polynomial has no minima or maxima.
- Throws:
NumericalException
- if minima or maxima cannot be determined due to numerical instabilities.
-
getExtrema
Gets location of minima or maxima (i.e. extrema) in this polynomial.- Parameters:
threshold
- threshold to allow possible small deviations in first derivative respect to pure real roots. This should be a very small positive value.- Returns:
- location of minima or maxima, or null if polynomial has no minima or maxima.
- Throws:
NumericalException
- if minima or maxima cannot be determined due to numerical instabilities.IllegalArgumentException
- if provided threshold is negative.
-