WallGo.helpers.gradient
- gradient(f, x, order=4, epsilon=1e-16, scale=1.0, dx=None, axis=None, args=None)[source]
Computes the gradient of a callable function. Uses the
epsilonandscaleparameters to estimate the optimal value ofdx, if the latter is not provided.- Parameters:
f (function) – Function to differentiate. Should take an array as argument and return an array.
x (array-like) – The position at which to evaluate the derivative. The size of the last axis must correspond to the number of variables on which f depends.
order (int, optional) – The accuracy order of the scheme. Errors are of order \(\mathcal{O}({\rm d}x^{\text{order}/(\text{order}+1)})\). Can be 2 or 4. The default is 4.
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 or array-like, optional) – Typical scale at which f(x) change by order 1. Can be an array, in which case each element corresponds to the scale of a different variable. Default is 1.
dx (float or np.ndarray or None, optional) – The magnitude of finite differences. Can be an array, in which case each element corresponds to the dx of a different variable.If None, use epsilon and scale to estimate the optimal dx. Default is None.
axis (list, int or None, optional) – Element of the gradient to return. If None, returns the whole gradient. Default is None.
args (list, optional) – List of other fixed arguments passed to the function \(f\).
- Returns:
res – The value of the gradient of
fevaluated atx.- Return type:
float