Top Description Fields Constructors Methods
org.apache.commons.math3.analysis.polynomials

public Class PolynomialFunctionLagrangeForm

extends Object
implements UnivariateFunction
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.analysis.UnivariateFunction
Imports
org.apache.commons.math3.analysis.UnivariateFunction, org.apache.commons.math3.util.FastMath, .MathArrays, org.apache.commons.math3.exception.DimensionMismatchException, .NonMonotonicSequenceException, .NumberIsTooSmallException, org.apache.commons.math3.exception.util.LocalizedFormats

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

Modifier and TypeField and Description
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.

private boolean
coefficientsComputed

Whether the polynomial coefficients are available.

private final double[]
x

Interpolating points (abscissas).

private final double[]
y

Function values at interpolating points.

Constructor Summary

AccessConstructor and Description
public
PolynomialFunctionLagrangeForm(double[]
interpolating points
x
,
double[]
function values at interpolating points
y
)

Construct a Lagrange polynomial with the given abscissas and function values.

Method Summary

Modifier and TypeMethod and Description
protected void
computeCoefficients()

Calculate the coefficients of Lagrange polynomial from the interpolation data.

public int

Returns:

the degree of the polynomial
degree
()

Returns the degree of the polynomial.

public static double

Returns:

the function value.
evaluate
(double[]
Interpolating points array.
x
,
double[]
Interpolating values array.
y
,
double
Point at which the function value is to be computed.
z
)

Evaluate the Lagrange polynomial using Neville's Algorithm.

private static double

Returns:

the function value.
evaluateInternal
(double[]
Interpolating points array.
x
,
double[]
Interpolating values array.
y
,
double
Point at which the function value is to be computed.
z
)

Evaluate the Lagrange polynomial using Neville's Algorithm.

public double[]

Returns:

a fresh copy of the coefficients array
getCoefficients
()

Returns a copy of the coefficients array.

public double[]

Returns:

a fresh copy of the interpolating points array
getInterpolatingPoints
()

Returns a copy of the interpolating points array.

public double[]

Returns:

a fresh copy of the interpolating values array
getInterpolatingValues
()

Returns a copy of the interpolating values array.

public double

Returns:

the function value.
value
(double
Point at which the function value is to be computed.
z
)

Implements org.apache.commons.math3.analysis.UnivariateFunction.value.

Calculate the function value at the given point.

public static boolean

Returns:

false if the x is not sorted in increasing order, true otherwise.
verifyInterpolationArray
(double[]
Interpolating points array.
x
,
double[]
Interpolating values array.
y
,
boolean
Whether to throw an exception if x is not sorted.
abort
)

Check that the interpolation arrays are valid.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

coefficientsback to summary
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.

coefficientsComputedback to summary
private boolean coefficientsComputed

Whether the polynomial coefficients are available.

xback to summary
private final double[] x

Interpolating points (abscissas).

yback to summary
private final double[] y

Function values at interpolating points.

Constructor Detail

PolynomialFunctionLagrangeFormback to summary
public PolynomialFunctionLagrangeForm(double[] x, double[] y) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException

Construct 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:double[]

interpolating points

y:double[]

function values at interpolating points

Exceptions
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

computeCoefficientsback to summary
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.

degreeback to summary
public int degree()

Returns the degree of the polynomial.

Returns:int

the degree of the polynomial

evaluateback to summary
public static double evaluate(double[] x, double[] y, double z) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException

Evaluate the Lagrange polynomial using Neville's Algorithm. It takes O(n^2) time.

Parameters
x:double[]

Interpolating points array.

y:double[]

Interpolating values array.

z:double

Point at which the function value is to be computed.

Returns:double

the function value.

Exceptions
DimensionMismatchException:
if x and y have different lengths.
NumberIsTooSmallException:
if the size of x is less than 2.
NonMonotonicSequenceException:
if x is not sorted in strictly increasing order.
evaluateInternalback to summary
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:double[]

Interpolating points array.

y:double[]

Interpolating values array.

z:double

Point at which the function value is to be computed.

Returns:double

the function value.

Exceptions
DimensionMismatchException:
if x and y have different lengths.
NonMonotonicSequenceException:
if x is not sorted in strictly increasing order.
NumberIsTooSmallException:
if the size of x is less than 2.
getCoefficientsback to summary
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:double[]

a fresh copy of the coefficients array

getInterpolatingPointsback to summary
public double[] getInterpolatingPoints()

Returns a copy of the interpolating points array.

Changes made to the returned copy will not affect the polynomial.

Returns:double[]

a fresh copy of the interpolating points array

getInterpolatingValuesback to summary
public double[] getInterpolatingValues()

Returns a copy of the interpolating values array.

Changes made to the returned copy will not affect the polynomial.

Returns:double[]

a fresh copy of the interpolating values array

valueback to summary
public double value(double z)

Implements org.apache.commons.math3.analysis.UnivariateFunction.value.

Calculate the function value at the given point.

Parameters
z:double

Point at which the function value is to be computed.

Returns:double

the function value.

Exceptions
DimensionMismatchException:
if x and y have different lengths.
NonMonotonicSequenceException:
if x is not sorted in strictly increasing order.
NumberIsTooSmallException:
if the size of x is less than 2.
verifyInterpolationArrayback to summary
public static boolean verifyInterpolationArray(double[] x, double[] y, boolean abort) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException

Check 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:double[]

Interpolating points array.

y:double[]

Interpolating values array.

abort:boolean

Whether to throw an exception if x is not sorted.

Returns:boolean

false if the x is not sorted in increasing order, true otherwise.

Exceptions
DimensionMismatchException:
if the array lengths are different.
NumberIsTooSmallException:
if the number of points is less than 2.
NonMonotonicSequenceException:
if x is not sorted in strictly increasing order and abort is true.
See Also
evaluate(double[], double[], double), computeCoefficients()