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

public Class FiniteDifferencesDifferentiator

extends Object
implements UnivariateFunctionDifferentiator, UnivariateVectorFunctionDifferentiator, UnivariateMatrixFunctionDifferentiator, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.apache.commons.math3.analysis.differentiation.UnivariateMatrixFunctionDifferentiator, org.apache.commons.math3.analysis.differentiation.UnivariateVectorFunctionDifferentiator, org.apache.commons.math3.analysis.differentiation.UnivariateFunctionDifferentiator
Imports
java.io.Serializable, org.apache.commons.math3.analysis.UnivariateFunction, .UnivariateMatrixFunction, .UnivariateVectorFunction, org.apache.commons.math3.exception.MathIllegalArgumentException, .NotPositiveException, .NumberIsTooLargeException, .NumberIsTooSmallException, org.apache.commons.math3.util.FastMath

Univariate functions differentiator using finite differences.

This class creates some wrapper objects around regular univariate functions (or univariate vector functions or univariate matrix functions). These wrapper objects compute derivatives in addition to function values.

The wrapper objects work by calling the underlying function on a sampling grid around the current point and performing polynomial interpolation. A finite differences scheme with n points is theoretically able to compute derivatives up to order n-1, but it is generally better to have a slight margin. The step size must also be small enough in order for the polynomial approximation to be good in the current point neighborhood, but it should not be too small because numerical instability appears quickly (there are several differences of close points). Choosing the number of points and the step size is highly problem dependent.

As an example of good and bad settings, lets consider the quintic polynomial function f(x) = (x-1)*(x-0.5)*x*(x+0.5)*(x+1). Since it is a polynomial, finite differences with at least 6 points should theoretically recover the exact same polynomial and hence compute accurate derivatives for any order. However, due to numerical errors, we get the following results for a 7 points finite differences for abscissae in the [-10, 10] range:

This example shows that the small step size is really bad, even simply for second order derivative!

Since
3.1

Field Summary

Modifier and TypeField and Description
private final double
halfSampleSpan

Half sample span.

private final int
nbPoints

Number of points to use.

private static final long
serialVersionUID

Serializable UID.

private final double
stepSize

Step size.

private final double
tMax

Upper bound for independent variable.

private final double
tMin

Lower bound for independent variable.

Constructor Summary

AccessConstructor and Description
public
FiniteDifferencesDifferentiator(final int
number of points to use
nbPoints
,
final double
step size (gap between each point)
stepSize
)

Build a differentiator with number of points and step size when independent variable is unbounded.

public
FiniteDifferencesDifferentiator(final int
number of points to use
nbPoints
,
final double
step size (gap between each point)
stepSize
,
final double
lower bound for independent variable (may be Double.NEGATIVE_INFINITY if there are no lower bounds)
tLower
,
final double
upper bound for independent variable (may be Double.POSITIVE_INFINITY if there are no upper bounds)
tUpper
)

Build a differentiator with number of points and step size when independent variable is bounded.

Method Summary

Modifier and TypeMethod and Description
public UnivariateDifferentiableFunction
differentiate(final UnivariateFunction
function to differentiate
function
)

Implements org.apache.commons.math3.analysis.differentiation.UnivariateFunctionDifferentiator.differentiate.

Create an implementation of a differential from a regular function.

public UnivariateDifferentiableVectorFunction
public UnivariateDifferentiableMatrixFunction
private DerivativeStructure

Returns:

value and derivatives at t
evaluate
(final DerivativeStructure
evaluation abscissa value and derivatives
t
,
final double
first sample point abscissa
t0
,
final double[]
function values sample y[i] = f(t[i]) = f(t0 + i * stepSize)
y
)

Evaluate derivatives from a sample.

public int

Returns:

number of points to use
getNbPoints
()

Get the number of points to use.

public double

Returns:

step size
getStepSize
()

Get the step size.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

halfSampleSpanback to summary
private final double halfSampleSpan

Half sample span.

nbPointsback to summary
private final int nbPoints

Number of points to use.

serialVersionUIDback to summary
private static final long serialVersionUID

Serializable UID.

stepSizeback to summary
private final double stepSize

Step size.

tMaxback to summary
private final double tMax

Upper bound for independent variable.

tMinback to summary
private final double tMin

Lower bound for independent variable.

Constructor Detail

FiniteDifferencesDifferentiatorback to summary
public FiniteDifferencesDifferentiator(final int nbPoints, final double stepSize) throws NotPositiveException, NumberIsTooSmallException

Build a differentiator with number of points and step size when independent variable is unbounded.

