Package jdistlib.math
Class Polynomial
java.lang.Object
jdistlib.math.Polynomial
A Java object that represents polynomials as arrays of numerical coefficients. The single variable is simple (not a function). The lowest (0) -degree term is at index 0 and the higher-degree terms follow to the right. No power is left out even if its coefficient is 0.
Also, this class does not represent polynomials with negative powers (e.g. x^-1) and all coefficients have to be real numbers.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final Polynomialstatic final Polynomialclone()Clone this polynomialvoidcompact()Compacts the representation of this polynomial.Compute the derivative of this polynomial and store the result into a new instance of QPolynomialdoubleevaluate(double x0) Evaluate this polynomial at x = x0doubleevaluate(double[] powersOfX) Evaluate this polynomial based on a precomputed vector of powers of the variable.double[][]Returns the root.static final double[][]findRoots(double[] realCoef, double[] imCoef) Jenkins-Traub algorithm for finding root of polynomials.doublefindScalarQuotient(Polynomial divisorPoly) double[]Get the coefficientsintGet the degree of the polynomialCompute the integral of this polynomial.integrate(double c) Compute the integral of this polynomial.doubleintegrate(double a, double b) Compute a definite integral bounded by (a, b)static voidminus(Polynomial poly) Subtract another polynomial from this polynomial and store the result into a new instance of QPolynomialvoidminusEquals(Polynomial poly) Subtract another polynomial from this polynomial, in place (this = this - poly)plus(Polynomial poly) Add another polynomial to this polynomial and store the result into a new instance of QPolynomialvoidplusEquals(Polynomial poly) Add another polynomial into this polynomial and let the result overwrite this polynomial (this = this + poly)voidsetCoefficient(int power, double value) Set coefficient of this polynomialsimplify()Can we simplify this polynomial? If the lowest coefficients are zero, the polynomial looks like x^n * simpler_polynomial.times(Polynomial poly) Multiply this polynomial with another polynomial and store the result into a new instance of QPolynomialvoidtimesEquals(Polynomial poly) Multiply this polynomial with another polynomial, in placetimesScalar(double c) Multiply this polynomial with a constant and store the result into a new instance of QPolynomialvoidtimesScalarEquals(double c) Multiply this polynomial with a constant, in placetoString()Get a string representation of this polynomial with x as the variable nameConstruct a string representation of this polynomial
-
Field Details
-
mCoefficients
protected double[] mCoefficients
-
-
Constructor Details
-
Polynomial
public Polynomial(int degree) -
Polynomial
public Polynomial(double... coeffs)
-
-
Method Details
-
getCoefficients
public double[] getCoefficients()Get the coefficients- Returns:
-
getDegree
public int getDegree()Get the degree of the polynomial- Returns:
-
setCoefficient
public void setCoefficient(int power, double value) Set coefficient of this polynomial- Parameters:
power-value-
-
chooseHigherDegreePolynomial
-
chooseLowerDegreePolynomial
-
plus
Add another polynomial to this polynomial and store the result into a new instance of QPolynomial- Parameters:
poly-
-
plusEquals
Add another polynomial into this polynomial and let the result overwrite this polynomial (this = this + poly)- Parameters:
poly-
-
minus
Subtract another polynomial from this polynomial and store the result into a new instance of QPolynomial- Parameters:
poly-
-
minusEquals
Subtract another polynomial from this polynomial, in place (this = this - poly)- Parameters:
poly-
-
timesScalar
Multiply this polynomial with a constant and store the result into a new instance of QPolynomial- Parameters:
c-
-
timesScalarEquals
public void timesScalarEquals(double c) Multiply this polynomial with a constant, in place- Parameters:
c-
-
times
Multiply this polynomial with another polynomial and store the result into a new instance of QPolynomial- Parameters:
poly-
-
timesEquals
Multiply this polynomial with another polynomial, in place- Parameters:
poly-
-
differentiate
Compute the derivative of this polynomial and store the result into a new instance of QPolynomial- Returns:
- the derivative
-
integrate
Compute the integral of this polynomial. The last constant is c- Parameters:
c- The constant at the last term (i.e. x^0)- Returns:
- the integrated polynomial
-
integrate
Compute the integral of this polynomial. The last constant defaults to ZERO- Returns:
- the integrated polynomial
-
integrate
public double integrate(double a, double b) Compute a definite integral bounded by (a, b)- Parameters:
a-b-- Returns:
-
evaluate
public double evaluate(double x0) Evaluate this polynomial at x = x0- Parameters:
x0-- Returns:
- The result
-
evaluate
public double evaluate(double[] powersOfX) Evaluate this polynomial based on a precomputed vector of powers of the variable. To be used in PolynomialMatrix. CN 10.31.05- Parameters:
powersOfX- [].- Returns:
- a scalar, the vector product
-
compact
public void compact()Compacts the representation of this polynomial. That is, we don't want the highest coefficient to be zero For example: If the polynomial object contains these coefficients: [2, 3, 1, 0], which means: 0 x^3 + 1 x^2 + 3 x^1 + 2 x^0 -- compact() method will delete the coefficient at x^3, like this:
1 x^2 + 3 x^1 + 2 x^0
Thus, the QPolynomial will have this array instead: [2, 3, 1] -
clone
Clone this polynomial -
toString
Construct a string representation of this polynomial- Parameters:
varName-- Returns:
-
simplify
Can we simplify this polynomial? If the lowest coefficients are zero, the polynomial looks like x^n * simpler_polynomial. So we want to factor that out first.- Returns:
- coefficients of the simpler polynomials
-
findRoots
public double[][] findRoots()Returns the root. result[0] is the real part. result[1] is the imaginary part- Returns:
-
toString
Get a string representation of this polynomial with x as the variable name -
findRoots
public static final double[][] findRoots(double[] realCoef, double[] imCoef) Jenkins-Traub algorithm for finding root of polynomials. The lowest (0) -degree term is at index 0 and the higher-degree terms follow to the right. No power is left out.- Parameters:
realCoef- Real coefficientsimCoef- Imaginary coefficients- Returns:
- result[0] will be the real part, result[1] will be the imaginary part of the roots.
-
findScalarQuotient
-
main
-