org.opensourcephysics.numerics

## Class Polynomial

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

```public class Polynomial
extends java.lang.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 Summary

Fields
Modifier and Type Field and Description
`protected double[]` `coefficients`
Polynomial coefficients.
• ### Constructor Summary

Constructors
Constructor and Description
`Polynomial(double[] coef)`
Constructs a polynomial with the given coefficients.
`Polynomial(java.lang.String[] coef)`
Constructs a polynomial with the given coefficients.
• ### Method Summary

All Methods
Modifier and Type Method and Description
`Polynomial` `add(double r)`
`Polynomial` `add(Polynomial p)`
Adds the given polynomial to this polynomial.
`double` `coefficient(int n)`
Gets the coefficient value at the desired position
`Polynomial` `deflate(double r)`
Deflates the polynomial by removing the root.
`int` `degree()`
Gets the degree of this polynomial function.
`Polynomial` `derivative()`
Gets the derivative of this polynomial.
`Polynomial` `divide(double r)`
Divides this polynomial by a constant.
`Polynomial` `divide(Polynomial p)`
Divides this polynomial by another polynomial.
`Polynomial[]` `divideWithRemainder(Polynomial p)`
Divides this polynomial by another polynomial.
`static double` ```evalPolynomial(double x, double[] coeff)```
Evaluates a polynomial using the given coefficients.
`double` `evaluate(double x)`
Evaluates the polynomial for the specified variable value.
`double[]` `getCoefficients()`
Gets a clone of the polynomial coefficients c: c[0] + c[1] * x + c[2] * x^2 + ....
`Polynomial` `integral()`
Integrates this polynomial.
`Polynomial` `integral(double value)`
Integrates this polynomial having the specified value for x = 0.
`Polynomial` `multiply(double r)`
Multiplies this polynomial by a constant.
`Polynomial` `multiply(Polynomial p)`
Multiplies this polynomial by another polynomial.
`double[][]` `roots()`
Gets the complex roots of this polynomial.
`double[]` `rootsReal()`
Gets the real roots of this polynomial.
`Polynomial` `subtract(double r)`
Subtracts a constant from this polynomial.
`Polynomial` `subtract(Polynomial p)`
Subtracts another polynomial from this polynomial.
`java.lang.String` `toString()`
Converts this polynomial to a String.
`double[]` `valueAndDerivative(double x)`
Returns the value and the derivative of this polynomial for the specified variable value in an array of two elements
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`
• ### Field Detail

• #### coefficients

`protected double[] coefficients`
Polynomial coefficients.
• ### Constructor Detail

• #### Polynomial

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

`public Polynomial(java.lang.String[] coef)`
Constructs a polynomial with the given coefficients.
Parameters:
`coef` - polynomial coefficients.
• ### Method Detail

• #### 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.

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

`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 java.lang.String toString()`
Converts this polynomial to a String.
Overrides:
`toString` in class `java.lang.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