Class Polynomial

java.lang.Object
org.jhotdraw8.geom.Polynomial
All Implemented Interfaces:
ToDoubleFunction<Double>

public class Polynomial extends Object implements ToDoubleFunction<Double>
A polynomial.

References:

polynomial.js
polynomial.js, Copyright (c) 2002 Kevin Lindsey, BSD 3-clause license. kevlindev.com
  • Constructor Details

    • Polynomial

      public Polynomial(double... coefs)
      Creates a new polynomial.

      The coefficients are in order by highest degree monomial first. For example, the following example initializes a Polynomial object for: 3x^4 + 2x^2 + 5.

       var poly = new Polynomial(3, 0, 2, 0, 5);
       
      All coefficients from highest degree to degree 0 must be provided. A zero is used for monomials that are not present in the polynomial.

      NOTE: The polynomial coefficients are stored in an array in the reverse order to how they were specified. This has the benefit that the coefficient's position in the array corresponds to the degree of the monomial to which it belongs. *

      Parameters:
      coefs - the coefficients of the polynomial
    • Polynomial

      public Polynomial(boolean highestToLowestDegree, double... coefs)
      Alternative constructor.
      Parameters:
      highestToLowestDegree - true if sorted from highest to lowest degree, false if sorted from lowest do highest degree.
      coefs - will be referenced
  • Method Details

    • add

      public Polynomial add(Polynomial that)
      Adds the coefficients of that polynomial to the coefficients of this polynomial and returns the resulting polynomial. Does not change this polynomial.
      Parameters:
      that - another polynomial
      Returns:
      a new polynomial containing the sum of the coefficients
    • subtract

      public Polynomial subtract(Polynomial that)
      Subtracts the coefficients of that polynomial from the coefficients of this polynomial and returns the resulting polynomial. Does not change this polynomial.
      Parameters:
      that - another polynomial
      Returns:
      a new polynomial containing the difference of the coefficients
    • applyAsDouble

      public double applyAsDouble(Double x)
      Specified by:
      applyAsDouble in interface ToDoubleFunction<Double>
    • bisection

      public static @Nullable Double bisection(ToDoubleFunction<Double> func, double min, double max)
      Searches for a root in the given interval using the bisection method.
      Parameters:
      func - the function
      min - the lower bound of the interval
      max - the upper bound of the interval
      Returns:
      the root, null if no root could be found
    • divideScalar

      public Polynomial divideScalar(double scalar)
      Divides the coefficients of this polynomial by the provided scalar. Does not change this polynomial.
      Parameters:
      scalar - a scalar
      Returns:
      a new polynomial
    • eval

      public double eval(double x)
      Evaluates the polynomial at the specified x value.
      Parameters:
      x - is a number that is "plugged into" the polynomial to evaluate it.
      Returns:
      the value of the polynomial at x
    • getDegree

      public int getDegree()
      Returns the degree of this polynomial.
      Returns:
      the degree = number of coefficients.
    • getDerivative

      public Polynomial getDerivative()
      Returns the derivative of this polynomial.
      Returns:
      returns the derivative of the current polynomial.
    • getQuadraticRoots

      public static double[] getQuadraticRoots(double a, double b, double c)
      Returns the roots of a quadratic polynomial (degree equals two).
           a*t^2 + b*t + c = 0
      
           d = b^2 - 4 * c
           t1 = ( -b + sqrt(d) ) / 2
           t2 = ( -b - sqrt(d) ) / 2
       
      Returns:
      the roots
    • getRoots

      public double[] getRoots()
      Attempts to find the roots of the current polynomial. This method will attempt to decrease the degree of the polynomial using the simplifiedDegree(). method. Once the degree is determined, getRoots() dispatches the appropriate root-finding method for the degree of the polynomial.

      NOTE This method does not find roots for polynomials, which can not be simplfied to 4th degree or less. Use getRootsInInterval(double, double) for polynomials above 4th degree.

      Returns:
      the roots of the polynomial
    • getRootsInInterval

      public DoubleArrayList getRootsInInterval(double min, double max)
      Gets roots in the given interval. Uses the bisection method for root finding. Can work with a polynomial of any degree.
      Parameters:
      min - the lower bound of the interval (inclusive)
      max - the upper bound of the interval (inclusive)
      Returns:
      a list of roots
    • getRootsInInterval

      public static DoubleArrayList getRootsInInterval(ToDoubleFunction<Double> func, DoubleArrayList droots, double min, double max)
      Gets roots in the given interval. Uses the bisection method for root finding. Can work with a polynomial of any degree.
      Parameters:
      func - the function
      droots - the roots of the derivative of the function in the interval [min,max].
      min - the lower bound of the interval (inclusive)
      max - the upper bound of the interval (inclusive)
      Returns:
      a list of roots. The list if empty, if no roots have been found
    • multiply

      public Polynomial multiply(Polynomial that)
      Multiplies the coefficients of this polynomial with the coefficients of that polynomial and returns the resulting polynomial. Does not change this polynomial.
      Parameters:
      that - another polynomial
      Returns:
      a new polynomial containing the product of the coefficients
    • simplify

      public Polynomial simplify()
      Returns a simplified polynomial, by removing coefficients of the highest degrees if they have a very small absolute value.
      Returns:
      a new polynomial
    • toString

      public String toString()
      toString.
      Overrides:
      toString in class Object
      Returns:
      string representation
    • trim

      public static double[] trim(int length, double[] a)
      Trims an array to the specified length.

      Returns the same array if it already has the specified length.

      Parameters:
      length - the specified length
      a - the array
      Returns:
      array of the specified length
    • arcLength

      public double arcLength(double min, double max)
      Estimates the arc length of the polynomial in the interval [min,max].

      Computes ∫_min‾max sqrt(1 + (f'(x))^2 )

      Parameters:
      min - the lower bound of the interval
      max - the upper bound of the interval
      Returns:
      the estimated arc length