Class Polynomial

java.lang.Object
com.irurueta.numerical.polynomials.Polynomial
All Implemented Interfaces:
Serializable

public class Polynomial extends Object implements Serializable
Contains a polynomial and common operations done with polynomials. This implementation only supports polynomials with real parameters.
See Also:
  • Field Details

    • MIN_ORDER

      private static final int MIN_ORDER
      Minimum derivative / integration order.
      See Also:
    • MIN_VALID_POLY_PARAMS_LENGTH

      public static final int MIN_VALID_POLY_PARAMS_LENGTH
      Minimum allowed length in polynomial parameters.
      See Also:
    • EPS

      public static final double EPS
      Constant defining machine precision
      See Also:
    • polyParams

      private double[] polyParams
      Array 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

      public void add(Polynomial other, Polynomial result)
      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

      public void add(Polynomial other)
      Adds another polynomial to this polynomial.
      Parameters:
      other - other polynomial to be added.
    • addAndReturnNew

      public Polynomial addAndReturnNew(Polynomial other)
      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

      public void subtract(Polynomial other, Polynomial result)
      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

      public void subtract(Polynomial other)
      Subtracts another polynomial form this one.
      Parameters:
      other - other polynomial to be subtracted from this one.
    • subtractAndReturnNew

      public Polynomial subtractAndReturnNew(Polynomial other)
      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

      public void multiply(Polynomial other, Polynomial result)
      Multiplies two polynomials.
      Parameters:
      other - other polynomial to multiply with.
      result - instance where resulting polynomial will be stored.
    • multiply

      public void multiply(Polynomial other)
      Multiplies this polynomial with another one.
      Parameters:
      other - other polynomial to multiply with.
    • multiplyAndReturnNew

      public Polynomial multiplyAndReturnNew(Polynomial other)
      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

      public void multiplyByScalar(double scalar, Polynomial result)
      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

      public Polynomial multiplyByScalarAndReturnNew(double scalar)
      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

      public com.irurueta.algebra.Complex[] getRoots() throws NumericalException
      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

      public void derivative(Polynomial result)
      Computes derivative of polynomial.
      Parameters:
      result - instance where derivative will be stored.
    • derivative

      public void derivative()
      Replaces this instance by its derivative.
    • derivativeAndReturnNew

      public Polynomial 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

      public void secondDerivative(Polynomial result)
      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

      public Polynomial 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

      public void nthDerivative(int order, Polynomial result)
      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

      public Polynomial nthDerivativeAndReturnNew(int order)
      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

      public void integration(Polynomial result, double constant)
      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

      public void integration(Polynomial result)
      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

      public Polynomial integrationAndReturnNew(double constant)
      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

      public 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

      public void nthIntegration(int order, Polynomial result, 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.
      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

      public void nthIntegration(int order, Polynomial result)
      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

      public Polynomial nthIntegrationAndReturnNew(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.
      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

      public Polynomial nthIntegrationAndReturnNew(int order)
      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

      public void trim(Polynomial result)
      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

      public Polynomial 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

      public void normalize(Polynomial result)
      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

      public Polynomial 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

      public void normalizeHighestDegreeTerm(Polynomial result)
      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

      public Polynomial 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

      public double[] getMaxima() throws NumericalException
      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

      public double[] getMaxima(double threshold) throws NumericalException
      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

      public double[] getMinima() throws NumericalException
      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

      public double[] getMinima(double threshold) throws NumericalException
      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

      public double[] getExtrema() throws NumericalException
      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

      public double[] getExtrema(double threshold) throws NumericalException
      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.