Package jdistlib.math

Class Polynomial

java.lang.Object
jdistlib.math.Polynomial

public class Polynomial extends Object

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

      public static final Polynomial chooseHigherDegreePolynomial(Polynomial p1, Polynomial p2)
    • chooseLowerDegreePolynomial

      public static final Polynomial chooseLowerDegreePolynomial(Polynomial p1, Polynomial p2)
    • plus

      public Polynomial plus(Polynomial poly)
      Add another polynomial to this polynomial and store the result into a new instance of QPolynomial
      Parameters:
      poly -
    • plusEquals

      public void plusEquals(Polynomial poly)
      Add another polynomial into this polynomial and let the result overwrite this polynomial (this = this + poly)
      Parameters:
      poly -
    • minus

      public Polynomial minus(Polynomial poly)
      Subtract another polynomial from this polynomial and store the result into a new instance of QPolynomial
      Parameters:
      poly -
    • minusEquals

      public void minusEquals(Polynomial poly)
      Subtract another polynomial from this polynomial, in place (this = this - poly)
      Parameters:
      poly -
    • timesScalar

      public Polynomial timesScalar(double c)
      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

      public Polynomial times(Polynomial poly)
      Multiply this polynomial with another polynomial and store the result into a new instance of QPolynomial
      Parameters:
      poly -
    • timesEquals

      public void timesEquals(Polynomial poly)
      Multiply this polynomial with another polynomial, in place
      Parameters:
      poly -
    • differentiate

      public Polynomial differentiate()
      Compute the derivative of this polynomial and store the result into a new instance of QPolynomial
      Returns:
      the derivative
    • integrate

      public Polynomial integrate(double c)
      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

      public 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

      public Polynomial clone()
      Clone this polynomial
      Overrides:
      clone in class Object
    • toString

      public String toString(String varName)
      Construct a string representation of this polynomial
      Parameters:
      varName -
      Returns:
    • simplify

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

      public String toString()
      Get a string representation of this polynomial with x as the variable name
      Overrides:
      toString in class Object
    • 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 coefficients
      imCoef - Imaginary coefficients
      Returns:
      result[0] will be the real part, result[1] will be the imaginary part of the roots.
    • findScalarQuotient

      public double findScalarQuotient(Polynomial divisorPoly)
    • main

      public static void main(String[] args)