WallGo.helpers.derivative

derivative(f, x, n=1, order=4, bounds=None, epsilon=1e-16, scale=1.0, dx=None, args=None)[source]

Computes numerical derivatives of a callable function. Uses the epsilon and scale parameters to estimate the optimal value of dx, if the latter is not provided.

Parameters:
  • f (function) – Function to differentiate. Should take a float or an array as argument and return a float or array (the returned array can have a different shape as the input, but the first axis must match).

  • x (float or array-like) – The position at which to evaluate the derivative.

  • n (int, optional) – The number of derivatives to take. Can be 0, 1, 2. The default is 1.

  • order (int, optional) – The accuracy order of the scheme. Errors are of order \(\mathcal{O}({\rm d}x^{\text{order}/(\text{order+n})})\). Can be 2 or 4. Note that the order at the endpoints is reduced by 1 as it would require more function evaluations to keep the same order. The default is 4.

  • bounds (tuple or None, optional) – Interval in which f can be called. If None, can be evaluated anywhere. The default is None.

  • epsilon (float, optional) – Fractional accuracy at which f can be evaluated. If f is a simple function, should be close to the machine precision. Default is 1e-16.

  • scale (float, optional) – Typical scale at which f(x) change by order 1. Default is 1.

  • dx (float or None, optional) – The magnitude of finite differences. If None, use epsilon and scale to estimate the optimal dx. Default is None.

  • args (list, optional) – List of other fixed arguments passed to the function \(f\).

Returns:

res – The value of the derivative of f evaluated at x.

Return type:

float