umontreal.iro.lecuyer.functions
Class MathFunctionUtil
- java.lang.Object
-
- umontreal.iro.lecuyer.functions.MathFunctionUtil
-
public class MathFunctionUtil extends java.lang.ObjectProvides utility methods for computing derivatives and integrals of functions.
-
-
Field Summary
Fields Modifier and Type Field and Description static doubleHStep length in x to compute derivatives.static intNUMINTERVALSDefault number of intervals for Simpson's integral.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static doublederivative(MathFunction func, double x)Returns the first derivative of the function func evaluated at x.static doublederivative(MathFunction func, double x, int n)Returns the nth derivative of function func evaluated at x.static doublefiniteCenteredDifferenceDerivative(MathFunction func, double x, double h)Returns (f (x + h) - f (x - h))/(2h), an estimate of the first derivative of f (x) using centered differences.static doublefiniteCenteredDifferenceDerivative(MathFunction func, double x, int n, double h)Computes and returns an estimate of the nth derivative of the function f (x) using finite centered differences.static doublefiniteDifferenceDerivative(MathFunction func, double x, int n, double h)Computes and returns an estimate of the nth derivative of the function f (x).static doublegaussLobatto(MathFunction func, double a, double b, double tol)Computes and returns a numerical approximation of the integral of f (x) over [a, b], using Gauss-Lobatto adaptive quadrature with 5 nodes, with tolerance tol.static doublegaussLobatto(MathFunction func, double a, double b, double tol, double[][] T)Similar to methodgaussLobatto(MathFunction, double, double, double), but also returns in T[0] the subintervals of integration, and in T[1], the partial values of the integral over the corresponding subintervals.static doubleintegral(MathFunction func, double a, double b)Returns the integral of the function func over [a, b].static double[][]removeNaNs(double[] x, double[] y)Removes any point (NaN, y) or (x, NaN) from x and y, and returns a 2D array containing the filtered points.static doublesimpsonIntegral(MathFunction func, double a, double b, int numIntervals)Computes and returns an approximation of the integral of func over [a, b], using the Simpson's 1/3 method with numIntervals intervals.
-
-
-
Field Detail
-
H
public static double H
Step length in x to compute derivatives. Default: 10-6.
-
NUMINTERVALS
public static int NUMINTERVALS
Default number of intervals for Simpson's integral.
-
-
Method Detail
-
derivative
public static double derivative(MathFunction func, double x)
Returns the first derivative of the function func evaluated at x. If the given function implementsMathFunctionWithFirstDerivative, this method callsMathFunctionWithFirstDerivative.derivative(double). Otherwise, if the function implementsMathFunctionWithDerivative, this method callsMathFunctionWithDerivative.derivative(double, int). If the function does not implement any of these two interfaces, the method usesfiniteCenteredDifferenceDerivative(MathFunction, double, double) to obtain an estimate of the derivative.- Parameters:
func- the function to derivate.x- the evaluation point.- Returns:
- the first derivative.
-
derivative
public static double derivative(MathFunction func, double x, int n)
Returns the nth derivative of function func evaluated at x. If n = 0, this returns f (x). If n = 1, this callsderivative(MathFunction, double) and returns the resulting first derivative. Otherwise, if the function implementsMathFunctionWithDerivative, this method callsMathFunctionWithDerivative.derivative(double, int). If the function does not implement this interface, the method usesfiniteCenteredDifferenceDerivative(MathFunction, double, int, double) if n is even, orfiniteDifferenceDerivative(MathFunction, double, int, double) if n is odd, to obtain a numerical approximation of the derivative.- Parameters:
func- the function to derivate.x- the evaluation point.n- the order of the derivative.- Returns:
- the nth derivative.
-
finiteDifferenceDerivative
public static double finiteDifferenceDerivative(MathFunction func, double x, int n, double h)
Computes and returns an estimate of the nth derivative of the function f (x). This method estimatesthe nth derivative of f (x) evaluated at x. This method first computes fi = f (x + iε), for i = 0,…, n, with ε = h1/n. The estimate is then given by Δnf0/h, where Δnfi = Δn-1fi+1 - Δn-1fi, and Δfi = fi+1 - fi.
,
- Parameters:
func- the function to derivate.x- the evaluation point.n- the order of the derivative.h- the error.- Returns:
- the estimate of the derivative.
-
finiteCenteredDifferenceDerivative
public static double finiteCenteredDifferenceDerivative(MathFunction func, double x, double h)
Returns (f (x + h) - f (x - h))/(2h), an estimate of the first derivative of f (x) using centered differences.- Parameters:
func- the function to derivate.x- the evaluation point.h- the error.- Returns:
- the estimate of the first derivative.
-
finiteCenteredDifferenceDerivative
public static double finiteCenteredDifferenceDerivative(MathFunction func, double x, int n, double h)
Computes and returns an estimate of the nth derivative of the function f (x) using finite centered differences. If n is even, this method returnsfiniteDifferenceDerivative(func, x - ε*n/2, n, h), with h = εn.- Parameters:
func- the function to derivate.x- the evaluation point.n- the order of the derivative.h- the error.- Returns:
- the estimate of the derivative.
-
removeNaNs
public static double[][] removeNaNs(double[] x, double[] y)Removes any point (NaN, y) or (x, NaN) from x and y, and returns a 2D array containing the filtered points. This method filters each pair (x[i], y[i]) containing at least one NaN element. It constructs a 2D array containing the two filtered arrays, whose size is smaller than or equal to x.length.- Parameters:
x- the X coordinates.y- the Y coordinates.- Returns:
- the filtered X and Y arrays.
-
integral
public static double integral(MathFunction func, double a, double b)
Returns the integral of the function func over [a, b]. If the given function implementsMathFunctionWithIntegral, this returnsMathFunctionWithIntegral.integral(double, double). Otherwise, this callssimpsonIntegral(MathFunction, double, double, int) withNUMINTERVALSintervals.- Parameters:
func- the function to integrate.a- the lower bound.b- the upper bound.- Returns:
- the value of the integral.
-
simpsonIntegral
public static double simpsonIntegral(MathFunction func, double a, double b, int numIntervals)
Computes and returns an approximation of the integral of func over [a, b], using the Simpson's 1/3 method with numIntervals intervals. This method estimates∫abf (x)dx,where f (x) is the function defined by func evaluated at x, by dividing [a, b] in n = numIntervals intervals of length h = (b - a)/n. The integral is estimated byThis method assumes that a <= b < ∞, and n is even.
(f (a) + 4f (a + h) + 2f (a + 2h) + 4f (a + 3h) + ... + f (b))
- Parameters:
func- the function being integrated.a- the left boundb- the right bound.numIntervals- the number of intervals.- Returns:
- the approximate value of the integral.
-
gaussLobatto
public static double gaussLobatto(MathFunction func, double a, double b, double tol)
Computes and returns a numerical approximation of the integral of f (x) over [a, b], using Gauss-Lobatto adaptive quadrature with 5 nodes, with tolerance tol. This method estimates∫abf (x)dx,where f (x) is the function defined by func. Whenever the estimated error is larger than tol, the interval [a, b] will be halved in two smaller intervals, and the method will recursively call itself on the two smaller intervals until the estimated error is smaller than tol.- Parameters:
func- the function being integrated.a- the left boundb- the right bound.tol- error.- Returns:
- the approximate value of the integral.
-
gaussLobatto
public static double gaussLobatto(MathFunction func, double a, double b, double tol, double[][] T)
Similar to methodgaussLobatto(MathFunction, double, double, double), but also returns in T[0] the subintervals of integration, and in T[1], the partial values of the integral over the corresponding subintervals. Thus T[0][0] = x0 = a and T[0][n] = xn = b; T[1][i] contains the value of the integral over the subinterval [xi-1, xi]; we also have T[1][0] = 0. The sum over all T[1][i], for i = 1,…, n gives the value of the integral over [a, b], which is the value returned by this method. WARNING: The user must reserve the 2 elements of the first dimension (T[0] and T[1]) before calling this method.- Parameters:
func- function being integrateda- left bound of intervalb- right bound of intervaltol- errorT- (x, y) = (values of partial intervals,partial values of integral)- Returns:
- value of the integral
-
-
DMelt 3.0 © DataMelt by jWork.ORG