Class Polynomial

java.lang.Object
org.opensourcephysics.numerics.Polynomial
All Implemented Interfaces:
Function
Direct Known Subclasses:
PolynomialLeastSquareFit

public class Polynomial extends Object implements Function
Polynomial implements a mathematical polynomial: c[0] + c[1] * x + c[2] * x^2 + .... This class is based on code published in Object-Oriented Implementation of Numerical Methods by Didier H. Besset. The code has been adapted to the OSP framework by Wolfgang Christian.
  • Field Details

    • coefficients

      protected double[] coefficients
      Polynomial coefficients.
  • Constructor Details

    • Polynomial

      public Polynomial(double[] coef)
      Constructs a polynomial with the given coefficients.
      Parameters:
      coef - polynomial coefficients.
    • Polynomial

      public Polynomial(String[] coef)
      Constructs a polynomial with the given coefficients.
      Parameters:
      coef - polynomial coefficients.
  • Method Details

    • getCoefficients

      public double[] getCoefficients()
      Gets a clone of the polynomial coefficients c: c[0] + c[1] * x + c[2] * x^2 + ....
      Returns:
      double[]
    • evalPolynomial

      public static double evalPolynomial(double x, double[] coeff)
      Evaluates a polynomial using the given coefficients.
      Parameters:
      x -
      coeff - the polynomial coefficients.
    • add

      public Polynomial add(double r)
      Parameters:
      r - double number added to the polynomial.
      Returns:
      Polynomial
    • add

      public Polynomial add(Polynomial p)
      Adds the given polynomial to this polynomial.
      Parameters:
      p - Polynomial
      Returns:
      Polynomial
    • coefficient

      public double coefficient(int n)
      Gets the coefficient value at the desired position
      Parameters:
      n - int the position of the coefficient to be returned
      Returns:
      double the coefficient value
    • deflate

      public Polynomial deflate(double r)
      Deflates the polynomial by removing the root.
      Parameters:
      r - double a root of the polynomial (no check made).
      Returns:
      Polynomial the receiver divided by polynomial (x - r).
    • degree

      public int degree()
      Gets the degree of this polynomial function.
      Returns:
      int degree of this polynomial function
    • derivative

      public Polynomial derivative()
      Gets the derivative of this polynomial.
      Returns:
      Polynomial the derivative.
    • divide

      public Polynomial divide(double r)
      Divides this polynomial by a constant.
      Parameters:
      r - double
      Returns:
      Polynomial
    • divide

      public Polynomial divide(Polynomial p)
      Divides this polynomial by another polynomial. The remainder is dropped.
      Parameters:
      p - Polynomial
      Returns:
      Polynomial
    • divideWithRemainder

      public Polynomial[] divideWithRemainder(Polynomial p)
      Divides this polynomial by another polynomial.
      Parameters:
      p - polynomial
      Returns:
      polynomial array containing the answer and remainder
    • integral

      public Polynomial integral()
      Integrates this polynomial. The integral has the value 0 at x = 0.
      Returns:
      Polynomial the integral
    • integral

      public Polynomial integral(double value)
      Integrates this polynomial having the specified value for x = 0.
      Parameters:
      value - double value of the integral at x=0
      Returns:
      Polynomial the integral.
    • multiply

      public Polynomial multiply(double r)
      Multiplies this polynomial by a constant.
      Parameters:
      r - double
      Returns:
      Polynomial
    • multiply

      public Polynomial multiply(Polynomial p)
      Multiplies this polynomial by another polynomial.
      Parameters:
      p - Polynomial
      Returns:
      Polynomial
    • roots

      public double[][] roots()
      Gets the complex roots of this polynomial.
      Returns:
      double[]
    • rootsReal

      public double[] rootsReal()
      Gets the real roots of this polynomial.
      Returns:
      double[]
    • subtract

      public Polynomial subtract(double r)
      Subtracts a constant from this polynomial.
      Parameters:
      r - the constant
      Returns:
      Polynomial
    • subtract

      public Polynomial subtract(Polynomial p)
      Subtracts another polynomial from this polynomial.
      Parameters:
      p - Polynomial
      Returns:
      Polynomial
    • toString

      public String toString()
      Converts this polynomial to a String.
      Overrides:
      toString in class Object
    • evaluate

      public double evaluate(double x)
      Evaluates the polynomial for the specified variable value.
      Specified by:
      evaluate in interface Function
      Parameters:
      x - double value at which the polynomial is evaluated
      Returns:
      double polynomial value.
    • valueAndDerivative

      public double[] valueAndDerivative(double x)
      Returns the value and the derivative of this polynomial for the specified variable value in an array of two elements
      Parameters:
      x - double value at which the polynomial is evaluated
      Returns:
      double[0] the value of the polynomial