java.lang.Object
org.jhotdraw8.geom.Polynomial
- All Implemented Interfaces:
ToDoubleFunction<Double>
A polynomial.
References:
- polynomial.js
- polynomial.js, Copyright (c) 2002 Kevin Lindsey, BSD 3-clause license. kevlindev.com
-
Constructor Summary
ConstructorsConstructorDescriptionPolynomial(boolean highestToLowestDegree, double... coefs) Alternative constructor.Polynomial(double... coefs) Creates a new polynomial. -
Method Summary
Modifier and TypeMethodDescriptionadd(Polynomial that) Adds the coefficients of that polynomial to the coefficients of this polynomial and returns the resulting polynomial.doubledoublearcLength(double min, double max) Estimates the arc length of the polynomial in the interval [min,max].static @Nullable Doublebisection(ToDoubleFunction<Double> func, double min, double max) Searches for a root in the given interval using the bisection method.divideScalar(double scalar) Divides the coefficients of this polynomial by the provided scalar.doubleeval(double x) Evaluates the polynomial at the specified x value.intReturns the degree of this polynomial.Returns the derivative of this polynomial.static double[]getQuadraticRoots(double a, double b, double c) Returns the roots of a quadratic polynomial (degree equals two).double[]getRoots()Attempts to find the roots of the current polynomial.getRootsInInterval(double min, double max) Gets roots in the given interval.static DoubleArrayListgetRootsInInterval(ToDoubleFunction<Double> func, DoubleArrayList droots, double min, double max) Gets roots in the given interval.multiply(Polynomial that) Multiplies the coefficients of this polynomial with the coefficients of that polynomial and returns the resulting polynomial.simplify()Returns a simplified polynomial, by removing coefficients of the highest degrees if they have a very small absolute value.subtract(Polynomial that) Subtracts the coefficients of that polynomial from the coefficients of this polynomial and returns the resulting polynomial.toString()toString.static double[]trim(int length, double[] a) Trims an array to the specified length.
-
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
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
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
- Specified by:
applyAsDoublein interfaceToDoubleFunction<Double>
-
bisection
Searches for a root in the given interval using the bisection method.- Parameters:
func- the functionmin- the lower bound of the intervalmax- the upper bound of the interval- Returns:
- the root, null if no root could be found
-
divideScalar
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
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 thesimplifiedDegree(). 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
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 functiondroots- 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
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
Returns a simplified polynomial, by removing coefficients of the highest degrees if they have a very small absolute value.- Returns:
- a new polynomial
-
toString
toString. -
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 lengtha- 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 intervalmax- the upper bound of the interval- Returns:
- the estimated arc length
-