Beware that wrong settings for the finite differences differentiator can lead to highly unstable and inaccurate results, especially for high derivation orders. Using very small step sizes is often a bad idea.

Parameters
nbPoints:int

number of points to use

stepSize:double

step size (gap between each point)

Exceptions
NotPositiveException:
if stepsize <= 0 (note that NotPositiveException extends NumberIsTooSmallException)
NumberIsTooSmallException:
nbPoint <= 1
FiniteDifferencesDifferentiatorback to summary
public FiniteDifferencesDifferentiator(final int nbPoints, final double stepSize, final double tLower, final double tUpper) throws NotPositiveException, NumberIsTooSmallException, NumberIsTooLargeException

Build a differentiator with number of points and step size when independent variable is bounded.

When the independent variable is bounded (tLower < t < tUpper), the sampling points used for differentiation will be adapted to ensure the constraint holds even near the boundaries. This means the sample will not be centered anymore in these cases. At an extreme case, computing derivatives exactly at the lower bound will lead the sample to be entirely on the right side of the derivation point.

Note that the boundaries are considered to be excluded for function evaluation.

Beware that wrong settings for the finite differences differentiator can lead to highly unstable and inaccurate results, especially for high derivation orders. Using very small step sizes is often a bad idea.

Parameters
nbPoints:int

number of points to use

stepSize:double

step size (gap between each point)

tLower:double

lower bound for independent variable (may be Double.NEGATIVE_INFINITY if there are no lower bounds)

tUpper:double

upper bound for independent variable (may be Double.POSITIVE_INFINITY if there are no upper bounds)

Exceptions
NotPositiveException:
if stepsize <= 0 (note that NotPositiveException extends NumberIsTooSmallException)
NumberIsTooSmallException:
nbPoint <= 1
NumberIsTooLargeException:
stepSize * (nbPoints - 1) >= tUpper - tLower

Method Detail

differentiateback to summary
public UnivariateDifferentiableFunction differentiate(final UnivariateFunction function)

Implements org.apache.commons.math3.analysis.differentiation.UnivariateFunctionDifferentiator.differentiate.

Doc from org.apache.commons.math3.analysis.differentiation.UnivariateFunctionDifferentiator.differentiate.

Create an implementation of a differential from a regular function.

The returned object cannot compute derivatives to arbitrary orders. The value function will throw a NumberIsTooLargeException if the requested derivation order is larger or equal to the number of points.

Parameters
function:UnivariateFunction

function to differentiate

Returns:UnivariateDifferentiableFunction

differential function

differentiateback to summary
public UnivariateDifferentiableVectorFunction differentiate(final UnivariateVectorFunction function)

Implements org.apache.commons.math3.analysis.differentiation.UnivariateVectorFunctionDifferentiator.differentiate.

Doc from org.apache.commons.math3.analysis.differentiation.UnivariateVectorFunctionDifferentiator.differentiate.

Create an implementation of a differential from a regular vector function.

The returned object cannot compute derivatives to arbitrary orders. The value function will throw a NumberIsTooLargeException if the requested derivation order is larger or equal to the number of points.

Parameters
function:UnivariateVectorFunction

function to differentiate

Returns:UnivariateDifferentiableVectorFunction

differential function

differentiateback to summary
public UnivariateDifferentiableMatrixFunction differentiate(final UnivariateMatrixFunction function)

Implements org.apache.commons.math3.analysis.differentiation.UnivariateMatrixFunctionDifferentiator.differentiate.

Doc from org.apache.commons.math3.analysis.differentiation.UnivariateMatrixFunctionDifferentiator.differentiate.

Create an implementation of a differential from a regular matrix function.

The returned object cannot compute derivatives to arbitrary orders. The value function will throw a NumberIsTooLargeException if the requested derivation order is larger or equal to the number of points.

Parameters
function:UnivariateMatrixFunction

function to differentiate

Returns:UnivariateDifferentiableMatrixFunction

differential function

evaluateback to summary
private DerivativeStructure evaluate(final DerivativeStructure t, final double t0, final double[] y) throws NumberIsTooLargeException

Evaluate derivatives from a sample.

Evaluation is done using divided differences.

Parameters
t:DerivativeStructure

evaluation abscissa value and derivatives

t0:double

first sample point abscissa

y:double[]

function values sample y[i] = f(t[i]) = f(t0 + i * stepSize)

Returns:DerivativeStructure

value and derivatives at t

Exceptions
NumberIsTooLargeException:
if the requested derivation order is larger or equal to the number of points
getNbPointsback to summary
public int getNbPoints()

Get the number of points to use.

Returns:int

number of points to use

getStepSizeback to summary
public double getStepSize()

Get the step size.

Returns:double

step size