SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
umontreal.ssj.functions.MathFunctionUtil Class Reference

Provides utility methods for computing derivatives and integrals of functions. More...

Static Public Member Functions

static double derivative (MathFunction func, double x)
 Returns the first derivative of the function func evaluated at x.
static double derivative (MathFunction func, double x, int n)
 Returns the \(n\)th derivative of function func evaluated at x.
static double finiteDifferenceDerivative (MathFunction func, double x, int n, double h)
 Computes and returns an estimate of the \(n\)th derivative of the function \(f(x)\).
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.
static double finiteCenteredDifferenceDerivative (MathFunction func, double x, int n, double h)
 Computes and returns an estimate of the \(n\)th derivative of the function \(f(x)\) using finite centered differences.
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 double integral (MathFunction func, double a, double b)
 Returns the integral of the function func over \([a, b]\).
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.
static double gaussLobatto (MathFunction func, double a, double b, double tol)
 Computes and returns a numerical approximation of the integral of.
static double gaussLobatto (MathFunction func, double a, double b, double tol, double[][] T)
 Similar to method gaussLobatto(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 Public Attributes

static double H = 1e-6
 Step length in \(x\) to compute derivatives.
static int NUMINTERVALS = 1024
 Default number of intervals for Simpson’s integral.

Detailed Description

Provides utility methods for computing derivatives and integrals of functions.

Definition at line 35 of file MathFunctionUtil.java.

Member Function Documentation

◆ derivative() [1/2]

double umontreal.ssj.functions.MathFunctionUtil.derivative ( MathFunction func,
double x )
static

Returns the first derivative of the function func evaluated at x.

If the given function implements

MathFunctionWithFirstDerivative, this method calls derivative(double). Otherwise, if the function implements MathFunctionWithDerivative, this method calls derivative(double, int). If the function does not implement any of these two interfaces, the method uses finiteCenteredDifferenceDerivative(MathFunction, double, double) to obtain an estimate of the derivative.

Parameters
functhe function to derivate.
xthe evaluation point.
Returns
the first derivative.

Definition at line 92 of file MathFunctionUtil.java.

◆ derivative() [2/2]

double umontreal.ssj.functions.MathFunctionUtil.derivative ( MathFunction func,
double x,
int n )
static

Returns the \(n\)th derivative of function func evaluated at x.

If \(n=0\), this returns \(f(x)\). If \(n=1\), this calls derivative(MathFunction, double) and returns the resulting first derivative. Otherwise, if the function implements MathFunctionWithDerivative, this method calls derivative(double, int). If the function does not implement this interface, the method uses finiteCenteredDifferenceDerivative(MathFunction, double, int, double) if \(n\) is even, or finiteDifferenceDerivative(MathFunction, double, int, double) if

\(n\) is odd, to obtain a numerical approximation of the derivative.

Parameters
functhe function to derivate.
xthe evaluation point.
nthe order of the derivative.
Returns
the \(n\)th derivative.

Definition at line 120 of file MathFunctionUtil.java.

◆ finiteCenteredDifferenceDerivative() [1/2]

double umontreal.ssj.functions.MathFunctionUtil.finiteCenteredDifferenceDerivative ( MathFunction func,
double x,
double h )
static

Returns \((f(x + h) - f(x - h))/(2h)\), an estimate of the first derivative of \(f(x)\) using centered differences.

Parameters
functhe function to derivate.
xthe evaluation point.
hthe error.
Returns
the estimate of the first derivative.

Definition at line 174 of file MathFunctionUtil.java.

◆ finiteCenteredDifferenceDerivative() [2/2]

double umontreal.ssj.functions.MathFunctionUtil.finiteCenteredDifferenceDerivative ( MathFunction func,
double x,
int n,
double h )
static

Computes and returns an estimate of the \(n\)th derivative of the function \(f(x)\) using finite centered differences.

If \(n\) is even, this method returns finiteDifferenceDerivative(func, x - *n/2, n, h), with

\(h=\epsilon^n\).

Parameters
functhe function to derivate.
xthe evaluation point.
nthe order of the derivative.
hthe error.
Returns
the estimate of the derivative.

Definition at line 193 of file MathFunctionUtil.java.

◆ finiteDifferenceDerivative()

double umontreal.ssj.functions.MathFunctionUtil.finiteDifferenceDerivative ( MathFunction func,
double x,
int n,
double h )
static

Computes and returns an estimate of the \(n\)th derivative of the function \(f(x)\).

This method estimates

\[ \frac{d^nf(x)}{dx^n}, \]

the \(n\)th derivative of \(f(x)\) evaluated at \(x\). This method first computes \(f_i=f(x+i\epsilon)\), for \(i=0,…,n\), with \(\epsilon=h^{1/n}\). The estimate is then given by

\(\Delta^nf_0/h\), where \(\Delta^nf_i=\Delta^{n-1}f_{i+1} - \Delta^{n-1}f_i\), and \(\Delta f_i = f_{i+1} - f_i\).

Parameters
functhe function to derivate.
xthe evaluation point.
nthe order of the derivative.
hthe error.
Returns
the estimate of the derivative.

Definition at line 149 of file MathFunctionUtil.java.

◆ gaussLobatto() [1/2]

double umontreal.ssj.functions.MathFunctionUtil.gaussLobatto ( MathFunction func,
double a,
double b,
double tol )
static

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

\[ \int_a^b f(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
functhe function being integrated.
athe left bound
bthe right bound.
tolerror.
Returns
the approximate value of the integral.

Definition at line 311 of file MathFunctionUtil.java.

◆ gaussLobatto() [2/2]

double umontreal.ssj.functions.MathFunctionUtil.gaussLobatto ( MathFunction func,
double a,
double b,
double tol,
double T[][] )
static

Similar to method gaussLobatto(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] \(= x_0 = a\) and T[0][n] \(=x_n =b\); T[1][i] contains the value of the integral over the subinterval \([x_{i-1}, x_i]\); 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
funcfunction being integrated
aleft bound of interval
bright bound of interval
tolerror
T\((x,y)\) = (values of partial intervals,partial values of integral)
Returns
value of the integral

Definition at line 359 of file MathFunctionUtil.java.

◆ integral()

double umontreal.ssj.functions.MathFunctionUtil.integral ( MathFunction func,
double a,
double b )
static

Returns the integral of the function func over \([a, b]\).

If the given function implements MathFunctionWithIntegral, this returns integral(double, double). Otherwise, this calls simpsonIntegral(MathFunction, double, double, int) with NUMINTERVALS intervals.

Parameters
functhe function to integrate.
athe lower bound.
bthe upper bound.
Returns
the value of the integral.

Definition at line 248 of file MathFunctionUtil.java.

◆ removeNaNs()

double[][] umontreal.ssj.functions.MathFunctionUtil.removeNaNs ( double[] x,
double[] y )
static

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
xthe \(X\) coordinates.
ythe \(Y\) coordinates.
Returns
the filtered \(X\) and \(Y\) arrays.

Definition at line 215 of file MathFunctionUtil.java.

◆ simpsonIntegral()

double umontreal.ssj.functions.MathFunctionUtil.simpsonIntegral ( MathFunction func,
double a,
double b,
int numIntervals )
static

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

\[ \int_a^b f(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 by

\[ \frac{h}{3}(f(a)+4f(a+h)+2f(a+2h)+4f(a+3h)+\cdots+f(b)) \]

This method assumes that \(a\le b<\infty\), and \(n\) is even.

Parameters
functhe function being integrated.
athe left bound
bthe right bound.
numIntervalsthe number of intervals.
Returns
the approximate value of the integral.

Definition at line 271 of file MathFunctionUtil.java.

Member Data Documentation

◆ H

double umontreal.ssj.functions.MathFunctionUtil.H = 1e-6
static

Step length in \(x\) to compute derivatives.

Default: \(10^{-6}\).

Definition at line 40 of file MathFunctionUtil.java.

◆ NUMINTERVALS

int umontreal.ssj.functions.MathFunctionUtil.NUMINTERVALS = 1024
static

Default number of intervals for Simpson’s integral.

Definition at line 73 of file MathFunctionUtil.java.


The documentation for this class was generated from the following file: