fdm.fdm module

class fdm.fdm.FDM(grid, deriv=1, bound_estimator=None, condition=10.0, factor=1.0)[source]

Bases: object

A finite difference method.

Call the finite difference method with a function f, an input location x (optional: defaults to 0), and a step size (optional: defaults to an estimate) to estimate the derivative of f at x. For example, if m is an instance of fdm.FDM, then m(f, 1) estimates the derivative of f at 1.

Parameters
  • grid (list[int] or np.ndarray) – Relative spacing of samples of the function used by the method.

  • deriv (int, optional) – Order of the derivative to estimate. Defaults to 1.

  • bound_estimator (fdm.FDM, optional) – A finite difference method that is tuned to perform adaptation for this method.

  • condition (float, optional) – Amplification of the infinity norm when passed to the function’s derivatives. Defaults to 10.0.

  • factor (float, optional) – Amplification of the round-off error on the function’s evaluations. Defaults to 1.0.

grid

Relative spacing of samples of the function used by the method.

Type

np.ndarray

order

Order of the estimator.

Type

int

deriv

Order of the derivative to estimate.

Type

int

bound_estimator

Estimator that taken in a function f and a location x and gives back an estimate of an upper bound on the len(grid)`th order derivative of `f at x.

Type

function or None

condition

Amplification of the infinity norm when passed to the function’s derivatives.

Type

float

factor

Amplification of the round-off error on the function’s evaluations.

Type

float

step

Estimate of the step size.

Type

float or None

acc

Estimate of the accuracy of the method.

Type

float or None

coefs

Weighting of the samples used to estimate the derivative.

Type

np.ndarray

df_magnitude_mult

Multiplier associated with the truncation error.

Type

float

f_error_mult

Multiplier associated with the round-off error.

Type

float

estimate(f=None, x=0, max_range=None)[source]

Estimate step size and accuracy of the method.

Parameters
  • f (function, optional) – Function to estimate step size and accuracy for. Defaults to the constant function 1.

  • x (tensor, optional) – Location to estimate derivative at. Defaults to 0.

  • max_range (float, optional) – Maximum amount to deviate from x.

Returns

Returns itself.

Return type

fdm.FDM

fdm.fdm.backward_fdm(order, deriv, adapt=1, **kw_args)[source]

Construct a backward finite difference method.

Further takes in keyword arguments of the constructor of fdm.FDM.

Parameters
  • order (int) – Order of the method.

  • deriv (int) – Order of the derivative to estimate.

  • adapt (int, optional) – Number of recursive calls to higher-order derivatives to dynamically determine the step size. Defaults to 1.

Returns

The desired finite difference method.

Return type

fdm.FDM

fdm.fdm.central_fdm(order, deriv, adapt=1, **kw_args)[source]

Construct a central finite difference method.

Further takes in keyword arguments of the constructor of fdm.FDM.

Parameters
  • order (int) – Order of the method.

  • deriv (int) – Order of the derivative to estimate.

  • adapt (int, optional) – Number of recursive calls to higher-order derivatives to dynamically determine the step size. Defaults to 1.

Returns

The desired finite difference method.

Return type

fdm.FDM

fdm.fdm.forward_fdm(order, deriv, adapt=1, **kw_args)[source]

Construct a forward finite difference method.

Further takes in keyword arguments of the constructor of fdm.FDM.

Parameters
  • order (int) – Order of the method.

  • deriv (int) – Order of the derivative to estimate.

  • adapt (int, optional) – Number of recursive calls to higher-order derivatives to dynamically determine the step size. Defaults to 1.

Returns

The desired finite difference method.

Return type

fdm.FDM