Class PolynomialFunctionLagrangeForm
- java.lang.Object
-
- org.apache.commons.math3.analysis.polynomials.PolynomialFunctionLagrangeForm
-
- All Implemented Interfaces:
UnivariateFunction
public class PolynomialFunctionLagrangeForm extends java.lang.Object implements UnivariateFunction
Implements the representation of a real polynomial function in Lagrange Form. For reference, see Introduction to Numerical Analysis, ISBN 038795452X, chapter 2.The approximated function should be smooth enough for Lagrange polynomial to work well. Otherwise, consider using splines instead.
- Since:
- 1.2
-
-
Field Summary
Fields Modifier and Type Field Description private double[]coefficientsThe coefficients of the polynomial, ordered by degree -- i.e.private booleancoefficientsComputedWhether the polynomial coefficients are available.private double[]xInterpolating points (abscissas).private double[]yFunction values at interpolating points.
-
Constructor Summary
Constructors Constructor Description PolynomialFunctionLagrangeForm(double[] x, double[] y)Construct a Lagrange polynomial with the given abscissas and function values.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidcomputeCoefficients()Calculate the coefficients of Lagrange polynomial from the interpolation data.intdegree()Returns the degree of the polynomial.static doubleevaluate(double[] x, double[] y, double z)Evaluate the Lagrange polynomial using Neville's Algorithm.private static doubleevaluateInternal(double[] x, double[] y, double z)Evaluate the Lagrange polynomial using Neville's Algorithm.double[]getCoefficients()Returns a copy of the coefficients array.double[]getInterpolatingPoints()Returns a copy of the interpolating points array.double[]getInterpolatingValues()Returns a copy of the interpolating values array.doublevalue(double z)Calculate the function value at the given point.static booleanverifyInterpolationArray(double[] x, double[] y, boolean abort)Check that the interpolation arrays are valid.
-
-
-
Field Detail
-
coefficients
private double[] coefficients
The coefficients of the polynomial, ordered by degree -- i.e. coefficients[0] is the constant term and coefficients[n] is the coefficient of x^n where n is the degree of the polynomial.
-
x
private final double[] x
Interpolating points (abscissas).
-
y
private final double[] y
Function values at interpolating points.
-
coefficientsComputed
private boolean coefficientsComputed
Whether the polynomial coefficients are available.
-
-
Constructor Detail
-
PolynomialFunctionLagrangeForm
public PolynomialFunctionLagrangeForm(double[] x, double[] y) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceExceptionConstruct a Lagrange polynomial with the given abscissas and function values. The order of interpolating points are not important.The constructor makes copy of the input arrays and assigns them.
- Parameters:
x- interpolating pointsy- function values at interpolating points- Throws:
DimensionMismatchException- if the array lengths are different.NumberIsTooSmallException- if the number of points is less than 2.NonMonotonicSequenceException- if two abscissae have the same value.
-
-
Method Detail
-
value
public double value(double z)
Calculate the function value at the given point.- Specified by:
valuein interfaceUnivariateFunction- Parameters:
z- Point at which the function value is to be computed.- Returns:
- the function value.
- Throws:
DimensionMismatchException- ifxandyhave different lengths.NonMonotonicSequenceException- ifxis not sorted in strictly increasing order.NumberIsTooSmallException- if the size ofxis less than 2.
-
degree
public int degree()
Returns the degree of the polynomial.- Returns:
- the degree of the polynomial
-
getInterpolatingPoints
public double[] getInterpolatingPoints()
Returns a copy of the interpolating points array.Changes made to the returned copy will not affect the polynomial.
- Returns:
- a fresh copy of the interpolating points array
-
getInterpolatingValues
public double[] getInterpolatingValues()
Returns a copy of the interpolating values array.Changes made to the returned copy will not affect the polynomial.
- Returns:
- a fresh copy of the interpolating values array
-
getCoefficients
public double[] getCoefficients()
Returns a copy of the coefficients array.Changes made to the returned copy will not affect the polynomial.
Note that coefficients computation can be ill-conditioned. Use with caution and only when it is necessary.
- Returns:
- a fresh copy of the coefficients array
-
evaluate
public static double evaluate(double[] x, double[] y, double z) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceExceptionEvaluate the Lagrange polynomial using Neville's Algorithm. It takes O(n^2) time.- Parameters:
x- Interpolating points array.y- Interpolating values array.z- Point at which the function value is to be computed.- Returns:
- the function value.
- Throws:
DimensionMismatchException- ifxandyhave different lengths.NonMonotonicSequenceException- ifxis not sorted in strictly increasing order.NumberIsTooSmallException- if the size ofxis less than 2.
-
evaluateInternal
private static double evaluateInternal(double[] x, double[] y, double z)Evaluate the Lagrange polynomial using Neville's Algorithm. It takes O(n^2) time.- Parameters:
x- Interpolating points array.y- Interpolating values array.z- Point at which the function value is to be computed.- Returns:
- the function value.
- Throws:
DimensionMismatchException- ifxandyhave different lengths.NonMonotonicSequenceException- ifxis not sorted in strictly increasing order.NumberIsTooSmallException- if the size ofxis less than 2.
-
computeCoefficients
protected void computeCoefficients()
Calculate the coefficients of Lagrange polynomial from the interpolation data. It takes O(n^2) time. Note that this computation can be ill-conditioned: Use with caution and only when it is necessary.
-
verifyInterpolationArray
public static boolean verifyInterpolationArray(double[] x, double[] y, boolean abort) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceExceptionCheck that the interpolation arrays are valid. The arrays features checked by this method are that both arrays have the same length and this length is at least 2.- Parameters:
x- Interpolating points array.y- Interpolating values array.abort- Whether to throw an exception ifxis not sorted.- Returns:
falseif thexis not sorted in increasing order,trueotherwise.- Throws:
DimensionMismatchException- if the array lengths are different.NumberIsTooSmallException- if the number of points is less than 2.NonMonotonicSequenceException- ifxis not sorted in strictly increasing order andabortistrue.- See Also:
evaluate(double[], double[], double),computeCoefficients()
-
-