Package org.opensourcephysics.numerics
Class Polynomial
java.lang.Object
org.opensourcephysics.numerics.Polynomial
- All Implemented Interfaces:
Function
- Direct Known Subclasses:
PolynomialLeastSquareFit
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 -
Constructor Summary
ConstructorsConstructorDescriptionPolynomial
(double[] coef) Constructs a polynomial with the given coefficients.Polynomial
(String[] coef) Constructs a polynomial with the given coefficients. -
Method Summary
Modifier and TypeMethodDescriptionadd
(double r) add
(Polynomial p) Adds the given polynomial to this polynomial.double
coefficient
(int n) Gets the coefficient value at the desired positiondeflate
(double r) Deflates the polynomial by removing the root.int
degree()
Gets the degree of this polynomial function.Gets the derivative of this polynomial.divide
(double r) Divides this polynomial by a constant.divide
(Polynomial p) Divides this polynomial by another polynomial.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[]
Gets a clone of the polynomial coefficients c: c[0] + c[1] * x + c[2] * x^2 + ....integral()
Integrates this polynomial.integral
(double value) Integrates this polynomial having the specified value for x = 0.multiply
(double r) Multiplies this polynomial by a constant.Multiplies this polynomial by another polynomial.double[][]
roots()
Gets the complex roots of this polynomial.double[]
Gets the real roots of this polynomial.subtract
(double r) Subtracts a constant from this polynomial.Subtracts another polynomial from this polynomial.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
-
Field Details
-
coefficients
protected double[] coefficientsPolynomial coefficients.
-
-
Constructor Details
-
Polynomial
public Polynomial(double[] coef) Constructs a polynomial with the given coefficients.- Parameters:
coef
- polynomial coefficients.
-
Polynomial
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
- Parameters:
r
- double number added to the polynomial.- Returns:
- Polynomial
-
add
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
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
Gets the derivative of this polynomial.- Returns:
- Polynomial the derivative.
-
divide
Divides this polynomial by a constant.- Parameters:
r
- double- Returns:
- Polynomial
-
divide
Divides this polynomial by another polynomial. The remainder is dropped.- Parameters:
p
- Polynomial- Returns:
- Polynomial
-
divideWithRemainder
Divides this polynomial by another polynomial.- Parameters:
p
- polynomial- Returns:
- polynomial array containing the answer and remainder
-
integral
Integrates this polynomial. The integral has the value 0 at x = 0.- Returns:
- Polynomial the integral
-
integral
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
Multiplies this polynomial by a constant.- Parameters:
r
- double- Returns:
- Polynomial
-
multiply
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
Subtracts a constant from this polynomial.- Parameters:
r
- the constant- Returns:
- Polynomial
-
subtract
Subtracts another polynomial from this polynomial.- Parameters:
p
- Polynomial- Returns:
- Polynomial
-
toString
Converts this polynomial to a String. -
evaluate
public double evaluate(double x) Evaluates the polynomial for the specified variable 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
-