Class Polynomial

All Implemented Interfaces:
DoubleUnaryOperator, RealValuedDomainOps, RealValuedFunctOps, RealValuedFunctVAOps, VADomainOps

public class Polynomial extends RealValuedFunction
Class representing polynomials using a monomial basis.

Methods include ones for computing a polynomial's value, obtaining or modifying a polynomial's coefficients, computing integrals and derivatives of a polynomial, and adding, multiplying or dividing polynomials.

The class Polynomials has a series of static methods that can be used to add, multiply, and divide polynomials. It also has methods that perform these operations on arrays representing a polynomial's coefficients. Whether decides to use the Polynomials class or the corresponding methods in this class is dependent more on coding style than anything else (for addition, multiplication and division, the methods in this class use the methods in the Polynomials class).

  • Constructor Details

    • Polynomial

      public Polynomial()
      Constructor.
    • Polynomial

      public Polynomial(Polynomial p)
      Constructor based on an existing polynomial.
      Parameters:
      p - the existing polynomial
    • Polynomial

      public Polynomial(int degree)
      Constructor specifying a degree. The degree is used to set an internal table size.
      Parameters:
      degree - the degree of a polynomial that this instance should support.
    • Polynomial

      public Polynomial(double[] coefficients, int degree)
      Constructor specifying coefficients and a degree. Coefficients are order so that, for a polynomial p(x) = a0 + a1x + ... + anxn, the coefficient for the xi term is stored in an array at index i, thus making a0 the first index in p's coefficient array. The degree is used to set an internal table size.
      Parameters:
      coefficients - the coefficients for this polynomial.
      degree - the degree of a polynomial that this instance should support.
    • Polynomial

      public Polynomial(double... coefficients)
      Constructor specifying coefficients. Coefficients are order so that, for a polynomial p(x) = a0 + a1x + ... + anxn, the coefficient for the xi term is stored in an array at index i, thus making a0 the first index in p's coefficient array.
      Parameters:
      coefficients - the coefficients for this polynomial.
  • Method Details

    • setCoefficients

      public void setCoefficients(double[] coefficients)
      Set the coefficients for this polynomial. Coefficients are order so that, for a polynomial p(x) = a0 + a1x + ... + anxn, the coefficient for the xi term is stored in an array at index i, thus making a0 the first index in p's coefficient array.
      Parameters:
      coefficients - the coefficients
    • softReset

      public void softReset(int degree)
      Reset this polynomial so that the polynomial is empty but so that the existing coefficients are not modified. If necessary, enough space will be allocated to store the coefficients of a polynomial whose degree is equal to or less than the argument, but the internal array will not be changed to a new array unless a new array has to be allocated, and if the array is extended, the old values will be preserved. A polynomial is considered to be empty if getDegree() returns -1.

      This method is appropriate when one will set all of the coefficients explicitly or when the old values need to be temporarily preserved (this can happen in cases where the same polynomial is used as two arguments, one of which will be modified).

      Parameters:
      degree - the degree
    • reset

      public void reset(int degree)
      Reset this polynomial so that it is empty but with space allocated for polynomials whose degree is no larger than the one specified. The internal array's elements for indices in the range [0,degree] will be set to zero.
      Parameters:
      degree - the degree
    • truncate

      public void truncate()
      Decrease the degree of a polynomial so that the highest orde term does not have a coefficient equal to 0. This method should be used if coefficients are explicitly modified.
    • setTo

      public void setTo(Polynomial p)
      Set the degree and coefficients of this polynomial to match those of a specified polynomial.
      Parameters:
      p - the specified polynomial
    • setCoefficients

      public void setCoefficients(double[] coefficients, int degree) throws IllegalArgumentException
      Set the coefficients of this polynomial, specifying a degree. Coefficients are order so that, for a polynomial p(x) = a0 + a1x + ... + anxn, the coefficient for the xi term is stored in an array at index i, thus making a0 the first index in p's coefficient array.
      Parameters:
      coefficients - the coefficients (only those indices in the range [0, degree] will be used)
      degree - the degree of this polynomial
      Throws:
      IllegalArgumentException
    • getDegree

      public int getDegree()
      Get the degree of this polynomial.
      Returns:
      the degree; -1 if the polynomial is empty (i.e., no coefficients have been provided)
    • getCoefficientsArray

      public double[] getCoefficientsArray()
      Get the array used to store coefficients for this polynomial. The size of the array is guaranteed to be at least 1 larger then the degree of this polynomial. Modifying this array may change the polynomial. Coefficients are ordered so that, for a polynomial p(x) = a0 + a1x + ... + anxn, the coefficient for the xi term is stored in an array at index i, thus making a0 the first index in p's coefficient array.
      Returns:
      the array
    • getCoefficients

      public double[] getCoefficients()
      Get the coefficients for this polynomial Coefficients are order so that, for a polynomial p(x) = a0 + a1x + ... + anxn, the coefficient for the xi term is stored in an array at index i, thus making a0 the first index in p's coefficient array. The size of the array will be the next integer larger than the this polynomial's degree. Modifying the array returned by this method will not change the polynomial.
      Returns:
      the coefficients
    • valueAt

      public double valueAt(double x)
      Description copied from class: RealValuedFunction
      Call the function.
      Specified by:
      valueAt in interface RealValuedFunctOps
      Overrides:
      valueAt in class RealValuedFunction
      Parameters:
      x - the function's argument
      Returns:
      the value of the function for the given argument
    • shift

      public Polynomial shift(double a)
      Shift a polynomial P(x). This polynomial is modified.
      Parameters:
      a - the value by which to shift the argument of P(x)
      Returns:
      the polynomial P(x+a)
    • reducedFormShift

      public double reducedFormShift()
      Get the amount by which to shift a polynomial to put it into its reduced form.

      A typical useage is

      
        Polynomial p = ...;
        double s = p.getCoefficientsArray[p.getDegree()];
        double shift = p.reducedFormShift();
        p.shift(shift).multiplyBy(1/s);
       
      To undo the effects of the shift, use p.shift(-shift).
      Returns:
      the shift
    • integral

      public Polynomial integral()
      Integrate a polynomial $P$ by computing $\int_{t=0}^xP(t)dt$. If this polynomial's degree as returned by getDegree() is -1, which indicates that the polynomial is not defined, a new polynomial with a degree of -1 will be returned.
      Returns:
      the integral of this polynomial
    • integralAt

      public double integralAt(double x)
      Evaluated the integral of a polynomial. For a polynomial P(x) The value is $\int_{t=0}^xP(t)dt$.
      Parameters:
      x - the polynomial's argument
      Returns:
      the integral of the polynomial, evaluated at x
    • deriv

      public Polynomial deriv()
      Description copied from class: RealValuedFunction
      Get a function that computes the first derivative of this real-valued function. If this real-valued function does not support a first derivative, the returned function's valueAt method will throw a UnsupportedOperationException.
      Overrides:
      deriv in class RealValuedFunction
      Returns:
      the first derivative
    • derivAt

      public double derivAt(double x)
      Description copied from class: RealValuedFunction
      Evaluate the function's first derivative.
      Overrides:
      derivAt in class RealValuedFunction
      Parameters:
      x - the function's argument
      Returns:
      the value of the function for the given argument
    • secondDeriv

      public Polynomial secondDeriv()
      Description copied from class: RealValuedFunction
      Get a function that computes the second derivative of this real-valued function. If this real-valued function does not support a second derivative, the returned function's valueAt method will throw a UnsupportedOperationException.
      Overrides:
      secondDeriv in class RealValuedFunction
      Returns:
      the second derivative
    • secondDerivAt

      public double secondDerivAt(double x)
      Description copied from class: RealValuedFunction
      Evaluate the function's second derivative.
      Overrides:
      secondDerivAt in class RealValuedFunction
      Parameters:
      x - the function's argument
      Returns:
      the value of the function for the given argument
    • incrBy

      public void incrBy(Polynomial p)
      Modify this polynomial by adding another polynomial to this polynomial.
      Parameters:
      p - the polynomial by which this polynomial is incremented
    • multiplyBy

      public void multiplyBy(double s)
      Modify this polynomial by multiplying it by a scalar.
      Parameters:
      s - the scalar
    • multiplyBy

      public void multiplyBy(Polynomial p)
      Modify this polynomial by multiplying it by another polynomial.
      Parameters:
      p - the polynomial by which this polynomial is multiplied
    • add

      public Polynomial add(Polynomial p)
      Compute the sum of this polynomial and another polynomial. This polynomial is not modified.
      Parameters:
      p - the polynomial to add to this polynomial
      Returns:
      a new polynomial that is equal to the sum of this polynomial and the polynomial p
    • multiply

      public Polynomial multiply(Polynomial p)
      Compute the product of this polynomial and another polynomial. This polynomial is not modified.
      Parameters:
      p - the polynomial by which this polynomial is multiplied
      Returns:
      a new polynomial that is equal to the product of this polynomial and the polynomial p
    • multiply

      public Polynomial multiply(double s)
      Compute the product of this polynomial and a scalar. This polynomial is not modified.
      Parameters:
      s - the scalar by which this polynomial is multiplied
      Returns:
      a new polynomial that is equal to the product of this polynomial and the scalar s
    • divide

      public Polynomial divide(Polynomial p, boolean isQuotient)
      Divide this polynomial by another polynomial and return either the quotient or the remainder. This polynomial is not modified.

      Please see Polynomials.divide(double[],double[],double[],int,double[],int) for documentation regarding numerical accuracy.

      Parameters:
      p - the polynomial by which this polynomial is divided
      isQuotient - true if the quotient polynomial is returned; false if the remainder polynomial is returned
      Returns:
      a new polynomial whose value is either the quotient or the remainder when this polynomial is divided by the polynomial p