SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.differentiation.multivariate
Class FiniteDifference

java.lang.Object
  extended by com.numericalmethod.suanshu.analysis.differentiation.multivariate.FiniteDifference
All Implemented Interfaces:
Function, RealScalarFunction

public class FiniteDifference
extends java.lang.Object
implements RealScalarFunction

This class computes the numerical partial derivative of a function.

A partial derivative of a multivariate function is the derivative with respect to one of the variables with the others held constant.

The computation is done by applying (first order) finite difference recursively on the function. Specifically,

2       ∂    ∂
 ------ = ---( ---- )
 ∂x2∂x1   ∂x2   ∂x1
 

Each of the two univariate derivative is computed using the central difference method.

See Also:

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.numericalmethod.suanshu.analysis.function.Function
Function.EvaluationException
 
Field Summary
 RealScalarFunction f
          the function to take the derivative of
 
Constructor Summary
FiniteDifference(RealScalarFunction f, int[] varidx)
          Construct a finite difference instance for a multi-variable function.
 
Method Summary
 int dimension4Domain()
          Get the number of variables of the function.
 int dimension4Range()
          Get the dimension of the range space the function.
 double evaluate(double... x)
          Numerically evaluate the derivative of f at point x.
 double evaluate(double[] x, double h)
          Numerically evaluate the derivative of f at point x with step size h.
 double evaluateByRecursion(int n, double h, double... z)
          Evaluate the n-th derivative using Recursion.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

f

public final RealScalarFunction f
the function to take the derivative of

Constructor Detail

FiniteDifference

public FiniteDifference(RealScalarFunction f,
                        int[] varidx)
Construct a finite difference instance for a multi-variable function.

For example, varidx = new int[]{1, 2} means

           ∂f
 fx1,x2 = -------
          ∂x2∂x1

    ∂    ∂
 = ---( ---- )
   ∂x2   ∂x1
 

Parameters:
f - the real multivariate function to take derivative of
varidx - specify the variable indices, numbering from 1 up to the domain dimension of f
Method Detail

dimension4Domain

public int dimension4Domain()
Description copied from interface: Function
Get the number of variables of the function.

For example, for a univariate function, the domain dimension is 1. For a bivariate function, the domain dimension is 2.

Specified by:
dimension4Domain in interface Function
Returns:
the number of variables

dimension4Range

public int dimension4Range()
Description copied from interface: Function
Get the dimension of the range space the function.

For example, for a Rn->Rm function, the dimension of the range is m.

Specified by:
dimension4Range in interface Function
Returns:
the dimension of the range

evaluate

public double evaluate(double... x)
Numerically evaluate the derivative of f at point x.

Make sure that h and x+h are representable in floating point precision so that the difference between x+h and x is exactly h.

Specified by:
evaluate in interface RealScalarFunction
Parameters:
x - the point to evaluate the derivative at
Returns:
the numerical derivative of f at point x
See Also:
Wikipedia: Practical considerations

evaluate

public double evaluate(double[] x,
                       double h)
Numerically evaluate the derivative of f at point x with step size h.

Automatically determining the step size h could be challenging, esp. for when |x| is near 0. An analysis that involves f' and f'' may be needed to determine the step size. User may want to experiment with different hs by calling this function.

Parameters:
x - the point to evaluate the derivative of f at
h - the step size
Returns:
the numerical derivative of f at point x with step size h

evaluateByRecursion

public double evaluateByRecursion(int n,
                                  double h,
                                  double... z)
Evaluate the n-th derivative using Recursion.
nf
 --------
 ∂x1...∂xn
 

Parameters:
n - the order, i.e., n-th derivative,
h - the step size
z - the point to evaluate the derivative at
Returns:
the n-th derivative at z

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.